Home > Backend Development > PHP Tutorial > Nginx related configuration optimization

Nginx related configuration optimization

WBOY
Release: 2016-07-29 09:15:18
Original
851 people have browsed it

Most Nginx installation guides tell you the following basics - install via apt-get, modify a few lines of configuration here or there, and voila, you already have a web server. And, in most cases, a regular installation of nginx will work just fine for your website. However, if you really want to squeeze the performance out of Nginx, you have to dig a little deeper. In this guide, I will explain those settings of Nginx that can be fine-tuned to optimize performance when handling large numbers of clients. It’s important to note that this is not a comprehensive guide to fine-tuning. This is a simple preview - an overview of settings that can be fine-tuned to improve performance. Your situation may be different.

Basic (Optimized) Configuration

The only file we will modify is nginx.conf, which contains all the settings for the different modules of Nginx. You should be able to find nginx.conf in the server's /etc/nginx directory. First, we'll talk about some global settings, and then we'll go through each module in the file and talk about which settings will give you good performance when a large number of clients access it, and why they will improve performance. There is a complete configuration file at the end of this article.

High-level configuration

In the nginx.conf file, there are a few advanced configurations in Nginx above the module part.

<ol>
<li>user www-data; </li>
<li>pid /var/run/nginx.pid; </li>
<li><span>worker_processes auto; </span></li>
<li><span>worker_rlimit_nofile 100000; </span></li>
</ol>
Copy after login

user and pid should be as default - we won't change these as it makes no difference whether they are changed or not.

worker_processes defines the number of worker processes when nginx provides web services to the outside world. The optimal value depends on many factors, including (but not limited to) the number of CPU cores, the number of hard drives storing data, and load patterns. When in doubt, setting it to the number of available CPU cores would be a good start (setting to "auto" will try to detect it automatically).

worker_rlimit_nofile Change the maximum number of open files limit for the worker process. If not set, this value is the operating system limit. After setting, your operating system and Nginx can handle more files than "ulimit -a", so set this value high so that nginx will not have "too many open files" problems.

Events module

The events module contains all settings for handling connections in nginx.

<ol>
<li>events { </li>
<li><span>worker_connections 2048; </span></li>
<li>multi_accept on; </li>
<li><span>use epoll; </span></li>
<li>} </li>
</ol>
Copy after login

worker_connections Set the maximum number of connections that can be opened simultaneously by a worker process. If worker_rlimit_nofile mentioned above is set, we can set this value very high.

Remember that the maximum number of clients is also limited by the number of available socket connections on the system (~64K), so there is no benefit in setting it unrealistically high.

multi_accept tells nginx to accept as many connections as possible after receiving a new connection notification.

use Set the polling method for reusing client threads. If you use Linux 2.6+, you should use epoll. If you use *BSD, you should use kqueue.

(It’s worth noting that if you don’t know which polling method Nginx should use, it will choose the one that best suits your operating system)

HTTP module

The HTTP module controls all core features of nginx http processing. Because there is only a small amount of configuration here, we only excerpt a small part of the configuration. All these settings should be in the http module, and you won't even notice this setting in particular.

<ol>
<li>http { </li>
<li>server_tokens off; </li>
<li>sendfile on; </li>
<li>tcp_nopush on; </li>
<li>tcp_nodelay on; </li>
<li>... </li>
<li>} </li>
</ol>
Copy after login

server_tokens does not make nginx execute faster, but it can turn off the nginx version number in the error page, which is good for security.

sendfile allows sendfile() to work. sendfile() can copy data (or any two file descriptors) between disk and TCP socket. Pre-sendfile is to apply for a data buffer in user space before transmitting data. Then use read() to copy the data from the file to this buffer, and write() to write the buffer data to the network. sendfile() immediately reads the data from the disk to the OS cache. Because this copy is done in the kernel, sendfile() is more efficient than combining read() and write() and turning the discard buffer on and off (more on sendfile).

tcp_nopush tells nginx to send all header files in one packet instead of sending them one after another.

tcp_nodelay tells nginx not to cache the data, but to send it piece by piece - when data needs to be sent in time, this attribute should be set to the application, so that when sending a small piece of data information, the return value cannot be obtained immediately.

<ol>
<li>access_log off; </li>
<li>error_log /var/log/nginx/error.log crit; </li>
</ol>
Copy after login

access_log Set whether nginx will store access logs. Turning this option off can make read disk IO operations faster (aka, YOLO)

error_log tells nginx to only log serious errors:

<ol>
<li>keepalive_timeout 10; </li>
<li>client_header_timeout 10; </li>
<li>client_body_timeout 10; </li>
<li>reset_timedout_connection on; </li>
<li>send_timeout 10; </li>
</ol>
Copy after login

