Wordpress on Nginx+php5-fpm+varnish+APC高CPU和内存使用


Wordpress on Nginx+php5-fpm+varnish+APC high CPU and memory usage

经过几天的调试和调整设置,我已经筋疲力尽了&无法找到解决方案。请导游。

我在DigitalOcean上有以下服务器:

64GB Memory
8 Core processor
200GB SSD drive

我在上面运行一个Wordpress站点。网站获得高流量。(2000到3000个并发实时用户)而且我确信由于我的设置不好,我正在失去流量& &;无法向用户提供页面。我期望实时用户达到5000+,但它总是停留在2000左右。

我不断得到OOM错误,由于mysqlphp5-fpm被杀死,网站下降。如果我调整php-fpmnginx,我得到502503错误。或者我得到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 settings:

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 settings: /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 config: /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.";
        }
}

这样的流量和配置,你错过了很多机会(和收入):)。你真的应该掌握Nginx微缓存和Varnish!