本文介绍了nginx优化、LINUX内核优化、linux连接数优化、nginx连接数优化的相关内容 在大多数情况下,一个常规安装的nginx对你的网站来说已经能很好地工作了。然而,如果你真的想挤压出nginx的性能,你必须更深入一些。在本指南中,我将解释Nginx的那些设置可以微调,以优化处理大量客户端时的性能。需要注意一点,这不是一个全面的微调指南。这是一个简单的预览——那些可以通过微调来提高性能设置的概述。你的情况可能不同。
基本的 (优化过的)配置
Top Level Configs
1 user www-data; worker_processes defines the number of worker processes that nginx should use when serving your website. The optimal value depends on many factors including (but not limited to) the number of CPU cores, the number of hard drives that store data, and load pattern. When in doubt, setting it to the number of available CPU cores would be a good start (the value “auto” will try to autodetect it). worker_rlimit_nofile changes the limit on the maximum number of open files for worker processes. If this isn’t set, your OS will limit. Chances are your OS and nginx can handle more than “ulimit -a” will report, so we’ll set this high so nginx will never have an issue with “too many open files”
译者信息
1 user www-data; worker_processes 定义了nginx对外提供web服务时的worder进程数。最优值取决于许多因素,包括(但不限于)CPU核的数量、存储数据的硬盘数量及负载模式。不能确定的时候,将其设置为可用的CPU内核数将是一个好的开始(设置为“auto”将尝试自动检测它)。 worker_rlimit_nofile 更改worker进程的最大打开文件数限制。如果没设置的话,这个值为操作系统的限制。设置后你的操作系统和Nginx可以处理比“ulimit -a”更多的文件,所以把这个值设高,这样nginx就不会有“too many open files”问题了。
Events Module
1 events { Keep in mind that the maximum number of clients is also limited by the number of socket connections available on your sytem (~64k), so setting this ridiculously high won’t benefit us. multi_accept tells nginx to accept as many connections as possible after getting a notification about a new connection use sets which polling method we should use for multiplexing clients on to threads. If you’re using Linux 2.6+, you should use epoll. If you’re using *BSD, you should use kqueue. Wanna know more about event polling?Let Wikipedia be your guide (warning, a neckbeard and an operating systems course might be needed to understand everything) (it’s worth noting if you don’t which polling method nginx should use, it’ll chose the best one for your OS)
译者信息
1 events { 记住,最大客户数也由系统的可用socket连接数限制(~ 64K),所以设置不切实际的高没什么好处。 multi_accept 告诉nginx收到一个新连接通知后接受尽可能多的连接。 use 设置用于复用客户端线程的轮询方法。如果你使用Linux 2.6+,你应该使用epoll。如果你使用*BSD,你应该使用kqueue。想知道更多有关事件轮询?看下维基百科吧(注意,想了解一切的话可能需要neckbeard和操作系统的课程基础) (值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的)
HTTP Module
01 http { sendfile enables the use of sendfile(). sendfile() copies data between the disk and a TCP socket (or any two file descriptors). Pre-sendfile, to transfer such data we would alloc a data buffer in the user space. We would then read() to copy the data from a file in to the buffer, and write() the content of the buffer to a network. sendfile() reads the data immediately from the disk into the OS cache. Because this copying is done within the kernel, sendfile() is more efficient than the combination of read() and write() and the context switching/cache trashing that comes along with it (read more about sendfile)
译者信息
01 http { tcp_nopush tells nginx to send all header files in one packet as opposed to one by one tcp_nodelay tells nginx not to buffer data and send data in small, short bursts - it should only be set for applications that send frequent small bursts of information without getting an immediate response, where timely delivery of data is required
1 access_log off; error_log tells nginx it should only log critical errors
1 keepalive_timeout 10; client_header_timeout and client_body_timeout sets the timeout for the request header and request body (respectively). We’ll set this low too.
译者信息 tcp_nodelay 告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。
1 access_log off;
1 keepalive_timeout 10; reset_timedout_connection tells nginx to close connection on non responding client. This will free up all memory associated with that client. send_timeout specifies the response timeout to the client. This timeout does not apply to the entire transfer, but between two subsequent client-read operations. If the client has not read any data for this amount of time, then nginx shuts down the connection.
1 limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn sets the maximum allowed number of connections for a given key value. The key is addr, and our value is 100, so we’ll only allow 100 concurrent connections per IP address.
1 include /etc/nginx/mime.types; default_type sets the default MIME-type to be used for files charset sets the default charset to be included in our header
译者信息 send_timeout 指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。
1 limit_conn_zone $binary_remote_addr zone=addr:5m;
1 include /etc/nginx/mime.types; charset设置我们的头文件中的默认的字符集 The performance improvement these two options give is explained in this great WebMasters StackExchange question.
1 gzip on; gzip_static tells nginx to look for the pre-gzip’d asset with the same name before gzipping the asset itself. This is requires you to pre-zip your files (it’s commented out for this example), but allows you to use the highest compression possible and nginx no longer has to zip those files (read more about gzip_statichere) gzip_proxied allows or disallows compression of a response based on the request/response. We’ll set it to any, so we gzip all requests. gzip_min_length sets the minimum number of bytes necessary for us to gzip data. If a request is under 1000 bytes, we won’t bother gzipping it, since gzipping does slow down the overall process of handling a request. gzip_comp_level sets the compression level on our data. These levesls can be anywhere from 1-9, 9 being the slowest but most compressed. We’ll set it to 4, which is a good middle ground. gzip_types sets the type of data to gzip. There are some above, but you can add more.
01 # cache informations about file descriptors, frequently accessed files
1 gzip on; gzip_disable为指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。 gzip_static告诉nginx在压缩资源之前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),从而允许你使用最高压缩比,这样nginx就不用再压缩这些文件了(想要更详尽的gzip_static的信息,请点击这里)。 gzip_proxied允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求。 gzip_min_length设置对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。 gzip_comp_level设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置。 gzip_type设置需要压缩的数据格式。上面例子中已经有一些了,你也可以再添加更多的格式。
01 # cache informations about file descriptors, frequently accessed files open_file_cache_min_uses defines the minimum use number of a file within the time specified in the directive parameter inactive in open_file_cache open_file_cache_errors specifies whether or not to cache errors when searching for a file Include is again used to add some files to our config. We’re including our server modules, defined in a different file. If your server modules aren’t at these locations, you should modify this line to point at the correct location.
译者信息 open_file_cache_valid 在open_file_cache中指定检测正确信息的间隔时间。 open_file_cache_min_uses 定义了open_file_cache中指令参数不活动时间期间里最小的文件数。 open_file_cache_errors指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。
The full config file
Takeaway
译者信息
复制代码代码示例:
01 user www-data;
02 pid /var/run/nginx.pid; 03 worker_processes auto; 04 worker_rlimit_nofile 100000; 05 06 events { 07 worker_connections 2048; 08 multi_accept on; 09 use epoll; 10 } 11 12 http { 13 server_tokens off; 14 sendfile on; 15 tcp_nopush on; 16 tcp_nodelay on; 17 18 access_log off; 19 error_log /var/log/nginx/error.log crit; 20 21 keepalive_timeout 10; 22 client_header_timeout 10; 23 client_body_timeout 10; 24 reset_timedout_connection on; 25 send_timeout 10; 26 27 limit_conn_zone $binary_remote_addr zone=addr:5m; 28 limit_conn addr 100; 29 30 include /etc/nginx/mime.types; 31 default_type text/html; 32 charset UTF-8; 33 34 gzipon; 35 gzip_disable"msie6"; 36 gzip_proxied any; 37 gzip_min_length 1000; 38 gzip_comp_level 6; 39 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 40 41 open_file_cache max=100000 inactive=20s; 42 open_file_cache_valid 30s; 43 open_file_cache_min_uses 2; 44 open_file_cache_errors on; 45 46 include /etc/nginx/conf.d/*.conf; 47 include /etc/nginx/sites-enabled/*; 48 } 编辑完配置后,确认重启nginx使设置生效。 sudo service nginx restart
后记
笔者对NGINX的理会: 1. worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计为8)。 2. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
为每个进程分配cpu,上例中将8 个进程分配到8 个cpu,当然可以写多个,或者将一 3. worker_rlimit_nofile 65535;
这个指令是指当一个nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文 现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。 这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。 查看linux系统文件描述符的方法: [root@web001 ~]# sysctl -a | grep fs.file fs.file-max = 789972 fs.file-nr = 510 0 789972
4. use epoll; ( 补充说明: 与apache相类,nginx针对不同的操作系统,有不同的事件模型
A)标准事件模型
/dev/poll:使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。 ) 5. worker_connections 65535; 每个进程允许的最多连接数, 理论上每台nginx 服务器的最大连接数为worker_processes*worker_connections。 6. keepalive_timeout 60; keepalive 超时时间。 7. client_header_buffer_size 4k; 客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。 分页大小可以用命令getconf PAGESIZE 取得。 [root@web001 ~]# getconf PAGESIZE 4096 但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。 8. open_file_cache max=65535 inactive=60s; 这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。 9. open_file_cache_valid 80s; 这个是指多长时间检查一次缓存的有效信息。 10. open_file_cache_min_uses 1; open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。
关于内核参数的优化:
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_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30 一个完整的内核优化设置:
vi /etc/sysctl.conf CentOS5.5中可以将所有内容清空直接替换为如下内容:
复制代码代码示例:
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
使配置立即生效可使用如下命令:
关于系统连接数的优化 问题描述: 说明 server 只允许同时打开 1024 个文件,处理 1024 个用户进程 使用ulimit -a 可以查看当前系统的所有限制值,使用ulimit -n 可以查看当前的最大打开文件数。 新装的linux 默认只有1024 ,当作负载较大的服务器时,很容易遇到error: too many open files 。因此,需要将其改大。 解决方法: 使用 ulimit –n 65535 可即时修改,但重启后就无效了。(注ulimit -SHn 65535 等效 ulimit -n 65535 ,-S 指soft ,-H 指hard)
有如下三种修改方式:
具体使用哪种,在 CentOS 中使用第1 种方式无效果,使用第3 种方式有效果,而在Debian 中使用第2 种有效果 备注:ulimit 命令本身就有分软硬设置,加-H 就是硬,加-S 就是软默认显示的是软限制 soft 限制指的是当前系统生效的设置值。 hard 限制值可以被普通用户降低。但是不能增加。 soft 限制不能设置的比 hard 限制更高。 只有 root 用户才能够增加 hard 限制值。
关于FastCGI 的几个指令:
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_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
<value name="rlimit_files">102400</value>
<value name="max_requests">204800</value> |