High-traffic site NGINX and PHP-fpm Configuration Optimization)

Source: Internet
Author: User
Tags unix domain socket
Original article: OptimizingNGINXandPHP-fpmforhightrafficsites Translator: youngsterxyf has been using Nginx with PHP for seven years. This article shows us how to optimize NGINX and PHP-fpm configurations for high-traffic sites. The following are some tips and suggestions for this: 1. Switch TCP to a UNIX-domain socket.

Original article: Optimizing NGINX and PHP-fpm for high traffic sites Translator: the seven-year experience of youngsterxyf using Nginx with PHP has taught us how to optimize NGINX and PHP-fpm configurations for high-traffic sites. The following are some tips and suggestions for this: 1. Switch TCP to a UNIX-domain socket.

Original article: Optimizing NGINX and PHP-fpm for high traffic sites

Translator: youngsterxyf

With over seven years of experience using Nginx with PHP, we have learned 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

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 coresworker_processes 16;# connections per workerevents{    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;}

The following are two process pools from php-fpm.conf:

     
  
   www1
      
  
   /var/run/php5-fpm.sock1
      
          
   
    -1
           
           
           
   
    0666
       
      
  
   www
      
  
   www
      
          
   
    static
           
   
    500
       
      
  
   50000
      
  
   0
      
  
   20s
      
  
   /var/log/php-slow.log
      
      
      
  
   no
      
  
   5000
      
  
   127.0.0.1
      
          
   
    $HOSTNAME
           
   
    /usr/local/bin:/usr/bin:/bin
           
   
    /usr/tmp
           
   
    /usr/tmp
           
   
    /usr/tmp
           
   
    $OSTYPE
           
   
    $MACHTYPE
           
   
    2
       
  
 
     
  
   www2
      
  
   /var/run/php5-fpm.sock2
      
          
   
    -1
           
           
           
   
    0666
       
      
  
   www
      
  
   www
      
          
   
    static
           
   
    500
       
      
  
   50000
      
  
   0
      
  
   20s
      
  
   /var/log/php-slow.log
      
      
      
  
   no
      
  
   5000
      
  
   127.0.0.1
      
          
   
    $HOSTNAME
           
   
    /usr/local/bin:/usr/bin:/bin
           
   
    /usr/tmp
           
   
    /usr/tmp
           
   
    /usr/tmp
           
   
    $OSTYPE
           
   
    $MACHTYPE
           
   
    2
       
  
 

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=16k;

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 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;

6. cache file-related information frequently accessed

open_file_cache max=200000 inactive=20s;open_file_cache_valid 30s;open_file_cache_min_uses 2;open_file_cache_errors on;

7. Adjust the client timeout time

client_max_body_size 500M;client_body_buffer_size 1m;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 16k;fastcgi_buffer_size 128k;fastcgi_connect_timeout 3s;fastcgi_send_timeout 120s;fastcgi_read_timeout 120s;reset_timedout_connection on;server_names_hash_bucket_size 100;

9./etc/sysctl. conf Optimization

# 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=120# Increase number of portsnet.inet.ip.portrange.first=2000net.inet.ip.portrange.last=100000net.inet.ip.portrange.hifirst=2000net.inet.ip.portrange.hilast=100000kern.ipc.semvmx=131068# Disable Ping-flood attacksnet.inet.tcp.msl=2000net.inet.icmp.bmcastecho=1net.inet.icmp.icmplim=1net.inet.tcp.blackhole=2net.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_modulemake install BATCH=yes

Original article address: high-traffic site NGINX and PHP-fpm configuration optimization (translation), thanks to the original author for sharing.

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.