Nginx Performance Optimization Author:HanjilongBack Home Introduction
In most cases, a regular installation of Nginx has worked well for the website. However, if you want to squeeze the performance of nginx, you need to know which instructions will affect nginx performance, in this article will explain which of nginx settings can be fine-tuned. It is important to note that this is a simple preview-an overview of those that can be tweaked to improve performance settings, which may not be the same for different environments.
For Nginx tuning, you can start with the following instructions:
1. worker_processes 2. worker_connections3. Buffers4. Timeouts5. Gzip Compression6. Static File Caching7. logging
1. worker_processes
worker_processes
Indicates the number of worker processes, the general situation is set to the number of CPU cores, a CPU configuration more than one worker number, no benefit to nginx, and do not forget the settings worker_cpu_affinity
, this configuration is used to bind the worker process to the specified CPU core, Reduce performance losses due to field rebuilds such as registers caused by multi-CPU core switching. grep Processor/proc/cpuinfo | Wc-l This command will tell you how many cores the current machine is, and the output is 2, which means 2 cores.
2. Worker_connections
worker_connections
Configure the number of concurrent connections that represent each worker process, with the default setting of 1024.
The following configuration file can be updated to modify the value: sudo vim/etc/nginx/nginx.conf
worker_processes 1;worker_connections 1024;
3. Buffers
Buffers: Another important parameter is buffer, if buffer is too small, nginx will continue to write some temporary files, which will cause the disk to continue to read and write, now we first understand some of the parameters set buffer: client_body_buffer_size
Maximum number of individual file bytes allowed to be requested by the client client_header_buffer_size
: The header buffer size used to set the client request, most of the 1KB size is sufficient client_max_body_size
: Set the file size that the client can upload, default to 1m large_client_header_buffers
: This directive is used to set the header buffer size for client requests
Refer to the configuration as follows:
client_body_buffer_size 10K;client_header_buffer_size 1k;client_max_body_size 8m;large_client_header_buffers 2 1k;
4. Timeouts
client_header_timeout
and client_body_timeout
set the request header and the request body (each) timeout time, if not send the request header and the request body, the Nginx server will return 408 error or request time out. keepalive_timeout
assigns a keep-alive link time-out to the client. The server will close the link after this timeout period, and we'll set it down to allow Nginx to continue working for a longer period of time. SEND_TIMEOUT Specifies the response time-out for the client. This setting is not used for the entire forwarder, but between two client read operations. If, during this time, the client does not read any data, Nginx closes the connection.
Refer to the configuration as follows:
client_body_timeout 12;client_header_timeout 12;keepalive_timeout 15;send_timeout 10;
5. Gzip Compression
Open Gzip,gzip can help nginx reduce a lot of network transmission work, in addition to pay attention to gzip_comp_level
the settings, too high, nginx service will waste CPU execution cycle.
Refer to the configuration as follows:
gzip on;gzip_comp_level 2;gzip_min_length 1000;gzip_proxied expired no-cache no-store private auth;gzip_types text/plain application/x-javascript text/xml text/css application/xml;
6. Static File Caching
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ { expires 365d;}
The above file types can be increased or decreased according to Nginx server matching.
7. Logging
access_log
Sets whether Nginx will store access logs. Turn off this option to make the read disk IO operation faster. You can modify the configuration file to turn this feature off:
access_log off;
Then restart the Nginx service:
sudo service nginx restart
tag: Nginx Linux
Nginx Tuning (excerpt from American Regiment)