How to optimize several aspects of nginx and php-fpm of high flow site

Source: Internet
Author: User
Tags fpm
the choice of communication mechanism

Nginx and PHP-FPM are using either a TCP socket or a UNIX socket. reasonable Configuration Nginx processing request number

#cat/proc/cpuinfo | grep processor #查看服务器cpu的处理器数量
# vi/etc/nginx/nginx.conf
worker_processes;  #修改为处理器数量
Events { 
  worker_connections 4096; # single woker process maximum connection concurrency number 
  multi_accept on;  #linux2.6+ default Epoll, use the default off if a better kqueue model is used.
}
Configure NGINX+PHP-FPM Load Balancing

Stand-alone capacity is limited, such as to support 1000 concurrent, generate two sock files, so that each PHP-FPM processing 500 units.

# nginx.conf
Upstream backend { 
  server unix:/dev/shm/php-fpm.sock1 weight=100 max_fails=5; 
  Server Unix:/dev/shm/php-fpm.sock2 weight=100 max_fails=5 fail_timeout=5; 
}


# php-fpm.conf (for the same reason, PHP7 introduced all the POOL.D configurations at the end of the configuration file)
# www1.conf
listen =/dev/shm/php-fpm.sock1;
Listen.backlog =-1  
listen.allowed_clients = 127.0.0.1

Pm.max_children =
pm.max_requests = 5000

Rlimit_files = 50000
request_slowlog_timeout = 20s
Slowlog =/var/log/php-slow.log

# cp www1.conf Www.conf2
listen =/dev/shm/php-fpm.sock2;
prohibit access to log files

High-flow sites involve large amounts of I/O and must be synchronized between threads.

# nginx.conf
access_log off; 
Log_not_found off; 
Error_log/var/log/nginx-error.log warn;

If log access cannot be turned off, at least set buffering

Access_log/var/log/nginx/access.log main buffer=16k;
Enable gzip
# nginx.conf
gzip on; 
Gzip_disable "Msie6"; 
Gzip_vary on; 
Gzip_proxied any; 
Gzip_comp_level 6; 
Gzip_min_length 1100; 
Gzip_buffers 8k; 
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.conf
open_file_cache max=2000 inactive=20s; 
Open_file_cache_valid 60s; 
Open_file_cache_min_uses 5; 
Open_file_cache_errors off;
Adjust Client Timeout
# nginx.conf
client_max_body_size 50M; 
Client_body_buffer_size 1m; 
Client_body_timeout; 
Client_header_timeout; 
Keepalive_timeout 2 2; 
Send_timeout; 
Sendfile on; 
Tcp_nopush on; 
Tcp_nodelay on;
Adjust output buffers
# nginx.conf
fastcgi_buffers 256 16k; 
Fastcgi_buffer_size 128k; 
Fastcgi_connect_timeout 3s; 
Fastcgi_send_timeout 120s; 
Fastcgi_read_timeout 120s; 
Fastcgi_busy_buffers_size 256k; 
Fastcgi_temp_file_write_size 256k; 
Reset_timedout_connection on; 
Server_names_hash_bucket_size 100;
Adjust/etc/sysctl.conf
# Recycle Zombie connections net.inet.tcp.fast_finwait2_recycle=1 net.inet.tcp.maxtcptw=200000 # Increase Number of fi Les kern.maxfiles=65535 kern.maxfilesperproc=16384 # Increase page share factor per process vm.pmap.pv_entry_max=5427 2521 vm.pmap.shpgperproc=20000 # Increase number of connections vfs.vmiodirenable=1 kern.ipc.somaxconn=3240000 net.i 
Net.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.ine t.tcp.blackhole=2 net.inet.udp.blackhole=1
Nginx State Monitoring

The Stub_status module in Nginx is mainly used to view some state information of Nginx, the default does not compile into Nginx, recompile installation nginx Stub_status module,

Continuously monitor the number of open connections, the available memory, and the number of wait threads. Set up alerts to notify you when thresholds are exceeded. You can build these alerts yourself, or use like serverdensity. Be sure to install the 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 {  
             stub_status on;  
        }  

When you restart Nginx, you can access www.x.com/nginx-status to see the information returned

Active connections– the number of actively connected
servers accepts handled requests-handled 11,989 connections, successfully created 11,989 handshakes, and handled 11,991 requests in total
Readin G-Read the number of connections to the client.
writing-response data to the number of clients
waiting-open keep-alive, this value equals active– (reading+writing), meaning that Nginx has finished processing the resident connection waiting for the next request instruction .

Reference
Nginx basic configuration Collation
Nginx Core functionality
Optimizing Nginx and PHP-FPM for high traffic sites
Enable Nginx Status of the state detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.