Nginx Server Configuration performance optimization Scheme _nginx

Source: Internet
Author: User
Tags epoll json sendfile nginx server

High-level configuration
in the nginx.conf file, a few of the advanced configurations in Nginx are above the module section.
User Www-data;
Pid/var/run/nginx.pid;
worker_processes Auto;
Worker_rlimit_nofile 100000;

The user and PID should be set by default-we do not change the content because the change is no different.

worker_processes defines the number of worker processes that are nginx to provide Web services externally. The optimal value depends on many factors, including (but not limited to) the number of CPU cores, the number of hard disks that store the data, and the load pattern. When unsure, setting it to the available number of CPU cores will be a good start (set to "Auto" will attempt to automatically detect it).

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

Events Module
The events module contains the settings for all processing connections in the Nginx.
Events {
worker_connections 2048;
multi_accept on;
Use epoll;
}

worker_connections Sets the maximum number of connections that can be opened by one worker process at the same time. If you set the Worker_rlimit_nofile mentioned above, we can set this value very high.
Remember, the maximum number of customers is also limited by the number of available socket connections (~ 64K), so it is no good setting unrealistic heights.

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

Use sets the polling method used to reuse 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 one that works best for your operating system.)

HTTP Module

The HTTP module controls all the core features of Nginx HTTP processing. Because there are only a few configurations here, we only extract a small portion of the configuration. All of these settings should be in the HTTP module, and you will not even notice this setting specifically.

HTTP { 
server_tokens off; 
Sendfile on; 
Tcp_nopush on; 
Tcp_nodelay on; 
... 
} 

Server_tokens does not allow Nginx to perform faster, but it can turn off the Nginx version number on the error page, which is good for security.
Sendfile can make sendfile () play a role. Sendfile () can copy data (or any two file descriptors) between a disk and a TCP socket. Pre-sendfile is to request data buffers in user space before transferring data. The data is then copied from the file to the buffer using read (), and write () writes the buffer data to the network.

Sendfile () is to immediately read data from disk to the OS cache. Because this copy is done in the kernel, sendfile () is more effective than combining read () and write () and turning off the discard buffer (more about sendfile).

Tcp_nopush tells Nginx to send all header files in a packet, not one after another.

Tcp_nodelay told Nginx not to cache data, but a paragraph of the send-when the need to send data in time, you should set this property to the application, so send a small piece of data information can not immediately get the return value.

Access_log off; 
Error_log/var/log/nginx/error.log Crit; 

Access_log Set nginx whether the access log will be stored. Turn off this option to make the read disk IO operation faster (Aka,yolo)
Error_log told Nginx to record only serious errors:

Keepalive_timeout; 
Client_header_timeout; 
Client_body_timeout; 
Reset_timedout_connection on; 
Send_timeout 10; 

keepalive_timeout assigns keep-alive link timeout time to the client. The server closes the link after this timeout period. We set it down so that Ngnix can continue to work longer hours.

client_header_timeout and client_body_timeout Set the timeout for the request header and the request body (respective). We can also set this down a little bit.

reset_timeout_connection tells Nginx to close the unresponsive client connection. This will release the memory space that the client occupies.

send_timeout Specifies the response timeout for the client. This setting is not used for the entire forwarder, but between the two client read operations. If the client does not read any data during this period, Nginx closes the connection.

Limit_conn_zone $binary _remote_addr zone=addr:5m; 
Limit_conn addr 100; 

limit_conn_zone Sets the parameters for saving shared memory for various keys, such as the current number of connections. 5m is 5 megabytes, this value should be set large enough to store (32k*5) 32byte status or (16k*5) 64byte state.

limit_conn Sets the maximum number of connections for a given key. Here key is addr, we set a value of 100, which means we allow each IP address to open at most simultaneous 100 connections.

Include/etc/nginx/mime.types; 
Default_type text/html; 
CharSet UTF-8; 

