日期:2014-05-17  浏览次数:20362 次

Nginx&&PHP-FPM配置及优化指南(上)

本文介绍在Centos5.8/6.2&&RedHat(RHEL) 5.8/6.2下LEMP/LNMP环境下的Nginx&&PHP-FPM的WEB服务器配置及优化指南。

截至目前,各软件版本为

  • Nginx 1.2.2
  • PHP && PHP-FPM5.4.4 

如果您还没有搭建LEMP环境,可以参照我之前写过一篇文章 LEMP(或LNMP)高性能的WEB服务器在CentOS6.2/5.8下的Yum搭建流程。在"LEMP搭建指南"中我只给出了Nginx&&PHP-FPM最基本的配置说明。

在本文中将更深入的介绍Nginx&&PHP-FPM的WEB服务器配置。

Nginx 配置文件也可以参考:http://wiki.nginx.org/NginxChs

Nginx&&PHP-FPM配置及优化指南(上)

Nginx的配置文件

Nginx的配置文件放在/etc/nginx路径之下,运行ls -l /etc/nginx 输出

total 36
drwxr-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详细说明

#运行用户
user  nginx;
#进程数目,通常设置成和cpu的数量相等
worker_processes  1;

#全局错误日志
error_log  /var/log/nginx/error.log warn;
#PID文件
pid        /var/run/nginx.pid;

#工作模式及连接数上限
events {
    #单个worker_process进程的最大并发链接数
    worker_connections  1024;
}

#设定http服务器,利用它的反向代理功能还可以提供负载均衡支持
http {

    #设定mime类型,类型由mime.type文件定义
    include       /etc/nginx/mime.types;

    #制定默认MIME类型为二进制字节流
    default_type  application/octet-stream;

    #日志格式,参考URL
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    #日志存放路径
    access_log  /var/log/nginx/access.log  main;

    #开启调用Linux的sendfile(),提供文件传输效率
    #sendfile一般设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime
    sendfile        on;

    #是否允许使用socket的TCP_NOPUSH或TCP_CORK选项
    #tcp_nopush     on;

    #指定客户端连接保持活动的超时时间,在这个时间之后,服务器会关掉连接
    keepalive_timeout  65;

    #设置开启gzip压缩,参考URL
    #gzip  on;

    #虚拟主机配置文件引入
    include /etc/nginx/conf.d/*.conf;
}

主配置文件nginx.conf参数优化要点

Nginx&&PHP-FPM配置及优化指南(上)
1. worker_processes及 worker_connections配置

默认配置中worker_processes及 worker_connections的数目有点小,只能应付1000次/秒以内的请求。

#默认配置
worker_processes  1;
worker_connections  1024;

通常情况下,worker_processes设置为cpu数目,worker_connections保持1024即可。你可以使用cat /proc/cpuinfo |grep processor来查看CPU数量

2. 隐藏Ngnix版本信息

server_tokens off;

3. 拒绝web访问系统隐藏文件

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

4. 限制最大文件上传大小 

client_max_body_size 20m;
client_body_buffer_size 128k;

5. Nginx静态文件缓存控制

浏览器缓存非常有利于节省带宽,在Nginx中非常容易配置

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


6. Ngnix转发PHP请求至PHP-FPM

# Pass PHP scripts to PHP-FPM
location ~* \.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.