Nginx+W3总缓存:重写规则问题

时间:2013-03-07 作者:Michael

nginx 1.2.7+WP 3.5.1+W3TC 0.9.2.8。

重写插件生成的规则根本不起作用,服务器在nginx服务器配置中包含规则后返回404。或者我做错了什么?

这是我的nginx.conf:

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

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    index index.html index.htm index.php;
    default_type application/octet-stream;

    sendfile on;
    tcp_nopush on;

    log_format main \'$remote_addr - $remote_user [$time_local] \'
        \'"$request" $status $body_bytes_sent "$http_referer" \'
        \'"$http_user_agent" "$http_x_forwarded_for"\';

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

    include /etc/nginx/conf.d/*.conf;
}
这是我的virtual server config:

server {
    listen 80;

    server_name example.com www.example.com;
    root /var/www/example.com;

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

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

    location ~ \\.php {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    include /var/www/example.com/nginx.conf;

}
最后config generated by W3TC (包含在服务器配置中):

# BEGIN W3TC Minify cache
location ~ /wp-content/cache/minify.*\\.js$ {
    types {}
    default_type application/x-javascript;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
}
location ~ /wp-content/cache/minify.*\\.css$ {
    types {}
    default_type text/css;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
}
location ~ /wp-content/cache/minify.*js\\.gzip$ {
    gzip off;
    types {}
    default_type application/x-javascript;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
    add_header Content-Encoding gzip;
}
location ~ /wp-content/cache/minify.*css\\.gzip$ {
    gzip off;
    types {}
    default_type text/css;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
    add_header Content-Encoding gzip;
}
# END W3TC Minify cache
# BEGIN W3TC Page Cache cache
location ~ /wp-content/cache/page_enhanced.*html$ {
    expires modified 3600s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding, Cookie";
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
}
location ~ /wp-content/cache/page_enhanced.*gzip$ {
    gzip off;
    types {}
    default_type text/html;
    expires modified 3600s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding, Cookie";
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
    add_header Content-Encoding gzip;
}
# END W3TC Page Cache cache
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ \\.(css|htc|js|js2|js3|js4)$ {
    expires 2628000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
}
location ~ \\.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
    expires 3600s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    try_files $uri $uri/ $uri.html /index.php?$args;
}
location ~ \\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
    expires 31536000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=31536000, public";
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
}
# END W3TC Browser Cache
# BEGIN W3TC Minify core
rewrite ^/wp-content/cache/minify.*/w3tc_rewrite_test$ /wp-content/plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 last;
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc .gzip;
}
if (-f $request_filename$w3tc_enc) {
    rewrite (.*) $1$w3tc_enc break;
}
rewrite ^/wp-content/cache/minify/(.+\\.(css|js))$ /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1 last;
# END W3TC Minify core
# BEGIN W3TC Page Cache core
set $w3tc_rewrite 1;
if ($request_method = POST) {
    set $w3tc_rewrite 0;
}
if ($query_string != "") {
    set $w3tc_rewrite 0;
}
if ($request_uri !~ \\/$) {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(comment_author|wp\\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") {
    set $w3tc_rewrite 0;
}
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc _gzip;
}
if (-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}
if ($w3tc_rewrite = 1) {
    rewrite .* "/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc" last;
}
# END W3TC Page Cache core
服务器返回404。怎么了?Wordpress不顾一切地寻求您的帮助。

2 个回复
最合适的回答,由SO网友:Michael 整理而成

Solved.

而不是

if (-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}
应该是

if (!-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}
在中W3 TC config.

SO网友:Paramdeo Singh

W3 Total Cache和Quick Cache等与Apache配合得很好。由于Wordpress在使用nginx时并不是现成的,您将由于涉及不同的重写规则而遇到问题。

尝试此插件:nginx Compatibility Plugin 这将使您的缓存能够与nginx+Wordpress一起工作。

如果您发现即使在使用插件时,仍没有缓存单个页面,请尝试DB Cache Reloaded.

干杯

结束

相关推荐

多个站点带有nginx的固定链接

我正在尝试设置一个新的本地开发服务器,而不是Apache,我决定使用nginx。我想做的是让每个WordPress站点位于其自己的目录中/usr/share/nginx/www. 这些网站只能在我们的本地网络上使用。例如:/usr/share/nginx/www/ourcompanywebsite -> http://192.168.2.250/ourcompanywebsite/usr/share/nginx/www/firstclientwebsite -> http://192.168.

