为什么使用 wordpress 时启用 php_fpm fastcgi_cache 后 NGINX 运行缓慢?

为什么使用 wordpress 时启用 php_fpm fastcgi_cache 后 NGINX 运行缓慢?

我遇到了一点麻烦。我按照教程一步一步进行操作rtcamp.com对于 wordpress 多站点,只需将“example.com”更改为我的域名。我安装了 nginx 帮助插件,看起来整个站点和所有功能都运行良好。(这是一个从 LAMP 设置迁移过来的约 40 个站点的网络)

不过我遇到了一个问题。即使使用 fastcgi_cache 进行配置,我的页面加载时间也非常长。当我查看源代码时间戳时,它显示的内容如下:

<!--Cached using Nginx-Helper on 2013-03-12 00:21:19. It took 62 queries executed in 13.000 seconds.
Visit http://wordpress.org/extend/plugins/nginx-helper/faq/ for more details -->

这不会发生在根站点上,只会发生在子目录站点上。

这是我的配置:这是 sites-available/example.com

#move next 3 lines to /etc/nginx/nginx.conf if you want to use fastcgi_cache across many sites 
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:500m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;

server {
    #@DM - uncomment following line for domain mapping or you will need to add every mapped-domain to server_name list 
    #listen 80 default_server;
    server_name example.com *.example.com ;
    #@DM - uncomment following line for domain mapping
    #server_name_in_redirect off;

    access_log   /var/log/nginx/example.com.access.log;
    error_log    /var/log/nginx/example.com.error.log;

    root /var/www/example.com/htdocs;
    index index.php index.html index.htm;

    #fastcgi_cache start
    set $skip_cache 0;

    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
        set $skip_cache 1;
    }   
    if ($query_string != "") {
        set $skip_cache 1;
    }   

    # Don't cache uris containing the following segments
    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.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;
    }

        if (!-e $request_filename) {
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
                rewrite ^(/[^/]+)?(/wp-.*) $2 last;
                rewrite ^/[^/]+(/.*.php)$ $1 last;
        }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }    

    location ~ \.php$ {
        try_files $uri /index.php; 
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    #   fastcgi_pass 127.0.0.1:9000;
        fastcgi_cache_bypass $skip_cache;
            fastcgi_no_cache $skip_cache;

        fastcgi_cache WORDPRESS;
        fastcgi_cache_valid  60m;
    }

    location ~ /purge(/.*) {
        fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
    }   

    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; }

##SITEMAP ABILTIES
    rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

}

我的 nginx.conf:

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # 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;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ## Block spammers and other unwanted visitors  ##
    include blockips.conf;


    #FastCGI
    fastcgi_intercept_errors on;
    fastcgi_ignore_client_abort on;
    fastcgi_buffers 8 32k;
    fastcgi_buffer_size 64k;
    fastcgi_read_timeout 120;
    fastcgi_index  index.php;

    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;



    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

如能帮助解决此问题,我们将不胜感激!:)

答案1

您如何确定速度很慢?您是否在 php-fpm 中启用了任何类型的限制?从 php-fpm 和 nginx 发送日志文件。

答案2

limit_req_zone 将对服务器的请求限制为每秒一个请求( )limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;,其中包括 PHP 和静态文件请求(具体取决于您放置语句的位置)limit_req zone=one;。尝试仅限制 PHP 请求(inside location ~ \.php$ {),并使静态内容不受限制。

相关内容