Nginx微博wordpress,为什么要返回旁路?

时间:2017-11-30 14:31:16

标签: nginx

最近我不得不为Wordpress应用程序设置AWS ASG环境。这个wordpress是一个非常常见的。

pre和pro中的环境是平等的,也是两者的行为。我的基础设施有:

  • 1 ASG
  • 1 ALB
  • m3.medium EC2实例
  • Memcached cluster
  • RDS MySQL
  • S3 for static content

Stack是一个常见的Nginx / Php 5.6 FPM,启用了微处理功能,W3TC可以为数据库和其他东西设置缓存。

我的nginx.conf就像(我将隐藏域名)

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
        worker_connections 65536;
        use epoll;
        multi_accept on;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    set_real_ip_from 172.16.0.0/16;
    real_ip_header X-Forwarded-For;
    log_format  main  '$host $remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"';

    access_log  /var/log/nginx/access.log  main;

    server_names_hash_max_size 1024;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    keepalive_requests 100000;
    keepalive_timeout  65 20;

    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_min_length 512;
    gzip_buffers 256 8k;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain test/html text/xml text/css image/x-icon image/bmp application/atom+xml text/javascript application/x-javascript application/pdf application/postscript application/rtf application/vnd.ms-powerpoint application/msword application/vnd.ms-excel application/vnd.wap.xhtml+xml;

    fastcgi_cache_path /var/run/cache/nginx levels=1:2 keys_zone=microcache:100m inactive=60m;
    add_header X-Cache $upstream_cache_status;

    #limit_req_zone $binary_remote_addr zone=one:10m rate=5r/m;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/conf.pre.d/*.conf;

}

虚拟主机的一个例子是:

server{
    listen 80;
    server_name   domain.org.dev;
    return        301  https://www.domain.org.dev$request_uri;
}

server {
        listen   80;

        root /var/www/domain;
        index index.html index.htm index index.php;

        server_name www.domain.org;

        error_page 503 @maintenance;
        location @maintenance {
                rewrite ^(.*)$ /503.html break;
        }

        if ($http_x_forwarded_proto != "https") {
               rewrite ^(.*)$ https://$server_name$1 permanent;
        }

        location / {
                if (-f $document_root/503.html) {
                    return 503;
                }
                try_files $uri/ $uri /index.php?q=$uri&$args;
                port_in_redirect off;
                add_header 'Access-Control-Allow-Origin' '*';
        }

         location ~ .php$ {
                #include global/wp-security.conf;
                include global/php.conf;
                include global/wp-microcache.conf;
        }

        location ~ /.ht(access|passwd) {
                deny  all;
        }
        location ~ /.git {
                deny  all;
        }
        location ~ /.svn {
                deny  all;
        }
        include global/static-content.conf;
}

最重要的部分是wp-microcache.conf。老实说,我看了一下这个网站来设置我的配置因为得到了很好的解释(https://github.com/A5hleyRich/wordpress-nginx/blob/master/global/server/fastcgi-cache.conf

fastcgi_cache_key "$scheme$request_method$host$request_uri";

fastcgi_cache_use_stale error timeout invalid_header http_500;

fastcgi_ignore_headers Cache-Control Expires Set-Cookie;


set $skip_cache 0;

if ($request_method = POST) {
    set $skip_cache 1;
}

if ($query_string != "") {
    set $skip_cache 1;
}

if ($request_uri ~* "/wp-admin/|/wp-json/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
    set $skip_cache 1;
}

if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
}

fastcgi_no_cache $skip_cache;
fastcgi_cache_bypass $skip_cache;
fastcgi_cache microcache;
fastcgi_cache_valid 60m;

所以我在Wordpress上运行BlazeMeter测试,只是GETS所以我期待在HIT周围取得相当大的成功。然而,在分析结果后,我注意到Worpress的许多URL都是BYPASSING缓存。然后我理解为什么我看到这么多php-fpm进程在EC2中做的事情同时测试正在运行。

无论如何,重点是,如果我测试Wordpress主页(无论是通过卷曲还是导航器):

$ curl -I https://www.domain.org.dev
HTTP/1.1 200 OK
Date: Thu, 30 Nov 2017 14:29:19 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Server: nginx
Vary: Accept-Encoding
Link: <https://www.domain.org.dev/wp-json/>; rel="https://api.w.org/"
Link: <https://www.domain.org.dev/>; rel=shortlink
Last-Modified: Thu, 30 Nov 2017 13:53:05 GMT
Link: <https://www.domain.org.dev/wp-json/>; rel="https://api.w.org/"
Link: <https://www.domain.org.dev/>; rel=shortlink
X-Cache: HIT

尽管如此,如果我从同一个Wordpress请求其他网址:

$ curl -I https://www.domain.org.dev/about-us
HTTP/1.1 301 Moved Permanently
Date: Thu, 30 Nov 2017 14:29:27 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Server: nginx
Last-Modified: Thu, 30 Nov 2017 14:18:58 GMT
Location: https://www.domain.org.dev/about-us/
X-Cache: BYPASS

为什么这种行为不同?为了解决这个问题,我可以检查哪些内容?我有点迷失在这里。我怀疑与插件或其他东西没有任何关系。

任何想法的人?

谢谢!

1 个答案:

答案 0 :(得分:0)

这很可能是由于语句try_files $uri $uri/ /index.php?q=$uri&$args;占用了一个页面路径(主页除外)并将其转换为查询字符串,即q=my-non-home-page。然后你有以下块来绕过查询字符串缓存,

if ($query_string != "") {
set $skip_cache 1;
}

我建议您将语句更新为try_files $uri $uri/ /index.php?$args;

您可以使用以下调试机制来帮助您突出问题的根本原因。

    set $cache_bypass_reason "NONE";
    set $skip_cache 0;

    # POST requests and URLs with a query string should always skip cache
    if ($request_method = POST) {
       set $skip_cache 1;
       set $cache_bypass_reason "POST";
     }
    if ($query_string != "") {
      set $skip_cache 1;
      set $cache_bypass_reason "QUERY-STRING";
    }
    etc

如果需要,可以重写任何路径,即

rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;