Nginx+W3总缓存:重写规则问题 - 小码农CODE - 行之有效找到问题解决它

Nginx+W3总缓存:重写规则问题

时间:2013-03-07 作者:Michael

nginx 1.2.7+WP 3.5.1+W3TC 0.9.2.8。

重写插件生成的规则根本不起作用,服务器在nginx服务器配置中包含规则后返回404。或者我做错了什么?

这是我的nginx.conf:

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

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    index index.html index.htm index.php;
    default_type application/octet-stream;

    sendfile on;
    tcp_nopush on;

    log_format main \'$remote_addr - $remote_user [$time_local] \'
        \'"$request" $status $body_bytes_sent "$http_referer" \'
        \'"$http_user_agent" "$http_x_forwarded_for"\';

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

    include /etc/nginx/conf.d/*.conf;
}
这是我的virtual server config:

server {
    listen 80;

    server_name example.com www.example.com;
    root /var/www/example.com;

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

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

    location ~ \\.php {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    include /var/www/example.com/nginx.conf;

}
最后config generated by W3TC (包含在服务器配置中):

# BEGIN W3TC Minify cache
location ~ /wp-content/cache/minify.*\\.js$ {
    types {}
    default_type application/x-javascript;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
}
location ~ /wp-content/cache/minify.*\\.css$ {
    types {}
    default_type text/css;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
}
location ~ /wp-content/cache/minify.*js\\.gzip$ {
    gzip off;
    types {}
    default_type application/x-javascript;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
    add_header Content-Encoding gzip;
}
location ~ /wp-content/cache/minify.*css\\.gzip$ {
    gzip off;
    types {}
    default_type text/css;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
    add_header Content-Encoding gzip;
}
# END W3TC Minify cache
# BEGIN W3TC Page Cache cache
location ~ /wp-content/cache/page_enhanced.*html$ {
    expires modified 3600s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding, Cookie";
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
}
location ~ /wp-content/cache/page_enhanced.*gzip$ {
    gzip off;
    types {}
    default_type text/html;
    expires modified 3600s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding, Cookie";
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
    add_header Content-Encoding gzip;
}
# END W3TC Page Cache cache
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ \\.(css|htc|js|js2|js3|js4)$ {
    expires 2628000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
}
location ~ \\.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
    expires 3600s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    try_files $uri $uri/ $uri.html /index.php?$args;
}
location ~ \\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
    expires 31536000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=31536000, public";
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
}
# END W3TC Browser Cache
# BEGIN W3TC Minify core
rewrite ^/wp-content/cache/minify.*/w3tc_rewrite_test$ /wp-content/plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 last;
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc .gzip;
}
if (-f $request_filename$w3tc_enc) {
    rewrite (.*) $1$w3tc_enc break;
}
rewrite ^/wp-content/cache/minify/(.+\\.(css|js))$ /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1 last;
# END W3TC Minify core
# BEGIN W3TC Page Cache core
set $w3tc_rewrite 1;
if ($request_method = POST) {
    set $w3tc_rewrite 0;
}
if ($query_string != "") {
    set $w3tc_rewrite 0;
}
if ($request_uri !~ \\/$) {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(comment_author|wp\\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") {
    set $w3tc_rewrite 0;
}
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc _gzip;
}
if (-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}
if ($w3tc_rewrite = 1) {
    rewrite .* "/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc" last;
}
# END W3TC Page Cache core
服务器返回404。怎么了?Wordpress不顾一切地寻求您的帮助。

2 个回复
最合适的回答,由SO网友:Michael 整理而成

Solved.

而不是

if (-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}
应该是

if (!-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}
在中W3 TC config.

SO网友:Paramdeo Singh

W3 Total Cache和Quick Cache等与Apache配合得很好。由于Wordpress在使用nginx时并不是现成的,您将由于涉及不同的重写规则而遇到问题。

尝试此插件:nginx Compatibility Plugin 这将使您的缓存能够与nginx+Wordpress一起工作。

如果您发现即使在使用插件时,仍没有缓存单个页面,请尝试DB Cache Reloaded.

干杯

相关推荐

Docker化的Wordpress出现了Nginx代理问题

我有一个停靠的Nginx代理,它位于我的服务器上。我的一个网站是Docker Compose Wordpress网站。它们是单独的容器,必须保持这种状态。When I update my Site Url in the Settings I can still log into my Admin Dashboard, but I cannot reach my actual site. I receive a 502 Gateway Error from Nginx. Nginx服务器块: serv