보안/java

2021.03.15 nginx config 설정 방법 중 일부

MustThanks 2021. 3. 15. 10:10
반응형
#nginx.conf 파일
# - nginx의 설정이 들어가는 핵심 파일.

#conf.d 폴더
# - nginx.conf에서 include로 불러올 수 있는 conf 파일 저장 폴더.


#user  nobody;


#[ worker_processes  1;  #의미
#  - 사용 코어 갯수 세팅
#  - 1이면 모든 요청을 하나의 프로세스로 실행하겠다는 의미
#  - CPU 멀티코어 시스템에서 1이면 하나의 코어만으로 요청을 처리
#  - auto로 놓는 경우가 많다.
#  - 1로 하는 설정을 core 모듈 설정이라고 의미
#  - nginx 설정값을 정하는 경우가 대부분 이에 해당한다.]

worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

# [ events 블록 의미
# - 네트워크 동작방법과 관련된 설정
# - worker_connections : 하나의 프로세스가 처리할 수 있는 커넥션의 수
# - 최대 접속자수는 worker_processes X worker_connections가 된다. ]

events {
    # worker_processes 갯수에 비래하여 커넥션
    worker_connections  1024;
}

# [ http 블록 의미
# - 하위에 server 블록, 그리고 location 블록을 갖는 루트 블록 
# - http 블록에 선언된 값은 하위 블록에 상속
# - 서버 설정의 기본값
# - include 는 server 블록에서도 사용이 가능
# - include 는 conf.d 에 정의해놓은 파일들을 이용하는데 사용 ]
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

# [ upstream 블록
   # - origin 서버(WAS) 를 의미
   # - nginx는 downstream을 의미
   # - 여러 서버 구성하여 , weight 값 할당이 가능 ]

   #[ upstream cluster 에 사용하는 옵션 들
   # - ip_hash :  같은 ip서버를 호출
   # - weight=n : 업스트림 서버의 비중을 나타냄 weight=2 > 2배 더 호출 함.\
   #max_fails=n : 실패 발생하면 서버 죽었다고 판단.
   #fail_timeout=n : 해당 시간만큼 응답없으면 죽었다고 판단.
   #backup : 모든 서버가 동작하지 않을 때 backup 사용.


  upstream cluster{
   # server 는 host:port; 로 구성
    server backend.goods.com:443; 
   #keepalive 로 유지시키는 최대 커넥션 수(연결시 마다 TCP handshake를 하지 않음)
    keepalive 100;
   }
  
    server {
        #포트
        listen       80;
        #도메인
        server_name  localhost;

        #charset
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        proxy_pass에서 클러스터 세팅 적용 
        # WebSocket connection to 'ws://localhost:8080/socket.io/?EIO=3&transport=websocket&sid=3uzcJLGrxo8cZq_gAAAD' failed: Error during WebSocket handshake: Unexpected response code: 400
        # 위 웹소켓 연결 에러로 proxy_http_version, proxy_set_header 옵션 추가
        # proxy_http_version : WebSocket에 필요한 노드 백엔드와 통신 할 때 HTTP / 1.1을 사용하도록 지시
        # proxy_set_header : WebSocket을 사용할 때 브라우저가 HTTP를 통해 시작하는 업그레이드 요청에 응답하도록 지시

        location / {
            # / root 셋팅 
            root   html;

            #시작페이지
            index  index.html index.htm;

           #클러스터 세팅 위의 upstream cluster 를 호출
           proxy_pass http://cluster;


           #WebSocket connection to 'ws://localhost' 오류 발생 시 아래의 내용을 추가
           #WebSocket에 필요한 백엔드  통신에  HTTP / 1.1을 사용을 명시
            proxy_http_version 1.1;
           #WebSocket을 이용시 브라우저가 HTTP 통신을 시작하는 업그레이드 요청에 응답
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host $host;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}