Nginx + PHP-FPM + APC 响应时间慢


Nginx + PHP-FPM + APC slow response times

我们目前有一个Nginx服务器设置,带有PHP-FPM和APC用于缓存。它被设置为运行一个中等负载的网站,我们希望强调网站加载时间,因为他们的整个业务严重依赖网站(在线预订)。

规格是:1 个中央处理器,1GB 内存,Ubuntu 12.04 LTS,Nginx 1.1.19,PHP-FPM 5.3.10

它已经工作了很长一段时间(大约 1 年)没有任何问题。

但是,我们需要设置拆分测试情况来测试网站的新版本。由于我们不能在不同的代码片段之间交替(使用 CMS 并且数据库结构也发生了变化),我们决定使用子域并将 20% 的流量发送到子域。

为了实现这一点,我们简单地创建了一个新的服务器块,并在一个新文件夹中完全独立地设置新站点。然后在原始服务器块文件中,我们使用nginx拆分客户端来设置cookie,并在该百分位数中重定向到"new"。

我们认为在主/旧站点上执行此操作是可以的,因为它们的所有流量都来自Google,并且在测试期间不太可能是重复客户,因此有人直接访问"新"的可能性非常低,不会影响结果。

起初它工作正常,几天后我们开始收到一些关于加载时间慢的投诉。经过一些调试后,它只是流量流向"新",并且只是间歇性的。此时,原始站点根本没有受到影响。

我们已经慢慢禁用了功能,试图找出问题所在,但我们找不到问题所在。拆分测试已被暂时禁用,事实证明,即使您尝试直接加载子域,它有时也会有非常慢的初始响应(大约 20 秒)。

起初我认为这可能是一个缓存问题,所以我们完全禁用了 APC,但它并没有解决问题。我们确实在某一时刻启用了fastcgi缓存以尝试加快速度,但这很久以前就被禁用了。

我们还有其他一些设置非常相似的服务器,所以我正在努力找到问题所在。

以下是文件片段

Nginx.conf

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;
events {
        worker_connections 1024;
        use epoll;
        multi_accept on;
}
http {
        ##
        # Basic Settings
        ##
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_tokens off;
        send_timeout 60;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        #fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache:10m max_size=100m inactive=60m;
        #fastcgi_cache_key "$scheme$request_method$host$request_uri";
        ##
        # Logging Settings
        ##
        access_log off;
        error_log /var/log/nginx/error.log;
        ##
        # Gzip Settings
        ##
        gzip on;
        gzip_disable "msie6";
        gzip_http_version 1.1;
        gzip_vary on;
        gzip_comp_level 6;
        gzip_proxied any;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
        gzip_buffers 16 8k;
        open_file_cache          max=2000  inactive=20s;
        open_file_cache_valid    30s;
        open_file_cache_min_uses 5;
        open_file_cache_errors   off;
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

站点 1/原始

split_clients "app${remote_addr}${http_user_agent}${date_gmt}" $upstream_variant {
        20% "new";
        * "original";
}
map $cookie_split_test_version $upstream_group {
        default    $upstream_variant;
        "new"      "new";
        "original" "original";
}
geo $internal_request {
        xxx.xxx.xxx.xxx 1;
        default 0;
}

server {
        listen 80;
        server_name domain.com otherdomain.com$
        return 301 $scheme://www.domain.com$request_uri;
}
server {
        listen 80;
        listen 443 ssl;
        add_header Set-Cookie "split_test_version=$upstream_group;Path=/;Max-Age=3600;";
        if ($upstream_group = "new") {
              set $test_group 1;
        }
        if ($test_group = 1) {
                return 301 $scheme://new.domain.com$request_uri;
                break;
        }

        ssl_certificate path/to/ssl;
        ssl_certificate_key path/to/sslkey;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;

        root /var/www/domain.com/httpdocs;
        index index.php index.html index.htm;
        server_name www.domain.com
    client_max_body_size 2M;

        location / {
                try_files $uri @website;
        }

    location @website {
                include fastcgi_params;
                fastcgi_split_path_info ^(.+'.php)(/.+)$;
                fastcgi_param SCRIPT_FILENAME $document_root/framework/main.php;
                fastcgi_param SCRIPT_NAME /framework/main.php;
                fastcgi_param QUERY_STRING url=$uri&$args;
                fastcgi_param ENVIRONMENT production;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_buffer_size 32k;
                fastcgi_buffers 4 32k;
                fastcgi_busy_buffers_size 64k;
        }
    ///other location directives
    expires 2w;
}

站点 2/新建

server {
        listen 80;
        listen 443 ssl;
        root /var/www/domain.com/subdomains/new;
        index index.php index.html index.htm;
        server_name new.domain.com
        ssl_certificate path/to/ssl;
        ssl_certificate_key path/to/sslkey;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
        client_max_body_size 2M;

        location / {
                try_files $uri @website;
        }

    location @website {
                include fastcgi_params;
                fastcgi_split_path_info ^(.+'.php)(/.+)$;
                fastcgi_param SCRIPT_FILENAME $document_root/framework/main.php;
                fastcgi_param SCRIPT_NAME /framework/main.php;
                fastcgi_param QUERY_STRING url=$uri&$args;
                fastcgi_param ENVIRONMENT new;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_buffer_size 32k;
                fastcgi_buffers 4 32k;
                fastcgi_busy_buffers_size 64k;
        }
    ///other location directives
    expires 2w;
}

来自 PHP-FPM pool.d 的位

user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
pm.max_children = 10
pm.start_servers = 5
pm.min_spare_servers = 4
pm.max_spare_servers = 6
pm.max_requests = 1000

我也不认为这是内存问题(没有收到任何内存警报)

top - 10:24:51 up 54 days, 11:11,  1 user,  load average: 0.16, 0.12, 0.12
Tasks:  77 total,   1 running,  76 sleeping,   0 stopped,   0 zombie
Cpu(s):  6.7%us,  0.7%sy,  0.0%ni, 91.6%id,  1.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1015380k total,   896120k used,   119260k free,   149888k buffers
Swap:        0k total,        0k used,        0k free,   333820k cached
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
28873 www-data  20   0  293m  47m 5572 S  6.0  4.8   0:41.26 php5-fpm
25241 www-data  20   0 80176 6112 2036 S  0.3  0.6  28:57.78 nginx
28872 www-data  20   0  293m  47m 5548 S  0.0  4.8   0:34.50 php5-fpm
28874 www-data  20   0  290m  44m 5556 S  0.0  4.5   0:34.60 php5-fpm
28875 www-data  20   0  286m  39m 5320 S  0.0  4.0   0:40.08 php5-fpm
29249 www-data  20   0  281m  34m 5288 S  0.0  3.5   0:32.04 php5-fpm
31620 www-data  20   0  280m  33m 5176 S  0.0  3.4   0:02.06 php5-fpm

日志文件似乎没有为我提供任何有用的信息,并且它不会导致任何以 15s 阈值登录 php slowlog 的内容。

如前所述,这只是一个间歇性问题,我找不到它的模式。

任何帮助将不胜感激。

另外,如果有更好的方法来进行拆分测试,我很想听听。

您是否安装了NewRelic(或任何其他分析工具)?这将有助于找出交易缓慢的确切原因,因为问题可能完全出在其他地方。