Nginx 최적화 전체 편

Nginx 최적화
1. Nginx 설정 의 최적화
1、work_processes 2; //Nginx   ,  n cpu   cpu  *2
2、worker_cpu_affinity 01 10; //       cpu,          cpu
3、worker_rlimit_nofile 65535; //  Nginx              。   (ulimit -n  )
4、use epoll; //  epoll I/O  ,              
5、woker_connections 65535; //            
6、keepalive_timeout 60; //http    ,   60s,                          ,             ,         。
7、client_header_buffer_size 4k; //             ,                 ,               1k,              1k,           。         getconf PAGESIZE  。
8、server_tokens off;//              web server(Nginx)      ,            。
9、sendfile on; //   sendfile()    。sendfile()      TCP socket        (          )。Pre-sendfile                   。   read()              ,write()          。sendfile()           OS  。             ,sendfile()    read() write()              
10、tcp_nopush on; //  nginx              ,          。              ,        ,        ,           。
11、tcp_nodelay on;//  nginx      ,         --          ,            ,                      。
12、open_file_cache max=65535 inactive=60s; //         ,        ,max       ,          ,inactive                    
13、open_file_cache_min_uses 1; //open_file_cache     inactive               ,        ,               ,   ,        inactive          ,     
14、open_file_cache_valid 80s; //                 

2. FastCGI 의 몇 가지 명령
1、fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;//     FastCGI        ,      ,                 
2、fastcgi_connect_timeout 300;//       FastCGI     。
3、fastcgi_send_timeout 300;// FastCGI         ,               FastCGI         。
4、fastcgi_read_timeout 300;//  FastCGI       ,                FastCGI       。
5、fastcgi_buffer_size 16k;//    FastCGI                ,       fastcgi_buffers          ,           1  16k              ,    ,               (    1k),      fastcgi_buffers             ,         fastcgi_buffers           。
6、fastcgi_buffers 16 16k;//                    FastCGI   ,    ,    php           256k,      16 16k       ,    256k,    256k       fastcgi_temp      ,                   ,                ,                     php              ,                     256k          16 16k,  4 64k   64 4k,    ,            ,           32k,   4 64k    1 64k       ,     64 4k    8 4k       ,     16 16k     2 16k     ,           。
7、fastcgi_busy_buffers_size 32k;//    fastcgi_buffers   。
8、fastcgi_temp_file_write_size 32k;//   fastcgi_temp_path         ,    fastcgi_buffers   。
9、fastcgi_cache TEST; //  FastCGI            。            ,      CPU  ,    502  。             ,           。             。
10、fastcgi_cache_valid 200 302 1h;
        fastcgi_cache_valid 301 1d;
        fastcgi_cache_valid any 1m;//              ,     200,302       ,301    1 ,   1  。
11、fastcgi_cache_min_uses 1;//   fastcgi_cache_path  inactive             ,   ,   5      1       ,          。

3. Linux 시스템 커 널 최적화 / etc / sysctl. conf
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000  //timewait   ,   180000。(Deven:      timewait      tcp_max_tw_buckets   )
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 1            // net.ipv4.tcp_tw_recycle   1   ,        
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1           //         TIME-WAIT  ,  NAT             tcp    ,  。
net.ipv4.tcp_tw_reuse = 1 //      ,   TIME-WAIT   sockets      TCP  。          ,       !
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 30 //            FIN-WAIT-2     
net.ipv4.tcp_keepalive_time = 30 // keepalive     ,TCP  keepalive     。   2  。
net.ipv4.ip_local_port_range = 1024 65000 //         
net.ipv4.ip_conntrack_max = 6553500

4. Nginx 안전 관련 설정
1、  autoindex  。         Nginx        ,              location    autoindex off;    。
2、       ssi (      )。       location    ssi off; 。
3、       。      (     )                    。 server_tokens off;     Nginx           。
4、                     ***    。
client_body_buffer_size  1K;
client_header_buffer_size 1k;
client_max_body_size 1k;
large_client_header_buffers 2 1k;
5、 timeout     DOS***。                 。
client_body_timeout   10;
client_header_timeout 10;
keepalive_timeout     65;
send_timeout          10;
6、          DOS***。
limit_zone slimits $binary_remote_addr 5m;
limit_conn slimits 5;
7、      HTTP  。HTTP      crypt,        。         MD5(              crypt ) 。

5. 간단 한 Nginx 프로필 첨부
user   www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;
error_log   /www/log/nginx_error.log   crit;
pid         /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events
{
   use epoll;
   worker_connections 65535;
}
http
{
   include       mime.types;
   default_type   application/octet-stream;
   charset   utf-8;
   server_names_hash_bucket_size 128;
   client_header_buffer_size 2k;
   large_client_header_buffers 4 4k;
   client_max_body_size 8m;
   sendfile on;
   tcp_nopush     on; 
   keepalive_timeout 60;
   fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
   keys_zone=TEST:10m
   inactive=5m;
   fastcgi_connect_timeout 300;
   fastcgi_send_timeout 300;
   fastcgi_read_timeout 300;
   fastcgi_buffer_size 16k;
   fastcgi_buffers 16 16k;
   fastcgi_busy_buffers_size 16k;
   fastcgi_temp_file_write_size 16k;
   fastcgi_cache TEST;
   fastcgi_cache_valid 200 302 1h;
   fastcgi_cache_valid 301 1d;
   fastcgi_cache_valid any 1m;
   fastcgi_cache_min_uses 1;
   fastcgi_cache_use_stale error timeout invalid_header http_500;  
   open_file_cache max=204800 inactive=20s;
   open_file_cache_min_uses 1;
   open_file_cache_valid 30s;  
   tcp_nodelay on;

   gzip on;
   gzip_min_length   1k;
   gzip_buffers     4 16k;
   gzip_http_version 1.0;
   gzip_comp_level 2;
   gzip_types       text/plain application/x-javascript text/css application/xml;
   gzip_vary on;
server
   {
     listen       8080;
     server_name   *********;
     index index.php index.htm;
     root   /www/html/;
location /status
     {
         stub_status on;
     }
location ~ .*\.(php|php5)?$
     {
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         include fcgi.conf;
     }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
     {
       expires       30d;
     }
                log_format   access   '$remote_addr - $remote_user [$time_local] "$request" '
               '$status $body_bytes_sent "$http_referer" '
               '"$http_user_agent" $http_x_forwarded_for';
                 access_log   /www/log/access.log   access;
       }
}

좋은 웹페이지 즐겨찾기