2017년 6월 21일 수요일

[EtC] Linux 에 nginx 설치 및 설정


Environment
- Ubuntu 14.x
- nginx 1.12.0
- spring boot 1.5.3 : executeable jar deploy(using embeded undertow)




nginx 공식 사이트 : http://nginx.org/en/download.html


# 컴파일시 필요한 의존성에 걸린 패키지들을 설치 한다.
root@freecatz:~# apt-get install openssl openssl-devel pcre pcre-devel zlib zlib-devel

root@freecatz:~# wget http://nginx.org/download/nginx-1.12.0.tar.gz

root@freecatz:~# tar zxvf nginx-1.12.0.tar.gz

root@freecatz:~# cd nginx-1.12.0

root@freecatz:~# ./configure --prefix=/usr/local/nginx-1.12.0 --with-http_ssl_module --with-http_v2_module --with-openssl=/root/archive/openssl-1.0.2l

* 참고 : --with-openssl 옵션을 적용하는 경우 openssl 의 소스 경로를 넣어 주어야 한다.

root@freecatz:~# make && make install

root@freecatz:~# cp /usr/local/nginx-1.12.0/conf/nginx.conf /usr/local/nginx-1.12.0/conf/nginx.conf.ori

# 설정 파일 편집
root@freecatz:~# vi /usr/local/nginx-1.12.0/conf/nginx.conf

worker_processes  auto;

events {
    worker_connections  1024;
    use epoll;
    multi_accept on;
}

... 중략 ...
http {

    # 보안을 위해 서버 버전 정보 노출을 막는다.
    server_tokens off;
    sendfile on;
    client_max_body_size 10M;

    ... 중략 ...
    upstream undertow {
        ip_hash;
        server 127.0.0.1:8080 weight=1 max_fails=3 fail_timeout=5s;
        server 127.0.0.1:8081 weight=1 max_fails=3 fail_timeout=5s;
        server 127.0.0.1:8082 weight=1 max_fails=3 fail_timeout=5s;
    }

    server {
        listen           80;
        server_name  freecatz.pe.kr;
        # return 301 https://$server_name$request_uri;
        rewrite ^ https://$server_name$request_uri? permanent;
    }

    server {
        listen           443 ssl;
        server_name  freecatz.pe.kr;

        # Chrome 콘솔에 "Error parsing header X-XSS-Protection: 1; mode=block, 1:mode=block: expected semicolon at character position 14. The default protections will be applied." 메세지 나타남.
        # add_header X-XSS-Protection "1; mode=block";

        ssl on;
        ssl_certificate      /etc/fullchain.pem;
        ssl_certificate_key  /etc/privkey.pem;

        # 보안상의 이유로 SSLv2 SSLv3 은 사용 하지 않는 것이 좋다고 한다. TLSv1.3을 지원 하기 위해서는 OpenSSL 1.1.1 이상의 버젼이 필요 하다고 한다.
        ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

 
        location / {
            charset                           utf-8;
            proxy_set_header              Host $http_host;
            proxy_set_header              X-Real-IP $remote_addr;
            proxy_set_header              X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header              X-Forwarded-Proto $scheme;
            proxy_set_header              X-NginX-Proxy true;
            proxy_set_header              X-Forwarded-Port $server_port;

            proxy_connect_timeout      150;
            proxy_send_timeout          100;
            proxy_read_timeout          100;
            proxy_redirect                  off;

            proxy_pass http://undertow$request_uri;

        } # location / end

    } # server end

} # http end
... 중략 ...

# nginx 설정 파일 테스트
root@freecatz:~# nginx -t
nginx: the configuration file /usr/local/nginx-1.12.0/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12.0/conf/nginx.conf test is successful

# nginx 시작
root@freecatz:~# /usr/local/nginx-1.12.0/sbin/nginx

# nginx 종료
root@freecatz:~# /usr/local/nginx-1.12.0/sbin/nginx -s quit

# nginx 재시작
root@freecatz:~# /usr/local/nginx-1.12.0/sbin/nginx -s reload