Nginx 기본 설정 및 매개 변수

24613 단어
태그 (공백 구분): nginx
1 전역 설정
#    
user nginx;

#    ,      ,    CPU    2  CPU。
worker_processes 2;

#      ,   notice  ,  debug,info,warn,error,crit  ,
#debug    ,crir    ,        
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;


#     pid        
#pid logs/nginx.pid;


#                。             (ulimit -n) nginx     ,
#  nginx           ,     ulimit -n       。
#   linux 2.6            65535,worker_rlimit_nofile        65535。
#    nginx                  ,      10240,
#      3-4          10240 ,     502  。
worker_rlimit_nofile 65535


#          
events {
    #  epoll I/O   。linux  epoll,FreeBSD    kqueue,window    。
    #    :
    # apache  ,nginx         ,        

    #A)      
    #Select、poll        ,               ,nginx   select poll

    #B)      
    #Kqueue:   FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0   MacOS X.       MacOS X    kqueue         。
    #Epoll:   Linux  2.6        。
    #/dev/poll:   Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+   Tru64 UNIX 5.1A+。
    #Eventport:   Solaris 10。              ,          。

    use epoll;


    #    worker process        ,                。
    #       ,            ,   ,    cpu  100%  。
    #      nginx          :max_clients = worker_processes * worker_connections
    #             ,max_clients = worker_processes * worker_connections / 4
    #             4,         
    #       ,       Nginx Server            :4 * 8000 = 32000
    #
    # worker_connections              
    #       IO    ,max_clients                  
    #                      ,  1GB                 10   
    #       1G     ECS              :
    # [root@AY140331150243Z ~]# cat /proc/sys/fs/file-max
    # 188015
    # 32000 < 34336,                      ,                 
    #   ,worker_connections       worker_processes                         
    #                        ,             CPU       
    #   ,                  ,                     。
    # ulimit -SHn 65535
    worker_connections 1024;


}


http {
    #  mime  ,   mime.type    
    include mime.types;
    default_type application/octet-stream;


    #       
    # $remote_addr $http_x_forwarded_for        ip  ;
    # $remote_user:           ;
    # $time_local:            ;
    # $request:        url http  ;
    # $status:         ;   200,
    # $body_bytes_sent :                ;
    # $http_referer:                ;
    # $http_user_agent:            ;
    #
    #   web            ,           IP   ,   $remote_add   IP           iP  。
    #              http    ,     x_forwarded_for  ,          IP                 。
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    log_format log404   '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';

    access_log logs/host.access.log main;
    access_log logs/host.access.404.log log404;


    #                     
    client_max_body_size 20M;

    #            headebuffer  
    client_header_buffer_size 32k;

    #                    
    client_body_temp_path /dev/shm/client_body_temp;

    #                         ,     4 32KB
    large client_header_buffers 4 32k;

    #           
    sendfile on;
    #         
    tcp_nopush on;
    #         
    tcp_nodelay on;

    #                 
    # keepalive_timeout 0;
    keepalive_timeout 65;

    #              
    client_header_timeout 10;
    #                
    client_body_timeout 10;
    #               
    send_timeout

    #client_body_in_file_only   On    client post              debug
    #client_body_temp_path                3   
    #location URL    .                    


2 gzip 설정
 ########################   gzip       ##############################
    #  gzip  
    gzip on;
    #              
    gzip_min_length 1k;
    #  4    16K            
    gzip_buffers 4 16k;
    #    http     ,   1.1
    gzip_http_version 1.1;
    #  gzip   ,1-9    ,     ,    
    gzip_comp_level 2;
    #       
    gzip_types text/plain application/x-javascript text/css application/xml;
    #           gzip     
    gzip_vary on;

3 역방향 에이전트 설정
########################   upstream       ##############################

    # 1、  (  )
    #                       ,       down ,     。
    upstream bakend1 {
        server 192.168.0.14;
        server 192.168.0.15;
    }

    # 2、weight
    #       ,weight        ,              。
    upstream bakend2 {
         server 192.168.0.14 weight=10;
        server 192.168.0.15 weight=10;
    }

    # 3、ip_hash
    #        ip hash    ,                 ,    session   。
    #           :
    #   1.down     server       
    #   2.weight  ,        。
    #   3.max_fails:            1.        ,   proxy_next_upstream         
    #   4.fail_timeout:max_fails    ,     。
    #   5.backup:      backup  down      ,  backup  。           。
    upstream bakend3{
        ip_hash;
        server 192.168.0.14 down;
        server 192.168.0.15 weight=2;
        server 192.168.0.16;
        server 192.168.0.17 backup;
    }

    # 4、fair(   )
    #                 ,          。
    upstream backend4 {
        server 192.168.0.14;
        server 192.168.0.15;
        fair;
    }

    # 5、url_hash(   )
    #    url hash       ,   url           ,             。
    #  upstream   hash  ,server       weight      ,hash_method    hash  
    upstream backend5 {
        server squid1:3128;
        server squid2:3128;
        hash $request_uri;
        hash_method crc32;
    }


}

