This article mainly introduces the Nginx management configuration optimization, has a certain reference value, now share to everyone, the need for friends can refer to
Nginx optimization
Optimization ideas
Basic optimization
1, adjust the parameters hidden Nginx software version number information
Server_tokens off;
2. Change Nginx Service Default User
User Nginx Nginx;
3. The number of worker processes to optimize Nginx service
Worker_processes cpu* nuclear number;
4. Optimize the binding of different nginx processes to different CPUs
Worker_cpu_affinit 0001 0010 0100 1000 0001 0010 0100 1000;
5, Nginx Time processing model optimization
Use Epoll
6, adjust the maximum number of single process connections
Worker_connections 20480;
7. The maximum number of open files in nginx work process
Worker_rlimit_nofile 65535;
8. Open efficient File transfer mode
Sendfile on;
The efficient transfer mode for opening files, while setting the Tcp_nopush and tcp_nodelay two instructions to on, can prevent network and disk I/O blocking and improve nginx productivity.
9, optimize the connection timeout time
Keepalive_timeout 0; Disconnect the TCP connection immediately after the response is ended.
10, fastcgi optimization
11. Configuring Nginx gzip compression for performance optimization
Gzip on;gzip_min_length 1k;gzip_buffers 4 32k;gzip_comp_level 9;gzip_vary on;
12. Configuring Nginx expires cache for performance optimization
Nginx expires 3650d;
13, Nginx log related optimization and security
14. Restrict program and file access according to extension name.
Dell All;allow All;
15, anti-theft chain
HTTP Referer
System-Level optimization
1. System level Optimization
echo 500000 >/proc/sys/net/core/somaxconn //Set maximum number of connections echo 1 >/proc/sys/net/ipv4/tcp_tw_recycle // Accelerate TCP recovery echo 1 >/proc/sys/net/ipv4/tcp_tw_reuse //null TCP allow recycle echo 0 >/proc/sys/net/ipv4/tcp_syncookies //Turn off flood protection
2, NGINX&PHP-FPM optimized configuration
Nginx forwarded the request to PHP-FPM processing, when the Nginx forwards many requests, the PHP-FPM process is not enough, the child process is generated.
However, generating sub-processes requires the kernel to dispatch, which is time consuming.
If the Web site is larger, you can generate multiple child processes in a static manner and keep them in memory.
Modify Phjp-fpm.conf
Pm=static//Let the PHP-FPM process persist, without the need to dynamically generate pm.max_children=32 number of child processes that are always maintained
3, Php&mysql optimization
Kernel level: Increase the number of connections and accelerate TCP recovery
MySQL level: Increase the number of connections
PHP Level: Use long connections to save the number of connections
Use memory cache ( memcached
) to relieve MySQL pressure
Attention
The memory cache is suitable for caching complex SQL queries. Because php
memcached
A TCP connection is also established, a simple SQL query does not need to be cached.