PostIT

[Server/NginX] NginX 주요 설정 정보 - 퍼옴 본문

Webserver/NginX

[Server/NginX] NginX 주요 설정 정보 - 퍼옴

shun10114 2016. 12. 15. 15:11

http://sarc.io/index.php/nginx/61-nginx-nginx-conf

지난 번 포스팅에서 OS X 에서 NginX 1.4.4 를 설치하여 보았습니다. 이번에는 설정 파일인 nginx.conf 의 주요 설정을 확인해 보고 또 변경해 보고자 합니다.

 

1.  Listen Port 설정

default 는 80 입니다. 하지만 모든 경우에 80 을 사용하지는 않습니다. 이 Port 를 변경하려면 conf/nginx.conf 의 listen 을 수정합니다. 

    server {
        listen       80;
......
}

 80 대신에 원하는 Listen Port 를 적으면 됩니다.

 

2. Document Root 설정

root 를 확인합니다. default 는 html 디렉토리 입니다. 

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

html 대신에 원하는 Document Root Directory 를 적으면 됩니다.

 

3. 초기 페이지 설정

index 를 확인합니다. default 는 index.html 입니다. 

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

만일 index 에 지정된 파일들이 존재하지 않는 경우 403 Forbidden 오류를 만나게 됩니다.

 

4. proxy_pass 설정 

특정 확장자 요청을 넘기는 설정을 추가해 봅니다. 즉, NginX 뒷 단에 WAS 가 존재하는 경우이며, 확장자 기반으로 요청 처리를 분리하는 것입니다. 아래 예는 *.do 에 대하여 http://localhost:8080 으로 넘기는 것입니다.

        location ~ \.do$ {
            proxy_pass  http://localhost:8080;
        }

 

5. Sub Domain 설정

최초 설치 후의 nginx.conf 를 보면 이렇습니다.

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

바로 본론부터 들어가서, 사이트에 2개의 Sub Domain 을 추가한다고 해 봅시다.

  • appsroot.com
  • nginx1.appsroot.com (추가!)
  • nginx2.appsroot.com (추가!)
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/nginx;
            index  index.html index.htm;
        }
......
}

server { listen 80; server_name nginx1.appsroot.com; location / { root /home/nginx1; index index.html index.htm; } }

server { listen 80; server_name nginx2.appsroot.com; location / { root /home/nginx2; index index.html index.htm; } }

server block 이 2개 추가되었고 각각 nginx1.appsroot.com (/home/nginx1), nginx2.appsroot.com (/home/nginx2) 의 역할을 담당하고 있습니다.

그리고 default 값을 사용하는 경우는 명시적으로 설정하지 않아도 무관합니다. listen 의 80, index 의 index.html 등 말입니다. 

    server {
        server_name  nginx2.appsroot.com;

        location / {
            root   /home/nginx2;
        }
    }

이렇게 해도 됩니다.

 

6. access log 설정

access_log 를 확인합니다. default 는 logs/access.log 입니다. 앞서 설명한 Sub Domain (Virtual Host) 로 분리되어 있어도 별도로 access log 설정을 하지 않으면 하나의 access.log 파일로 쌓입니다.

# ls -tlr
total 136
-rw-r--r--  1 nginx  staff  42485 Feb 23 03:29 access.log
-rw-r--r--  1 nginx  staff  20064 Feb 23 13:35 error.log
-rw-r--r--  1 nginx  staff      6 Feb 23 13:35 nginx.pid

각 server 별로 log 를 분리합니다.

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/host.access.log;
......
}

server { #listen 80; server_name nginx1.appsroot.com; access_log logs/nginx1.access.log; ...... }

server { #listen 80; server_name nginx2.appsroot.com; access_log logs/nginx2.access.log; ......

그리고 다시 NginX 를 기동한 후 logs 디렉토리를 확인합니다.

# ls -tlr
total 136
-rw-r--r--  1 nginx  staff  42485 Feb 23 03:29 access.log
-rw-r--r--  1 nginx  staff  20125 Feb 23 13:38 error.log
-rw-r--r--  1 nginx  staff      0 Feb 23 13:39 nginx2.access.log
-rw-r--r--  1 nginx  staff      0 Feb 23 13:39 nginx1.access.log
-rw-r--r--  1 nginx  staff      6 Feb 23 13:39 nginx.pid
-rw-r--r--  1 nginx  staff      0 Feb 23 13:39 host.access.log

새롭게 host.access.log, nginx1.access.log, nginx2.access.log 파일이 생성되었습니다. (물론 아직 어떤 요청도 처리하지 않은 상태여서 0 바이트로 나오고 있습니다)


Comments