【问题标题】:Nginx - W3 Caching Rules on WordpressNginx - WordPress 上的 W3 缓存规则
【发布时间】:2012-08-24 10:48:57
【问题描述】:

首先,我对 nginx 很陌生。

我正在尝试在托管 wordpress 站点的一系列 Nginx 服务器上实现 gzip 压缩和浏览器缓存规则。我从以下页面获取了应该放在 nginx.conf 文件中的代码: http://codex.wordpress.org/Nginx

W3 总缓存规则

# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ \.(css|js)$ {
    expires 31536000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
    add_header X-Powered-By "W3 Total Cache/0.9.2.3";
}
location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
    expires 180s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=180, public, must-revalidate, proxy-revalidate";
    add_header X-Powered-By "W3 Total Cache/0.9.2.3";
}
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
    expires 31536000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
    add_header X-Powered-By "W3 Total Cache/0.9.2.3";
}
# END W3TC Browser Cache
# BEGIN W3TC Skip 404 error handling by WordPress for static files
if (-f $request_filename) {
    break;
}
if (-d $request_filename) {
    break;
}
if ($request_uri ~ "(robots\.txt|sitemap(_index|[0-9]+)?\.xml(\.gz)?)") {
    break;
}
if ($request_uri ~* \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$) {
    return 404;
}
# END W3TC Skip 404 error handling by WordPress for static files

我尝试将它放在http { 部分并重新启动服务器,但它说位置在错误的位置。

是否需要进入server {?或者放在什么地方最好?

谢谢!

【问题讨论】:

    标签: wordpress caching nginx gzip browser-cache


    【解决方案1】:

    http://nginx.org/en/docs/http/ngx_http_core_module.html#location 我们了解到,位置块的可接受上下文是服务器和位置。

    所以是的,您需要将位置块放在服务器块内(或嵌套在另一个位置块内,但您的配置不这样做)

    根据http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip,gzip 位具有“http, server, location, if in location”的允许上下文,因此您可以将它们放在您的 http 或服务器块中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-24
      • 2012-06-07
      • 2016-02-24
      • 2019-02-11
      • 2016-05-20
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多