The choice of communication mechanism
are Nginx and PHP-FPM using a TCP socket or a UNIX socket?
Reasonable configuration of Nginx processing request number
#cat /proc/cpuinfo | grep processor #查看服务器cpu的处理器数量# vi /etc/nginx/nginx.conf16; #修改为处理器数量events { 4096# 单个woker进程最大连接并发数 on; #linux2.6+默认epoll,如果使用了更优秀的kqueue模型,则使用默认off。}
Configuring NGINX+PHP-FPM Load Balancing
Single-machine capability is limited, for example, to support 1000 concurrent, generate two sock files, so that each PHP-FPM processing 500 units.
# nginx.confUpstream Backend {server unix:/dev/shm/php-fpm. Sock1 weight= -max_fails=5fail_timeout=5; Server unix:/dev/shm/php-fpm. Sock2 weight= -max_fails=5fail_timeout=5; }# php-fpm.conf (Similarly, PHP7 has introduced all configurations of POOL.D in the last line of the configuration file)# www1.confListen =/DEV/SHM/PHP-FPM. Sock1;Listen. Backlog= -1Listen. Allowed_clients =127.0. 0. 1Pm. Max_children = -Pm. Max_requests = theRlimit_files =50000Request_slowlog_timeout = -Sslowlog =/var/log/php-slow. Log# CP www1.conf WWW.CONF2Listen =/DEV/SHM/PHP-FPM. Sock2;
Disable access to log files
High-traffic sites involve a lot of I/O and must be synchronized between threads.
# nginx.confaccess_logofflog_not_foundofferror_log /var/log/nginx-errorwarn;
If log access cannot be turned off, set the buffer at least
access_log /var/log/nginx/access.log main buffer=16k;
Enable gzip
# nginx.conf gzip Span class= "Hljs-function_start" >on ; Gzip_disable "msie6" ; Gzip_vary on ; Gzip_proxied any; Gzip_comp_level 6 ; Gzip_min_length 1100 ; Gzip_buffers 16 8 K; Gzip_http_version 1.1 ; Gzip_types text /plain text /css application /json application /x-javascript text /xml application /xml Application /xml+rss text /javascript;
Cache frequently accessed files
# nginx.confmax=2000 inactive=20605; open_file_cache_errors off;
Adjust client Timeouts
# nginx.conf50115152215on; on; on;
Adjust the output buffer
# nginx.conf256161283120s; fastcgi_read120256256k; reset_timedout_connection on; server_names_hash100;
Adjust/etc/sysctl.conf
# Recycle Zombie connectionsNet. inet. TCP. Fast_finwait2_recycle=1Net. inet. TCP. MAXTCPTW=200000 # Increase number of filesKern. Maxfiles=65535Kern. Maxfilesperproc=16384 # Increase Page share factor per processVm. Pmap. PV_entry_max=54272521Vm. Pmap. Shpgperproc=20000 # Increase number of connectionsVfs. Vmiodirenable=1Kern. IPC. Somaxconn=3240000Net. inet. TCP. RFC1323=1Net. inet. TCP. Delayed_ack=0Net. inet. TCP. Restrict_rst=1Kern. IPC. Maxsockbuf=2097152Kern. IPC. Shmmax=268435456 # Host CacheNet. inet. TCP. Hostcache. Hashsize=4096Net. inet. TCP. Hostcache. CacheLimit=131072Net. inet. TCP. Hostcache. Bucketlimit= - # Increase number of portsNet. inet. IP. Portrange. First= -Net. inet. IP. Portrange. Last=100000Net. inet. IP. Portrange. Hifirst= -Net. inet. IP. Portrange. Hilast=100000Kern. IPC. SEMVMX=131068 # Disable Ping-flood attacksNet. inet. TCP. MSL= -Net. inet. ICMP. Bmcastecho=1Net. inet. ICMP. Icmplim=1Net. inet. TCP. Blackhole=2Net. inet. UDP. Blackhole=1
Nginx Condition Monitoring
Nginx in the Stub_status module is mainly used to view the Nginx some status information, the default does not compile into Nginx, recompile the installation of Nginx Stub_status module,
Continuously monitor the number of open connections, available memory, and number of waiting threads. Set up alerts to notify you when thresholds are exceeded. You can build these alerts yourself, or use them like serverdensity. Be sure to install Nginx stub_status module you need to recompile nginx-
./configure --prefix=/usr/local/nginx --with-http_stub_status_module make && make install
Add location to server block after installation
server{ location /nginx-status { on; }
After restarting Nginx, visit Www.x.com/nginx-status to see the returned information.
active connections – 活跃的连接数量server accepts handled requests — 总共处理了11989个连接 , 成功创建11989次握手, 总共处理了11991个请求reading — 读取客户端的连接数.writing — 响应数据到客户端的数量waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.
Reference
Nginx Basic Configuration Finishing
Nginx Core Functionality
Optimizing NGINX and PHP-FPM for high traffic sites
Enable the Nginx status State explanation
How to optimize the Nginx and php-fpm of high-flow site