We have been using Nginx with PHP for seven years. this experience has taught us how to optimize NGINX and PHP-fpm configurations for high-traffic sites.
The following are some tips and suggestions:
1. switch TCP to a UNIX domain socket
1. switch TCP to a UNIX domain socket
Compared with TCP sockets, UNIX-domain sockets provide better performance on the loopback interface (less data copying and context switching ).
But remember that only programs running on the same server can access UNIX domain sockets (obviously there is no network support ).
Upstream backend
{
# UNIX domain sockets
Server unix:/var/run/fastcgi. sock;
# TCP sockets
# Server 127.0.0.1: 8080;
}
2. adjust the number of working processes
Modern computer hardware is multi-processor, and NGINX can use multiple physical or virtual processors.
In most cases, your Web server is not configured to handle multiple tasks (for example, providing services as a Web server and printing server). you can configure NGINX to use all available processors, the NGINX workflow is not multi-threaded.
Run the following command to know how many processors your machine has:
On Linux
Cat/proc/cpuinfo | grep processor
FreeBSD
Sysctl dev. cpu | grep location
Set the work_processes value in the nginx. conf file to the number of processor cores of the machine.
At the same time, increase the value of worker_connections (how many connections can be processed by each processor core) and set "multi_accept" to ON. if you are using Linux, you can also use "epoll ":
# We have 16 cores
Worker_processes 16;
# Connections per worker
Events
{
Worker_connections 4096;
Multi_accept on;
}
3. set upstream server load balancer
In our experience, multiple upstream backends on the same machine can bring higher throughput than a single upstream backend.
For example, if you want to support a maximum of 1000 PHP-fpm sub-processes (children), you can evenly allocate the number to two upstream backends and process 500 PHP-fpm sub-processes respectively:
Upstream backend {
Server unix:/var/run/php5-fpm.sock1 weight = 100 max_fails = 5 fail_timeout = 5;
Server unix:/var/run/php5-fpm.sock2 weight = 100 max_fails = 5 fail_timeout = 5;
}
4. disable access log files
This affects a lot because log files on high-traffic sites involve a large number of IO operations that must be synchronized across all threads.
Access_log off;
Log_not_found off;
Error_log/var/log/nginx-error.log warn;
If you cannot close access log files, at least use the buffer:
Access_log/var/log/nginx/access. log main buffer = 16 k;
5. enable GZip
Gzip 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 + rss text/javascript;
6. cache file-related information frequently accessed
Open_file_cache max = 200000 inactive = 20 s;
Open_file_cache_valid 30 s;
Open_file_cache_min_uses 2;
Open_file_cache_errors on;
7. adjust the client timeout time
Client_max_body_size 500 M;
Client_body_buffer_size 1 m;
Client_body_timeout 15;
Client_header_timeout 15;
Keepalive_timeout 2 2;
Send_timeout 15;
Sendfile on;
Tcp_nopush on;
Tcp_nodelay on;
8. adjust the output buffer size
Fastcgi_buffers 256 16 k;
Fastcgi_buffer_size 128 k;
Fastcgi_connect_timeout 3 s;
Fastcgi_send_timeout 120 s;
Fastcgi_read_timeout 120 s;
Reset_timedout_connection on;
Server_names_hash_bucket_size 100;
9./etc/sysctl. conf optimization
# Recycle Zombie connections
Net. inet. tcp. fast_finwait2_recycle = 1
Net. inet. tcp. maxtcptw = 200000
# Increase number of files
Kern. maxfiles = 65535
Kern. maxfilesperproc = 16384
# Increase page share factor per process
Vm. pmap. pv_entry_max = 54272521
Vm. pmap. shpgperproc = 20000
# Increase number of connections
Vfs. vmiodirenable = 1
Kern. ipc. somaxconn = 3240000
Net. inet. tcp. rfc1323 = 1
Net. inet. tcp. delayed_ack = 0
Net. inet. tcp. restrict_rst = 1
Kern. ipc. maxsockbuf = 2097152
Kern. ipc. shmmax = 268435456
# Host cache
Net. inet. tcp. hostcache. hashsize = 4096
Net. inet. tcp. hostcache. cachelimit = 131072
Net. inet. tcp. hostcache. bucketlimit = 120
# Increase number of ports
Net. inet. ip. portrange. first = 2000
Net. inet. ip. portrange. last = 100000
Net. inet. ip. portrange. hifirst = 2000
Net. inet. ip. portrange. hilast = 100000
Kern. ipc. semvmx= 131068
# Disable Ping-flood attacks
Net. inet. tcp. msl = 2000
Net. inet. icmp. bmcastecho = 1
Net. inet. icmp. icmplim = 1
Net. inet. tcp. blackhole = 2
Net. inet. udp. blackhole = 1
10. monitoring
Continuously monitors the number of opened connections, idle memory, and number of waiting threads.
You are notified when an alert is set to exceed the threshold. You can build these alarms on your own, or use something similar to ServerDensity.
Confirm that the stub_status module of NGINX is installed. This module is not compiled into NGINX by default, so you may need to recompile NGINX
./Configure -- with-http_ssl_module -- with-http_stub_status_module -- without-mail_pop3_module
-- Without-mail_imap_module -- without-mail_smtp_module
Make install BATCH = yes