我查看了许多其他 Server Fault 和 Google 搜索结果,它们都谈到了大致类似的情况,但我找不到我想要的见解。我很感激社区提供的任何帮助,也不想让任何人认为我在寻求帮助之前没有做过详尽的研究。我非常感谢任何花时间回复并教育我的人。
情况如下:
我有一台 Digital Ocean 服务器,配备 8 核处理器、16 GB RAM 和 160 GB SSD。该服务器在 Ubuntu 14.04.2 LTS 上的 nginx + php5-fpm 上运行 WordPress。
我以前一直使用 Apache,所以这是我第一次尝试 nginx。
无论如何,该网站运行良好但我偶尔会收到 502 bad gateway 错误,并且有报告称用户也会间歇性地遇到此问题。我使用 CloudFlare 进行了相当积极的边缘缓存,以限制对实际原始服务器的命中次数。
nginx 错误日志显示了一个常见问题:
2015/07/03 17:38:51 [error] 4283#0: *74154 connect() to unix:/var/run/php5-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: 173.245.xx.xx, server: www.mydomain.com, request: "GET /path/to/file HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:
我发现这些条目与 502 场景相对应,但我无法弄清楚如何有效调整 php5-fpm 和 nginx 以支持流量。
top
显示 %Cpu(s): 在我监控的几个不同时间内在 27% 到 54% 之间波动。
内存使用情况对我来说似乎也很好;以下是输出free -h
:
total used free shared buffers cached
Mem: 15G 15G 248M 71M 1.3G 11G
-/+ buffers/cache: 2.3G 13G
Swap: 0B 0B 0B
最后,转到 nginx 配置,根据我读到的内容,我将 worker_processes 设置为 8,worker_connections 为 16192。如下所示/etc/nginx/nginx.conf
:
user www-data;
worker_processes 8;
pid /run/nginx.pid;
events {
worker_connections 16192;
multi_accept on;
use epoll;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
以下是配置/etc/nginx/sites-available/default
fastcgi_cache_key "$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
listen 127.0.0.1 default_server;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name my.website.com;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
最后说明:这台机器上也运行着虚拟主机,每个虚拟主机都有类似的站点配置和 fastcgi 缓存,并且都运行着 WordPress。
根据 @pennywise 的建议,为其中一个 vhost 添加配置文件。我将我的评论保留在行内。
fastcgi_cache_path /etc/nginx/cache/vhostdomain levels=1:2 keys_zone=vhostdomain:100m inactive=60m;
server {
listen 80;
listen [::]:80;
root /usr/share/nginx/www.vhostdomain.com;
index index.php index.html index.htm;
server_name www.vhostdomain.com;
server_namevhostdomain.com;
access_log /var/log/nginx/vhostdomain.com.access.log;
error_log /var/log/nginx/vhostdomain.com.error.log;
set $skip_cache 0;
# POST requests should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
# If the URL contains the query strings params cb or clk, bypass the cache
if ($query_string ~* "cb=|clk=") {
set $skip_cache 1;
}
# If the URL contains the /ad/ directory, bypass cache
if ($request_uri ~* "/ad/") {
set $skip_cache 1;
}
# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cachevhostdomain;
fastcgi_cache_valid 60m;
}
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\. { deny all; access_log off; log_not_found off; }
}
添加/etc/php5/fpm/pool.d/www.conf
sans 评论:
[www]
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
答案1
在您的配置中,您有两个位置块,\.php$
这很可能是罪魁祸首,将它们合并为一个,就应该没问题了。
有时长时间盯着代码/配置文件会让你错过一些显而易见的东西。