Advanced configuration of Nginx server
Because the actual environments in which Nginx servers run and the functions they provide vary greatly, more customized configurations can be made based on the hardware and software environments.
Kernel optimization for IPv4
Change /sbin/sysctl effective /sbin/sysctl -p 1.net.core.netdev_max_backlog This parameter indicates the maximum number of packets that can be sent to the queue for each network interface if the speed at which the network interface receives packets is faster than the kernel can process the data for these packets. The general default is 128. The NGX_LISITEN_BACKLOG defined in the Nginx server defaults to 511, so adjust it appropriately.
net.core.netdev_max_backlog =262144
Copy the code
2.net.core.somaxconn This parameter is used to adjust the number of TCP connections initiated by the system. The default value is 128. In the case of high concurrent requests on the client, this default value is small and may cause link timeout or retransmission problems.
net.core.somaxconn=262144
Copy the code
3.net.ipv4.tcp_max_orphans This parameter is used to set the maximum number of TCP sockets allowed in the system that are not associated with any user file handles. Generally, when the system has sufficient memory, you can increase this value appropriately.
net.ipv4.tcp_max_orphans=262144
Copy the code
4.net.ipv4.tcp_max_syn_backlog This parameter records the maximum number of connection requests that have not received confirmation from the client. The default is 1024 for systems with 128MB of memory and 128 for systems with small memory. Generally, when the system has sufficient memory, you can increase this value appropriately.
net.ipv4.tcp_max_syn_backlog=262144
Copy the code
5.net.ipv4.tcp_timestamps This parameter is used to set timestamps to avoid sequence number winding. By default, the TCP protocol allows the kernel to receive such “abnormal” packets. For Nginx servers, it is recommended to turn them off:
net.ipv4.tcp_timestamps=0
Copy the code
6.net.ipv4.tcp_synack_retries Sets the number of SYN+ACK packets that the kernel sends to the client before disallowing the TCP connection. Usually set to 1, that is, the kernel sends a SYN+ACK packet before abandoning the connection
net.ipv4.tcp_synack_retries=1
Copy the code
7.net.ipv4.tcp_syn_retries Sets the number of SYN packets that the kernel sends before disallowing the connection
net.ipv4.tcp_syn_retries=1
Copy the code
Nginx configuration optimization for CPUS
In the events block of the nginx.conf configuration file 1. Worker_processes This directive sets the number of nginx service processes. (Usually a few cores for a few threads)
Take a quad-core CPU server as an example
worker_processes 4;
Copy the code
2. Worker_cpu_affinity This directive is used to allocate CPU working cores for each process. (Cores make groups of digits)
# quad-core worker_CPU_affinity 0001 0010 0100 1000 # eight-core worker_CPU_affinity 00000001 00000010 00000100 0000100000010000 00100000 01000000 10000000Copy the code
Configurations related to network connections
1. Keepalived_timeout The keepalived_timeout command is used to set the timeout period for the Nginx server to keep the connection with the client
The keep-alive field timeout in the reply header sent to the client is set to 100s keepalive_timeout 120s 100s.Copy the code
2. The send_timeout command is used to set the timeout period for the Nginx server to respond to the client
send_timeout 10s;
Copy the code
Client_header_buffer_size This directive is used to set the buffer size of client request headers allowed by the home Nginx server. The default is 1KB. Allowing the Nginx server to receive larger request headers improves server support for clients.
client_header_buffer_size 4k;
Copy the code
4. Multi_accept is used to configure whether the Nginx server can receive multiple network connections from clients. The default value is off.
multi_accept on;
Copy the code
Configurations related to event-driven models
1. The use use directive specifies the event-driven model used by the Nginx server.
use select;
Copy the code
2. Worker_connections Specifies the maximum number of simultaneous connections allowed by each worker process on the Nginx server.
worker_connections 1024;
Copy the code
Maximum number of clients that the Nginx server can connect to at the same time: Max _Client=worker_processes * worker_connections/2. As a reverse proxy server: max_Client=worker_processes * worker_connections/4.
- Why divide by 2: This formula is based on the HTTP 1.1 protocol. Most browsers send two connections for one request, not two threads for a request and a response.
- Why divide by 4: Since nginx acts as a directional proxy, the client establishes a connection with Nginx, and nginx establishes a connection with the back-end server
3. Worker_rlimit_sigpending This command is used to set the upper limit of the event signal queue length on the Linux platform. To ensure efficient processing of client requests by the Nginx server, this value is set according to the number of concurrent requests from the client and the processing capacity of the server running environment.
worker_rlimit_sigpending 1024;
Copy the code
4. The devpoll_changes and devpoll_events directives are used to set the number of events that can be passed between the Nginx server and the kernel in /dev/poll event-driven mode. Devpoll_changes: Number of events passed to the kernel (default: 32) devpoll_events: 5. The kqueue_CHANGES and kqueue_events directives set the number of events that can be passed between the Nginx server and the kernel in kqueue event-driven mode. Kqueue_changes: Number of events passed to the kernel (default: 512) Epoll_events directives These two directives set the number of events that can be passed between the Nginx server and the kernel in epoll event-driven mode. Kqueue_events: Number of events passed to the kernel (default: 512)