经过几天的调试和调整后,我已经筋疲力尽了。无法找到解决方案。请指导。
我在DigitalOcean上有以下服务器:
64GB Memory
8 Core processor
200GB SSD drive
我正在运行一个Wordpress网站。网站流量很大。 (2000到3000个并发实时用户)而且我确信由于我的糟糕设置,我正在失去流量和安全性。无法向用户提供页面。我希望实时用户为5000+,但总是保持在2000左右。
我经常收到OOM错误,导致mysql
或php5-fpm
被杀,网站出现故障。如果我调整php-fpm
和nginx
,我会收到502
和503
错误。或者我收到upstream timed out (110: Connection timed out)'
或FastCGI sent in stderr: PHP message: PHP Fatal error: Maximum execution time of 30 seconds exceeded
错误。
现在,我已经调整了设置,以便我不会收到任何错误,但流量已降至大约1500个并发用户,并且它拒绝上升。所以我确定我的设置有问题。
/etc/php5/fpm/pool.d/www.conf
设置:
pm = dynamic
pm.max_children = 150
pm.start_servers = 40
pm.min_spare_servers = 30
pm.max_spare_servers = 50
pm.max_requests = 1000
FastCGI设置:/etc/nginx/conf.d/default.conf
location ~ \.php$ {
try_files $uri =404;
# proxy buffers - no 502 errors!
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
fastcgi_buffers 256 16k;
fastcgi_buffer_size 128k;
fastcgi_max_temp_file_size 0;
fastcgi_intercept_errors on;
fastcgi_keep_conn off;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/dev/shm/php-fpm-www.sock;
}
APC设置:/etc/php5/fpm/php.ini
[apc]
apc.write_lock = 1
apc.slam_defense = 0
apc.shm_size = "1024M"
我注意到php5-fpm
进程占用了大量内存。
例如。当我计算每个进程的平均内存时,我得到:
ps --no-headers -o "rss,cmd" -C php5-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
为238M
提供了1100的并发流量。
请指导我配置不正确的地方。因为我百分百肯定我的交通堵塞了。
其他信息
Nginx配置:/etc/nginx/nginx.conf
worker_processes 24;
worker_rlimit_nofile 20000;
events {
worker_connections 40000;
use epoll;
multi_accept on;
}
但我注意到服务器上的ulimit是:
ulimit -n
仅显示1024
。这与我的问题有关吗?
Daniel的回复后添加了VCL
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
.max_connections = 800;
}
acl purge {
"localhost";
}
sub vcl_recv {
set req.grace = 6h;
# Set X-Forwarded-For header for logging in nginx
remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;
# Remove has_js and CloudFlare/Google Analytics __* cookies.
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
# Remove a ";" prefix, if present.
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
# Either the admin pages or the login
if (req.url ~ "/wp-(login|admin|cron)") {
# Don't cache, pass to backend
return (pass);
}
# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");
# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");
# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");
# Static content unique to the theme can be cached (so no user uploaded images)
# The reason I don't take the wp-content/uploads is because of cache size on bigger blogs
# that would fill up with all those files getting pushed into cache
if (req.url ~ "wp-content/themes/" && req.url ~ "\.(css|js|png|gif|jp(e)?g)") {
unset req.http.cookie;
}
# Even if no cookies are present, I don't want my "uploads" to be cached due to their potential size
if (req.url ~ "/wp-content/uploads/") {
return (pass);
}
# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
# A wordpress specific cookie has been set
return (pass);
}
# allow PURGE from localhost
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return (lookup);
}
# Force lookup if the request is a no-cache request from the client
if (req.http.Cache-Control ~ "no-cache") {
return (pass);
}
# Try a cache-lookup
return (lookup);
}
sub vcl_fetch {
#set obj.grace = 5m;
set beresp.grace = 6h;
}
sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
}
答案 0 :(得分:0)
有了这个流量和这个配置,你就错过了很多机会(和收入):)。你真的应该接受Nginx微洗和Varnish!