Nginx & PHP-FPM configuration and Optimization Guide (II)

Source: Internet
Author: User
Tags sendfile
Nginx & amp; PHP-FPM configuration and Optimization Guide (I) This article introduces in Centos5.86.2 & amp; RedHat (RHEL) & nbsp; 5.86.2 LEMPLNMP environment Nginx & amp; PHP-FPM WEB server configuration and Optimization Guide. Up to now, the software version is Nginx1.2.2PHP & amp; P Nginx & PHP-FPM configuration and Optimization Guide (on)

This article introduces the WEB server configuration and Optimization Guide for Nginx & PHP-FPM in LEMP/LNMP environment under Centos5.8/6.2 & RedHat (RHEL) 5.8/6.2.

Up to now, each software version is

  • Nginx 1.2.2
  • PHP & PHP-FPM5.4.4

If you have not set up a LEMP environment, refer to the Yum building process of a LEMP (or LNMP) high-performance WEB server under CentOS 5.8. In the "LEMP build Guide", I only gave the Nginx & PHP-FPM basic configuration instructions.

This article gives a more in-depth introduction to Nginx & PHP-FPM WEB server configuration.

Nginx configuration file can also refer to: http://wiki.nginx.org/NginxChs

Nginx & PHP-FPM configuration and Optimization Guide (I)

Nginx configuration file

Put the Nginx configuration file under the/etc/nginx path and run ls-l/etc/nginx output

total 36drwxr-xr-x. 2 root root 4096 Jul 11 19:52 conf.d-rw-r--r--. 1 root root  964 Jul  3 19:53 fastcgi_params-rw-r--r--. 1 root root 2837 Jul  3 19:53 koi-utf-rw-r--r--. 1 root root 2223 Jul  3 19:53 koi-win-rw-r--r--. 1 root root 3463 Jul  3 19:53 mime.types-rw-r--r--. 1 root root  643 Jul  3 19:50 nginx.conf-rw-r--r--. 1 root root  596 Jul  3 19:53 scgi_params-rw-r--r--. 1 root root  623 Jul  3 19:53 uwsgi_params-rw-r--r--. 1 root root 3610 Jul  3 19:53 win-utf

Nginx. conf

# Run user nginx; # Number of processes, which are usually set to the same number as the cpu: worker_processes 1; # global error log error_log/var/log/nginx/error. log warn; # PID/var/run/nginx. pid; # working mode and maximum number of connections events {# maximum number of concurrent connections for a single worker_process process worker_connections 1024;} # set the http server, with its reverse proxy function, server load balancer supports http {# set the mime type, which is determined by mime. the type file defines include/etc/nginx/mime. types; # set the default MIME type to binary byte stream default_type application/octet-stream; # log format, refer to the URL log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ status $ response" $ http_referer "'' "$ http_user_agent" "$ http_x_forwarded_for "'; # log storage path access_log/var/log/nginx/access. log main; # Enable and call Linux's sendfile () to provide file transfer efficiency # sendfile is generally set to on. if it is used for downloading and other application disk I/O heavy load applications, you can set it to off, to balance the disk and network I/O processing speed and reduce the system's uptime sendfile on; # whether to allow the use of socket TCP_NOPUSH or TCP_CORK option # tcp_nopush on; # specify the timeout time for client connection persistence. after this time, the server will turn off keepalive_timeout 65; # set to enable gzip compression, refer to URL # gzip on; # introduce include/etc/nginx/conf to the VM configuration file. d /*. conf ;}

Key points for optimizing nginx. conf parameters in the main configuration file

Nginx & PHP-FPM configuration and Optimization Guide (I)

1. worker_processes and worker_connections configuration

In the default configuration, the number of worker_processes and worker_connections is small, and only 1000 requests/second are allowed.

# Worker_processes 1; worker_connections 1024 by default;

Generally, worker_processes is set to the number of CPUs, and worker_connections is set to 1024. You can use cat/proc/cpuinfo | grep processor to view the number of CPUs

2. hide Ngnix version information

server_tokens off;

3. deny web access to system hidden files

location ~ /\. {    access_log off;    log_not_found off;     deny all;}

4. restrict the maximum file upload size

client_max_body_size 20m;client_body_buffer_size 128k;

5. Nginx static file cache control

Browser cache is very helpful for saving bandwidth and easy to configure in Nginx

location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {    access_log        off;    log_not_found     off;    expires           360d;}


6. Ngnix forward PHP requests to PHP-FPM

# Pass PHP scripts to PHP-FPMlocation ~* \.php$ {    try_files       $uri /index.php;    fastcgi_index   index.php;    fastcgi_pass    127.0.0.1:9000;    #fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;    include         fastcgi_params;    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;}
7. enable GZIP compression

gzip on;gzip_min_length  1k;gzip_buffers     4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types       text/plain application/x-javascript text/cssapplication/xml;gzip_vary on;

**************************************** ****
* Author: Ye Wentao
* Link to this article: Nginx & PHP-FPM configuration and Optimization Guide (I)
* ***************** For reprinting, please indicate the source ***************


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.