nginx与php调优实战

以一台8核32G的服务器nginx.conf


user  www www;

worker_processes            8;
#nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计为8)。
#查看物理cpu个数,本机显示:8  grep 'physical id' /proc/cpuinfo | sort -u | wc -l
#查看每个cpu核心数量,本机显示:1  grep 'core id' /proc/cpuinfo | sort -u | wc -l
#查看线程总数,本机显示:8  grep 'processor' /proc/cpuinfo | sort -u | wc -l

worker_cpu_affinity         00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
#为每个进程分配cpu,上例中将8 个进程分配到8 个cpu,当然可以写多个,或者将一个进程分配到多个cpu。

error_log  logs/error.log crit;
pid        logs/nginx.pid;

worker_rlimit_nofile 51200;
#这个指令是指当一个nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文
#件数(ulimit -n)与nginx 进程数相除,但是nginx 分配请求并不是那么均匀,所以最好与ulimit -n 的值保持#一致。这里设置比实际设置较少一点

#现在在Linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。

#这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超#过10240了,这时会返回502错误。

events {
     # 语法  use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
     use epoll;      # 使用epoll(linux2.6的高性能方式)
     worker_connections 51200;         #每个进程最大连接数(最大连接=连接数×进程数)

     # 并发总数是 worker_processes 和 worker_connections 的乘积
     # 即 max_clients = worker_processes * worker_connections
     # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4
     # 并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
     # 查看系统可以打开的最大文件数
     # cat /proc/sys/fs/file-max
}

http{
        include  mime.types;
        default_type application/octet-stream;
        # charset utf-8;
        server_names_hash_bucket_size 128;
        #sendfile on;
        #tcp_nopush  on;
        #  server_name_in_redirect  off;
        server_tokens off ;
        keepalive_timeout 200;
#keepalive 超时时间。

        client_header_buffer_size    4k;
#客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,但也有client_header_buffer_size超过4k的情况,注意client_header_buffer_size该值必须设置为"系统分页大小"的整倍数。
#分页大小可以用命令 getconf PAGESIZE 取得。

        client_max_body_size         8m;
        large_client_header_buffers  4 16k;
        client_header_timeout  300;
        client_body_timeout    300;
        send_timeout           300;
        connection_pool_size        256;
        request_pool_size        4k;
        output_buffers   4 32k;
        postpone_output  1460;
        fastcgi_connect_timeout 400;
        fastcgi_send_timeout 400;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 256k;
        fastcgi_buffers 32 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_temp_path /dev/shm;
        fastcgi_intercept_errors on;
        #  limit_zone   website  $binary_remote_addr  100m;

        #  error_page   404  /404.html;
        #  error_page  403 500 502 503 504  /500.html;
        tcp_nodelay on;


   gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 8k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        #gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;

        gzip_types   text/plain application/x-javascript text/css  application/xml application/jpg text/javascript\;charset=UTF-8 text/css\;charset=UTF-8;

        log_format  lan_format  '$host $remote_addr - $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"';
#If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
    open_file_cache max=1000 inactive=20s;
#这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
    open_file_cache_valid 30s;
#这个是指多长时间检查一次缓存的有效信息。
    open_file_cache_min_uses 2;
#open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。
    open_file_cache_errors on;


include vhost/*.conf;

}

php-fpm.conf


;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]

pid = run/php-fpm.pid error_log = log/php-fpm.log log_level = warning emergency_restart_threshold = 800 emergency_restart_interval = 60s process_control_timeout = 5s daemonize = yes ;;;;;;;;;;;;;;;;;;;; ; Pool Definitions ; ;;;;;;;;;;;;;;;;;;;;

[www]

user = www group = www listen = 127.0.0.1:9000 pm = dynamic ;static静态方式下生效 pm.max_children = 800 ;动态方式下的起始php-fpm进程数量 pm.start_servers = 500 ;动态方式空闲状态下的最小php-fpm进程数量 pm.min_spare_servers = 300 ;动态方式空闲状态下的最大php-fpm进程数量 pm.max_spare_servers = 800 pm.max_requests = 2048 pm.process_idle_timeout = 10s request_terminate_timeout = 120 request_slowlog_timeout = 0 pm.status_path = /php-fpm_status slowlog = log/slow.log rlimit_files = 51200 rlimit_core = 0 catch_workers_output = yes env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp ;php_flag[display_errors] = on