keepalive_timeout Assigns a keep-alive link timeout to the client. The server will close the link after this timeout. We set it lower to allow ngnix to continue working longer.

client_header_timeout and client_body_timeout set the timeout for the request header and request body (each). We can also set this lower.

reset_timeout_connection tells nginx to close unresponsive client connections. This will free up the memory space occupied by that client.

send_timeout 指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。

<ol>
<li>limit_conn_zone $binary_remote_addr <span>zone</span>=<span>addr</span>:5m; </li>
<li>limit_conn addr 100; </li>
</ol>
Copy after login

limit_conn_zone 设置用于保存各种key(比如当前连接数)的共享内存的参数。5m就是5兆字节,这个值应该被设置的足够大以存储(32K*5)32byte状态或者(16K*5)64byte状态。

limit_conn 为给定的key设置最大连接数。这里key是addr,我们设置的值是100,也就是说我们允许每一个IP地址最多同时打开有100个连接。

<ol>
<li>include /etc/nginx/mime.types; </li>
<li>default_type text/html; </li>
<li>charset UTF-8; </li>
</ol>
Copy after login

include 只是一个在当前文件中包含另一个文件内容的指令。这里我们使用它来加载稍后会用到的一系列的MIME类型。

default_type 设置文件使用的默认的MIME-type。

charset 设置我们的头文件中的默认的字符集

<ol>
<li>gzip on; </li>
<li>gzip_disable "msie6"; </li>
<li># gzip_static on; </li>
<li>gzip_proxied any; </li>
<li>gzip_min_length 1000; </li>
<li>gzip_comp_level 4; </li>
<li>gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; </li>
</ol>
Copy after login

gzip 是告诉nginx采用gzip压缩的形式发送数据。这将会减少我们发送的数据量。

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 设置需要压缩的数据格式。上面例子中已经有一些了,你也可以再添加更多的格式。

<ol>
<li># cache informations about file descriptors, frequently accessed files </li>
<li># can boost performance, but you need to test those values </li>
<li>open_file_cache <span>max</span>=<span>100000</span> <span>inactive</span>=<span>20s</span>; </li>
<li>open_file_cache_valid 30s; </li>
<li>open_file_cache_min_uses 2; </li>
<li>open_file_cache_errors on; </li>
<li>## </li>
<li># Virtual Host Configs </li>
<li># aka our settings for specific servers </li>
<li>## </li>
<li>include /etc/nginx/conf.d/*.conf; </li>
<li>include /etc/nginx/sites-enabled/*; </li>
</ol>
Copy after login

open_file_cache 打开缓存的同时也指定了缓存最大数目,以及缓存的时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。

open_file_cache_valid 在open_file_cache中指定检测正确信息的间隔时间。

open_file_cache_min_uses 定义了open_file_cache中指令参数不活动时间期间里最小的文件数。

open_file_cache_errors 指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。

一个完整的配置

<ol>
<li>user www-data; </li>
<li>pid /var/run/nginx.pid; </li>
<li>worker_processes auto; </li>
<li>worker_rlimit_nofile 100000; </li>
<li>events { </li>
<li>worker_connections 2048; </li>
<li>multi_accept on; </li>
<li>use epoll; </li>
<li>} </li>
<li>http { </li>
<li>server_tokens off; </li>
<li>sendfile on; </li>
<li>tcp_nopush on; </li>
<li>tcp_nodelay on; </li>
<li>access_log off; </li>
<li>error_log /var/log/nginx/error.log crit; </li>
<li>keepalive_timeout 10; </li>
<li>client_header_timeout 10; </li>
<li>client_body_timeout 10; </li>
<li>reset_timedout_connection on; </li>
<li>send_timeout 10; </li>
<li>limit_conn_zone $binary_remote_addr <span>zone</span>=<span>addr</span>:5m; </li>
<li>limit_conn addr 100; </li>
<li>include /etc/nginx/mime.types; </li>
<li>default_type text/html; </li>
<li>charset UTF-8; </li>
<li>gzip on; </li>
<li>gzip_disable "msie6"; </li>
<li>gzip_proxied any; </li>
<li>gzip_min_length 1000; </li>
<li>gzip_comp_level 6; </li>
<li>gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; </li>
<li>open_file_cache <span>max</span>=<span>100000</span> <span>inactive</span>=<span>20s</span>; </li>
<li>open_file_cache_valid 30s; </li>
<li>open_file_cache_min_uses 2; </li>
<li>open_file_cache_errors on; </li>
<li>include /etc/nginx/conf.d/*.conf; </li>
<li>include /etc/nginx/sites-enabled/*; </li>
<li>} </li>
</ol>
Copy after login

编辑完配置后,确认重启nginx使设置生效。

<ol><li>sudo service nginx restart</li></ol>
Copy after login

以上就介绍了Nginx 相关配置优化,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template