include is simply a directive that contains the contents of another file in the current file. Here we use it to load a series of MIME types that will be used later.

Default_type The default mime-type used by the settings file.

CharSet Set the default character set in our header file

gzip on; 
Gzip_disable "Msie6"; 
# gzip_static on; 
Gzip_proxied any; 
Gzip_min_length 1000; 
Gzip_comp_level 4; 
Gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml Application/xml+rss Text/javascript; 

gzip is to tell Nginx to send data in the form of gzip compression. This will reduce the amount of data we send.

gzip_disable Disables the gzip feature for the specified client. We are set to IE6 or lower versions to make our solutions broadly compatible.

gzip_static tells Nginx to find out if there are any resources that have been previously gzip processed before compressing the resource. This requires you to compress your files in advance (commented out in this example), allowing you to use the highest compression ratio so that nginx will not have to compress the files (please click here for more detailed gzip_static information).

gzip_proxied allows or disables compression of response streams based on requests and responses. We set to any, which means that all requests will be compressed.

gzip_min_length Sets the minimum number of bytes to enable compression for data. If a request is less than 1000 bytes, we'd better not compress it, because compressing these small data lowers the speed of all processes that handle this request.

Gzip_comp_level Sets the compression level of the data. This level can be any number between 1-9, and 9 is the slowest but the maximum compression ratio. We set it to 4, which is a more eclectic setting.

Gzip_type Sets the data format that you want to compress. There are already some in the example above, and you can add more formatting.

# cache informations about file descriptors, frequently accessed files 
# can boost performance, but you need to test T Hose values 
open_file_cache max=100000 inactive=20s; 
Open_file_cache_valid 30s; 
Open_file_cache_min_uses 2; 
Open_file_cache_errors on; 
# # 
Virtual Host configs 
# aka our settings for specific servers 
# 
include/etc/nginx/conf.d/*.conf;< c11/>include/etc/nginx/sites-enabled/*; 

Open_file_cache Opens the cache and also specifies the maximum number of caches and the time of the cache. We can set a relatively high maximum time so that we can clear them out after they are inactive for more than 20 seconds.
open_file_cache_valid Specifies the interval of time to detect the correct information in Open_file_cache.
open_file_cache_min_uses defines the minimum number of files in the Open_file_cache period during which the instruction parameter is inactive.
open_file_cache_errors Specifies whether to cache error messages when searching for a file, including adding files to the configuration again. We also include server modules, which are defined in different files. If your server module is not in these locations, you will have to modify this line to specify the correct location.
A complete configuration

User Www-data; 
Pid/var/run/nginx.pid; 
Worker_processes Auto; 
Worker_rlimit_nofile 100000; 
Events {Worker_connections 2048; 
Multi_accept on; 
Use Epoll; 
HTTP {server_tokens off; 
Sendfile on; 
Tcp_nopush on; 
Tcp_nodelay on; 
Access_log off; 
Error_log/var/log/nginx/error.log Crit; 
Keepalive_timeout 10; 
Client_header_timeout 10; 
Client_body_timeout 10; 
Reset_timedout_connection on; 
Send_timeout 10; 
Limit_conn_zone $binary _remote_addr zone=addr:5m; 
Limit_conn addr 100; 
Include/etc/nginx/mime.types; 
Default_type text/html; 
CharSet UTF-8; 
gzip on; 
Gzip_disable "Msie6"; 
Gzip_proxied any; 
Gzip_min_length 1000; 
Gzip_comp_level 6; Gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml Application/xml+rss 
Text/javascript; 
Open_file_cache max=100000 inactive=20s; 
Open_file_cache_valid 30s; 
Open_file_cache_min_uses 2; 
Open_file_cache_errors on; 
include/etc/nginx/conf.d/*.conf; 
include/etc/nginx/sites-enabled/*; } 
 

After you finish editing the configuration, confirm that the reboot Nginx makes the settings effective.
sudo service nginx restart

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.