작은숲:위키노트/Nginx 설정

큰숲백과, 나무를 보지 말고 큰 숲을 보라.

Nginx 초기 설정 내용

따로 설정하지 않았다면 Nginx의 설정 파일은 /etc/nginx/nginx.conf이다. 소스를 컴파일해서 설치했다면 /usr/local/nginx/conf/nginx.conf 파일일 수도 있다. 아래는 수정하기 전의 nginx.conf 파일의 내용이다.

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

위 설정 파일의 마지막에 보면 include /etc/nginx/conf.d/*.conf;이라고 나오는데 이 말은 /etc/nginx/conf.d 디렉토리에 있는 모든 conf 파일을 읽어들인다는 것이다. 이건 가상 호스트를 위한 것인데 만약 서버 내에 여러 가상 호스트를 두고 있다면 가상 호스트 설정을 nginx.conf 파일에서 하는 것보다는 conf.d 디렉토리 아래에 가상 호스트마다 개별 파일로 만들어서 관리하는 것이 더 유지보수하기 편할 수도 있다. /etc/nginx/conf.d 디렉토리에 보면 default.conf, example_ssl.conf 두 설정 파일이 있다. default.conf 파일은 가상 호스트 설정의 기본적인 예를 보여주므로 이 파일을 수정하거나 복사해서 가상 호스트 설정에 사용한다. example_ssl.conf 파일은 SSL로 접속하는, 즉 HTTPS로 접속하는 것을 처리하기 위한 것이다. 만약 HTTPS를 사용하지 않는다면 이 파일을 지우거나 파일 이름을 .conf가 아닌 다른 이름으로 바꾸도록 하자. nginx.conf 파일에서의 설정에 따라 conf.d 디렉토리 아래에 있는 .conf로 끝나는 파일을 모두 읽어들여 설정하게 되니 필요 없는 파일은 삭제하거나 이름을 바꾸는 것이 좋다.

Nginx 사용자 설정

# refer:
#   https://wiki.gentoo.org/wiki/Nginx/ko
#   https://www.nginx.com/resources/admin-guide/nginx-web-server/
#   http://nginx.org/en/docs/http/ngx_http_core_module.html
#   http://nginx.org/en/docs/http/ngx_http_charset_module.html
#   http://nginx.org/en/docs/http/ngx_http_gzip_module.html
user                nobody nobody;
worker_processes    8;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
    use epoll;      # you should use epoll here for Linux kernels 2.6.x
}http {
    charset             utf-8;
    override_charset    on;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # disable emitting nginx version in error messages and "Server" response header field
    server_tokens       off;
    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          /var/log/nginx/access.log  main;
    client_header_timeout   10m;
    client_body_timeout     10m;
    send_timeout            10m;
    connection_pool_size        512;
    client_header_buffer_size   1k;
    large_client_header_buffers 4 2k;
    request_pool_size           4k;
    client_body_buffer_size     256k;
    output_buffers      1 32k;
    postpone_output     1460;
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout   65;
    ignore_invalid_headers  on;
    index index.html;
    # include server configurations
    #
    include /etc/nginx/conf.d/*.conf;
}

user 지시자

Nginx는 데몬이 시작되면 루트 프로세스가 각 작업 프로세스를 생성한다. 이때 루트 프로세스는 root 권한으로 실행되고 작업 프로세스는 user 지시자에서 설정한 계정으로 실행된다. 기본값은 nobody이고, Nginx를 패키지로 설치하면 nginx.conf 설정 파일에 nginx 계정으로 설정되어 있다.

worker_process 지시자

worker_process 지시자는 작업 프로세스를 몇 개나 실행할 것인가를 지정하는 것이다. 초기 설정처럼 worker_process 1;이라고 되어 있다면 작업 프로세스를 하나만 띄운다는 말이다. 보통 서버의 효율을 높이기 위해 작업 프로세스는 CPU 코어의 수만큼 띄우는 것이 좋다. 레드햇 계열의 리눅스에서는 cat /proc/cpuinfo | grep processor 명령으로 CPU 코어의 수를 확인할 수 있다.

# cat /proc/cpuinfo | grep processor
processor       : 0
processor       : 1
processor       : 2
processor       : 3
processor       : 4
processor       : 5
processor       : 6
processor       : 7

같이 보기

참고

이 작은숲 문서의 출처는 위키노트의 위키노트/Nginx 설정 문서입니다.