nginx优化

此文章非原创,出自鸟哥之手~ 

http://blog.chinaunix.net/uid-25266990-id-2985541.html

 

改排版改得多,当然红色部分要注意下,用得较多

------------------------------------------------------------------------------ 分隔线

nginx优化(配置文件+内核优化)
 
一、nginx配置文件
1、worker_processes  8;
按照cpu数目来指定,一般为它的倍数
 
2、worker_cpu_affinity  00000001  00000010  00000100 00010000 00100000 01000000 10000000
为每个进程分配cpu,上例中将8个进程分配到8个cpu,也可以将1个进程分配到多个cpu
即进程绑定到核数 ,一般并不需要设置
 
3、worker_rlimit_nofile 102400;
一个nginx进程打开的最多文件描述符数目,
理论值 = 最多打开文件数(ulimit -n) / nginx 进程数
最好与 ulimit -n 保持一致
 
4、use epoll;
使用epoll的I/O模型
 
5、worker_connections  102400;
每个进程允许的最多连接数
理论上,每台nginx服务器的最大连接数 = worker_processes*worker_connections
 
6、keepalive_timeout 60;
keepalive 超时时间
即长连接,对于访问量很少的网站来说,打开没有很大意义
 
7、client_header_buffer_size 4k;
客户端请求头部的缓冲区大小,设置 为 系统分页大小(getconf PAGESIZE)
 
8、open_file_cache max=102400 inactive=20s;
打开文件指定缓存,默认是没有启用的
max:缓存数量,建议和打开文件数一致
inactive:经过多长时间文件没被请求后,删除缓存
 
9、open_file_cache_valid 30s;
30s检查一次缓存的有效信息
 
10、open_file_cache_min_uses 1;
open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,
文件描述符一直是在缓存中打开的,如上例,if有一个文件在inactive时间内一次没被使用,它将被移除
 
 
二、内核参数优化
1、net.ipv4.tcp_max_tw_buckets = 180000
timewait的数量,默认是180000
 
2、net.ipv4.ip_local_port_range = 1024 65000
允许系统打开的端口范围
 
3、net.ipv4.tcp_tw_recycle = 1
启用timewait快速回收
timewait:已经传输完数据还没断开的状态
统计timewait数量: netstat -an | grep -ic time_w
 
4、net.ipv4.tcp_tw_reuse = 1
开启重用。允许将TIME-WAIT sockets 重新用于新的TCP连接
timewait重新利用,不销毁
 
5、net.ipv4.tcp_syncookies = 1
开启SYN Cookies,当出现 SYN 等待队列溢出时,启用cookies来处理
预防SYN Flood攻击(DDos的一种方式),利用TCP/IP 的三次握手原理
客户端端发送SYN到服务端 ——》 服务端发ACK确认
 
6、net.core.somaxconn = 262144
web应用中 listen 函数的backlog默认会给我们内核参数的net.core.somaxconn限制到128,而nginx定义的NGX_LISTEN_BACKLOG 默认为 511, 所以有必要调整这个值。
 
7、net.core.netdev_max_backlog = 262144
每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。
 
8、net.ipv4.tcp_max_orphans = 262144
系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤儿连接将即刻被复位并打印出警告信息,这个限制仅仅是为了防止简单的DoS 攻击,不能过分依靠它或者人为地减少这个值,更应该增加这个值(if 增加内存)
 
9、net.ipv4.tcp_max_syn_backlog = 262144
记录的那些尚未收到客户端确认信息的连接请求的最大值。对应有128M内存的系统而言,缺省值是1024,小内存的系统则是 128
 
10、net.ipv4.tcp_timestamps = 0
时间戳可以避免序列号的卷绕。一个1Gbps的链路肯定会遇到以前用过的序列号。
时间戳能够让内核接受这种“异常”的数据包,这里需要将其关掉
 
11、net.ipv4.tcp_synack_retries = 1
为了打开对端的连接,内核需要发送一个SYN并附带一个回应前面一个SYN的ACK
也就是三次握手中的第二次握手
这个设置决定了内核放弃连接之前发送SYN+ACK 包的数量
 
12、net.ipv4.tcp_syn_retries  = 1
在内核放弃建立连接之前发送SYN包的数量
 
13、net.ipv4.tcp_fin_timeout = 1
如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2 状态的时间。对端可以出错并永远不关闭连接,甚至意外宕机。缺省值是60 秒。2.2 内核的通常值是180 秒,3你可以按这个设置,但要记住的是,即使你的机器是一个轻载的WEB 服务器,也有因为大量的死套接字而内存溢出的风险,FIN- WAIT-2 的危险性比FIN-WAIT-1 要小,因为它最多只能吃掉1.5K 内存,但是它们的生存期长些。
 
14、net.ipv4.tcp_keepalive_time = 30
当keepalive起用的时候,TCP发送keepalive消息的频度,缺省值是2小时
 
 
三、一个完整的内核优化设置
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
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 = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
 
 
四、一个简单的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 204800;

events
{
     use epoll;
     worker_connections 204800;
}

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 4k;
     fastcgi_buffers 8 4k;
     fastcgi_busy_buffers_size 8k;
     fastcgi_temp_file_write_size 8k;
     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 backup.aiju.com;
     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;
 }

 

 

 

posted @ 2016-12-30 21:25  windysai  阅读(271)  评论(0编辑  收藏  举报