使用PHP5.5.x可大幅度提高Webserver负载能力,详细看这里
max_clients = worker_processes * worker_connections
worker_processes 1; worker_connections 1024;
worker_processes
的数值一般设置为内核数
$ cat /proc/cpuinfo |grep processor processor : 0 processor : 1 processor : 2 processor : 3 processor : 4 processor : 5 processor : 6 processor : 7
根据查到的内核数,这里我们配置worker_processes
为8
worker_processes 8;
server_tokens
在Nginx配置文件的http/server/location部分设置。
server_tokens off;
client_max_body_size
的默认值是1m,如果你的网站需要上传大于1m的文件,设置client_max_body_size为你需要的值。否则,在你上传超过client_max_body_size的文件时,Nginx会返回给你413错误,告诉你“Request Entity Too Large” 。client_max_body_size
在Nginx配置文件的http/server/location部分设置。
client_max_body_size 20m; client_body_buffer_size 128k;
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { access_log off; log_not_found off; expires 360d; }
gzip_proxied any; gzip_types text/css text/plain text/javascript application/javascript application/json application/x-javascript application/xml application/xml+rss application/xhtml+xml application/x-font-ttf application/x-font-opentype application/vnd.ms-fontobject image/svg+xml image/x-icon application/rss+xml application/atom_xml; gzip_comp_level 9; gzip_http_version 1.0; gzip_vary on; gzip_buffers 16 8k; gzip_min_length 50;
location / { try_files $uri $uri/ /index.php$uri?$args; } location ~* \.php { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_split_path_info ^(.+\.php)(/?.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
location ~ /\. { access_log off; log_not_found off; deny all; }
emergency_restart_threshold 10 emergency_restart_interval 1m process_control_timeout 10s
pm.max_children
是PHP-FPM Pool 最大的子进程数,他数值取决于你的服务器内存。
假设你打算给1G内存给当前配置的PHP-FPM Pool,一般一个PHP请求占用内存10M-40M,我们按站点每个PHP请求占用内存25M,这样max_children = 1G/25M = 40.96。
这样,我设置pm.max_children为40. max_requests是每个子进程重生之前处理的请求数, 默认值为unlimited,设置为2000主要是为了避免内存泄露带来的问题
pm = dynamic pm.max_children = 40 pm.start_servers = 15 pm.min_spare_servers = 10 pm.max_spare_servers = 20 pm.max_requests = 2000
建议不同站点使用不同的PHP-FPM Pool,绑定在不同的端口。这样一个站点出问题,不至于影响其他的站点。同时,如果php-fpm进程占用CPU过高,也可以很快定位有问题的站点。
下边配置了两个PHP-FPM Pool,绑定了9000和9001两个http端口
/etc/php-fpm.d/site.conf
[site] listen = 127.0.0.1:9000 user = site group = site request_slowlog_timeout = 5s slowlog = /var/log/php-fpm/slowlog-site.log listen.allowed_clients = 127.0.0.1 pm = dynamic pm.max_children = 5 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 4 pm.max_requests = 200 listen.backlog = -1 pm.status_path = /status request_terminate_timeout = 120s rlimit_files = 131072 rlimit_core = unlimited catch_workers_output = yes env[HOSTNAME] = $HOSTNAME env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
/etc/php-fpm.d/blog.conf
[blog] listen = 127.0.0.1:9001 user = blog group = blog request_slowlog_timeout = 5s slowlog = /var/log/php-fpm/slowlog-blog.log listen.allowed_clients = 127.0.0.1 pm = dynamic pm.max_children = 4 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 pm.max_requests = 200 listen.backlog = -1 pm.status_path = /status request_terminate_timeout = 120s rlimit_files = 131072 rlimit_core = unlimited catch_workers_output = yes env[HOSTNAME] = $HOSTNAME env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
这段代码放在 /etc/php-fpm.conf 中,加载多个php-fpm pool
include=/etc/php-fpm.d/*.conf
php-fpm.conf配置文件要启用以下选项:
pm.status_path = /status
在Nginx配置的server部分增加以下配置:
location ~ ^/(status)$ { access_log off; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
访问http://localhost/status 查看php-fpm状态