web-dev-qa-db-ja.com

W3 Total Cache - ApacheからNginxへの書き換え

次のApacheの書き換えをNginxに変換しようとしています。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} =""
RewriteCond %{REQUEST_URI} \/$
RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_Host}/%{REQUEST_URI}/_index.html" -f
RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_Host}/%{REQUEST_URI}/_index.html" [L]
</IfModule>

私がやったことは:

# Set a variable to work around the lack of nested conditionals
set $cache_uri $request_uri;

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

# Don't cache uris containing the following segments
if ($request_uri ~* "(\/wp-admin\/|\/xmlrpc.php|\/wp-(app|cron|login|register|mail)\.php|wp-.*\.php|index\.php|wp\-comments\-popup\.php|wp\-links\-opml\.php|wp\-locations\.php)") {
    set $cache_uri "no cache";
 }

# Don't use the cache for logged in users or recent commenters
if if ($http_cookie ~* "comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle") {
    set $cache_uri 'no cache';
 }

 # If the cache file does not exist, pass it of to Apache for processing
location / {
    try_files /wp-content/cache/page_enhanced/example.com/$cache_uri/_index.html @backend;
}

# Pass off php requests to Apache
location ~* \.php$ {           
    include /usr/local/etc/nginx/proxypass.conf;
    proxy_pass         http://127.0.0.1:80;
}

# Pass off php requests to Apache
location @backend {
    include /usr/local/etc/nginx/proxypass.conf;
    proxy_pass         http://127.0.0.1:80;
}

私が疑問に思っているのは私が何かを逃しているということですか?または何か悪いことをしましたか?

1
Dave

あなたのコメントに基づいて、これはW3 Total Cacheプラグインの "disk:enhanced"ページキャッシュメソッドを使ったNginx-Apacheスタックのためのソリューションです...

location / {
    error_page 418 = @cachemiss;
    recursive_error_pages on;

    if ($request_method = POST) { return  418; }

    if ($query_string != "") { return 418; }

    if ($request_uri ~* "(\/wp-admin\/|\/xmlrpc.php|\/wp-(app|cron|login|register|mail)\.php|wp-.*\.php|index\.php|wp\-comments\-popup\.php|wp\-links\-opml\.php|wp\-locations\.php)") { return 418; }

    if ($http_cookie ~* "comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle") { return 418; }

    try_files "/wp-content/cache/page_enhanced/$Host/$uri/_index.html" =418;

    # optional code
    # expires 30m;
    # add_header "X-W3TC-Cache" "HIT :)";
}

location @cachemiss {
    # pass the requests to backend (Apache)

    # optional header
    # add_header "X-W3TC-Cache" "Miss :(";
}

# other directives
# for example
location ~* \.php$ {
    # pass PHP requests to Apache
}

# another example
location /wp-admin {
    # pass requests to Apache
}

上記の解決策は、 Nginxでif文を使用することのベストプラクティス に従っており、変更するとWPSCに対して正しく機能します。それが役立つことを願っています。

2
Pothi Kalimuthu