4 php 가상 호스트 설정
server {
        #  80  
        listen 80;

        #server_name                :
        #
        # 1、   server_name  ,  :
        # server {
        #   listen 80;
        #   server_name domain.com www.domain.com;
        #   ...
        # }
        #
        # 2、 *         :
        # server {
        #   listen 80;
        #   server_name *.domain.com;
        #   ...
        # }
        #
        # 3、 *         :
        # server {
        #   listen 80;
        #   server_name www.*;
        #   ...
        # }
        #
        # 4、       :
        # server {
        #   listen 80;
        #   server_name ~^(?.+)\.domain\.com$;
        #   ...
        # }
        #    http://nginx.org/en/docs/http/server_names.html
        #
        #      www.nginx.cn   
        server_name www.nginx.cn;

        #               
        root html;

        #            
        access_log logs/nginx.access.log main;


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


        #         
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }


        #    ,nginx    
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            #  30 ,         ,        ,
            #      ,         。
            expires 30d;
        }

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


        #PHP           FastCGI  .   FastCGI    .
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }


        #     .htxxx   
        location ~ /.ht {
            deny all;
        }

}

5 mp3 가상 호스트 설정

server
    {
        #       
        listen 80;
        #       
        server_name *.img.com;

        #   “mp3 exe”           
        location ~* \.(mp3|exe)$ {
            #                ,  URL
            proxy_pass http://img_relay$request_uri;

            #     ,                         
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }


        location /face {
            if ($http_user_agent ~* "xnp") {
                rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
            }

            proxy_pass http://img_relay$request_uri;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            error_page 404 502 = @fetch;
        }

        location @fetch {
            access_log /data/logs/face.log log404;
            rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
        }


        #    Nginx     
        location /NginxStatus {
            stub_status on;
            access_log on;
            auth_basic "NginxStatus";
            auth_basic_user_file conf/htpasswd;
        }


        #    .htxxx  
        location ~ /\.ht {
            deny all;
        }

    }

6 https 가상 호스트 설정
########################    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;
    }
}

7 변수 설명
#  :  
#Ngx_http_core_module        ,      apache         。
#         title   ,  $http_user_agent,$http_cookie  。
#           
#$args             
#$content_length      “Content_Length”  。
#$content_type        ”Content_Type”  
#$document_root        root      
#$document_uri $uri  
#$host      “Host”       request   server   (  Host )  
#$limit_rate         
#$request_method   request method,   “GET” “POST”
#$remote_addr   ip
#$remote_port   port
#$remote_user      , ngx_http_auth_basic_module  
#$request_filename           , root alias URI request    
#$request_body_file
#$request_uri          URI
#$query_string $args  
#$sheeme http  (http,https)         
#Rewrite ^(.+)$ $sheme://example.com$; Redirect;
#$server_protocol   request   ,  “HTTP/ “HTTP/
#$server_addr request   server ip,                   。        ,    listen     ip,   bind  。
#$server_name         
#$server_port            

좋은 웹페이지 즐겨찾기