기업 급 nginx. conf 최적화 참조 템 플 릿

다음은 제 가 정리 한 매우 실 용적 인 기업 급 nginx. conf 최적화 참고 템 플 릿 입 니 다. 전체 설정 이 반드시 각종 환경 에 적합 하지 않 기 때문에 여러분 들 이 각자 의 수요 에 따라 일부 기능 을 선택 하여 자신의 nginx 설정 파일 에 배치 해 야 합 니 다.
user nginx;     #Nginx       
worker_processes 4;     #   CPU   
worker_cpu_affinity 0001 0010 0100 1000;  #    Nginx     CPU 
worker_rlimit_nofile 65535;     #worker         ,                ,
                                #       ulimit -n       
error_log  logs/error.log  info;     #             

#pid        logs/nginx.pid;     #pid      

dso {
#       load ngx_http_lua_module.so;    #    
}

events {               #        
    use epoll;     #  epoll I/O      
    worker_connections  20480;     #               ,
                                    #                ,
                                    #      ulimit -n       
}

http {
    include       mime.types;     #             
    default_type  application/octet-stream;     #      
    log_format main '$remote_addr - $remote_user [$time_local]  '
                    '"$request" [$request_uri]  $status $body_bytes_sent $request_body  '
                    '"$http_referer"  "$http_user_agent" $http_x_forwarded_for  ';     #      

    server_names_hash_max_size 512;     #   512kb,             。     CPU L1 4-5 
    server_names_hash_bucket_size 128;     #   128kb,                 HASH ,
                                            #           ,      。

    sendfile        on;     #           
    #tcp_nopush     on;     #    off,     Linux  TCP_CORK socket  ,
                            #        sendfile    。
                            #     http response header                 ,
                            #               。

    keepalive_timeout  65;     #    75s,                。
    client_body_timeout 15;     #    60s,                。
    send_timeout 25;     #    60s,        HTTP             

    client_max_body_size 8m;     #    1m,                 

    server_tokens on;     #  Nginx       

    #limit_conn_zone $binary_remote_addr zone=addr:10m;  #        ,  limit_conn  
    #limit_req_zone $binary_remote_addr zone=one:10m rete=1r/s; #       IP  key ,
                                                                #       one,
                                                                #  10m    ,
                                                                #       1 1   (request),
                                                                #  limit_req  

    fastcgi_connect_timeout 240;    #    60s,  Nginx      FastCGI          ,
                                    #          75s
    fastcgi_send_timeout 240;   #    60s,  Nginx  FastCGI            
    fastcgi_read_timeout 240;   #   Nginx FastCGI               
    fastcgi_buffer_size 64k;    #  Nginx FastCGI        
    fastcgi_buffers 4 64k;  #    8 4k|8k,       FastCGI                       
    fastcgi_busy_buffers_size 128k; #    8k|16k,              fastcgi_buffers  ,
                                    #       fastcgi_buffers*2
    fastcgi_temp_file_write_size 128k;  #FastCGI       ,    128 - 256 KB
    #fastcgi_temp_path /data/ngx_fcgi_tmp;  #FastCGI      
    fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g;
                                            #factcgi_cache    ,
                                            #          ,  2:2   256*256    ,
                                            #keys_zone          ,cache      ,inactive        ,
                                            #max_size           ,      fastcgi_cache      
                                            #fastcgi_temp_path   fastcgi_cache_path   ,               

    gzip on; #  gzip    
    gzip_min_length 1k; #        
    gzip_buffers 4 16k; #       
    gzip_http_version 1.1; #    (  1.1,     squid2.5   1.0)
    gzip_comp_level 2;  #    。    gzip   ,1     ,
                        #      ;9     ,     ,    ,     CPU
    gzip_types text/plain  text/css application/x-javascript application/xml; #         
    gzip_disable "MSIE [1-6]."  #    IE6 gzip  
    gzip_vary on;   #vary header  。

    ############      ###########
    upstream default_pools {         #        
            server 10.0.0.14:33101;
            server 10.0.0.15:33101 down;    #        ,  ip_hash  
            server 10.0.0.16:33101;
            ip_hash;    #    
            #check interval=3000 rise=2 fail=5 timeout=1000 type=http;   #  nginx_upstream_check_module  ,
                                                                         #      
    }
    upstream upload_pools {          #        
        server 10.0.0.9:80 weight=1;    #weight   ,1-100,    ,    

    }
    upstream static_pools {          #        
        server 10.0.0.10:80 weight=1;
    }
    ############      ###########

    #        
    server {
        listen 80 default_server;
        server_name _;
        rewrite ^(.*) http://www.test.com$1 permanent;
    }

    # HTTP server
    server {
            listen       80 ;
            server_name  www.test.com ;

            #charset utf-8; #    

            #access_log  logs/host.access.log  main;
            #error_log  logs/host.error.log  main;

            #nginx      ,     IP
            if ($remote_addr = 10.0.0.7) {
                return 403;
            }
            if ($remote_addr = 114.114.114.114) {
                set $allow_access_root 'true';
            }

            #     URI  ,           
            if ($request_uri ~* "^/upload/(.*)$") {
                proxy_pass http://upload_pools/$1;
            }
            if ($request_uri ~* "^/static/(.*)$") {
                proxy_pass http://static_pools/$1;
            }

            location / {
                #          ,          
                if ($http_user_agent ~* "MSIE") {
                    #           IE(MSIE),     static_pools   
                    proxy_pass http://static_pools;
                }
                if ($http_user_agent ~* "Chrome") {
                #if ($http_user_agent ~* "Firefox") {
                    #              (Chrome),     upload_pools   
                    proxy_pass http://upload_pools;
                }
#                if ($http_user_agent ~* "android") {
#                    proxy_pass http://static_pools;
#                }
#                if ($http_user_agent ~* "iphone") {
#                    proxy_pass http://upload_pools;
#                }

                proxy_pass http://default_pools;
                proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
                proxy_set_header Host $host;           #proxy_set_header                          IP
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size 10m; #                
                client_body_buffer_size 128k; #                  ,
                proxy_connect_timeout 90; #nginx            (      )
                proxy_send_timeout 90; #           (      )
                proxy_read_timeout 90; #     ,         (      )
                proxy_buffer_size 4k; #          proxy_buffers     
                proxy_buffers 4 64k; #                    
                proxy_busy_buffers_size 128k; #        (      proxy_buffers*2)
                proxy_temp_file_write_size 128k;     #         ,     ,  upstream    
           }
    }

    # HTTPS server
    server {
        listen       443 ssl;
        ssl on;
        server_name  www.test.com ;

        ssl_certificate      test.com_bundle.crt;   #  
        ssl_certificate_key  test.com.key;          #  

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

        ssl_ciphers 'AES128+EECDH:AES128+EDH';  #  ssl    
        ssl_prefer_server_ciphers  on;              #               
        ssl_protocols TLSv1.2 TLSv1.1 TLSv1;        #  ssl         

        location / {
            root   /var/www/wordpress;
            index  index.html index.htm index.php;
            #limit_conn addr 10; #  IP     10,  limit_conn_zone  
            #limit_req zone=one burst=5;    #  limit_req_zone         ,    5,    5       
        }

        location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$ {
            access_log off; #           
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
           #expires             3 ,          ,      ,                 
           expires      1d;
        }

        #              
        location ~ ^/p_w_picpaths/.*\.(php|php5|sh|pl|py)$ {
            deny all;
        }

        #    *.txt *.doc  
        location ~* \.(txt|doc)$ {
            if (-f $request_filename) {
                root /var/www/wordpress/files;
                #rewrite ...;   #        URL
                break;
            }
        }
        location ~* \.(txt|doc)$ {
            root /var/www/wordpress/files;
            deny all;
        }

        #        
        location /logs/ {
            deny all;
            #return 404;
        }
        location ~ ^/log/ {
            return 404;
        }
        location ~ ^/(local|log) {
            return 404;
        }

        #      IP  
        location ~ ^/test/ {
            deny 192.168.1.1;
            allow 192.168.1.0/24;
            allow 127.0.0.1;
            deny all;
        }

        #   
        location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
            valid_referers none blocked servernames *.test.com test.com;
            if ($invalid_referer) {
                rewrite ^/ http://www.test.com/img/nolink.jpg;
            }
            access_log off;
            root /var/www/wordpress/p_w_picpaths;
            expires 1d;
            break;
        }

        #      
        error_page   400 403 404 405 408 410 411 412 413 414 415 /4xx.html;    #    
        error_page   500 501 502 503 504  /50x.html;    #    

        location ~ .*\.(php|php5)?$ {
            root           /var/www/wordpress ;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;

            fastcgi_cache ngx_fcgi_cache;
            fastcgi_cache_valid 200 302 1h; #      200 302      1  
            fastcgi_cache_valid 301 1d; #      301      1 
            fastcgi_cache_valid any 1m; #         1  
            fastcgi_cache_min_uses 1;   #              ,1        
            fastcgi_cache_use_stale error timeout invalid_header http_500;  #              
            fastcgi_cache_key http://$host$request_uri;    #  :fast_cache_key $request_method://$host$request_uri;
                                                        #fast_cache_key http://$host$request_uri
                                                        #  fastcgi_cache key,       $request_method  cache key,
                                                        #         head  ,   GET      
        }
    }

#    #TCP       (nginx1.9.0     )
#    ######### TCP            ###############
#    upstream tcp_pro {
#        hash $remote_addr consistent;
#        server 10.0.0.14:40003 max_fails=3 fail_timeout=3s;
#        server 10.0.0.16:40003 max_fails=3 fail_timeout=3s;
#    }
#    ######### TCP            ###############
#
#    server {
#        listen 40003;
#        proxy_connect_timeout 2s;
#        proxy_timeout 2s;
#        proxy_pass tcp_pro;
#    }

}

좋은 웹페이지 즐겨찾기