简体   繁体   English

Nginx-Wordpress上的W3缓存规则

[英]Nginx - W3 Caching Rules on Wordpress

To start off, I'm very new to nginx. 首先,我是nginx的新手。

I am trying to implement gzip compression and browser caching rules on a series of Nginx servers that are hosting wordpress sites. 我正在尝试在托管wordpress网站的一系列Nginx服务器上实现gzip压缩和浏览器缓存规则。 I got code from the following page that is supposed to be put in the nginx.conf file: http://codex.wordpress.org/Nginx 我从以下页面获得了应该放在nginx.conf文件中的代码: http : //codex.wordpress.org/Nginx

W3 Total Cache Rules 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

I tried placing it in the http { section and restart the server, but it said locations are in the wrong place. 我尝试将其放在http {部分中,然后重新启动服务器,但是它说位置在错误的位置。

Does it need to go in the server { ? 是否需要进入server { Or what is the best place to put this? 或放置此物品的最佳地点是什么?

Thanks! 谢谢!

from http://nginx.org/en/docs/http/ngx_http_core_module.html#location we learn that the accepted contexts for a location block are server and location. http://nginx.org/en/docs/http/ngx_http_core_module.html#location可以知道,位置块的可接受上下文是服务器和位置。

So yes you need to put the location blocks inside a server block (or nested inside another location block but your config doesn't do that) 因此,是的,您需要将位置块放置在服务器块内(或嵌套在另一个位置块内,但您的配置不执行此操作)

the gzip bits have an allowed context of "http, server, location, if in location" according to http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip , so you can put them in either your http or server block. 根据http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip,gzip位具有允许的上下文“ http,服务器,位置(如果在位置)”,因此您可以将它们放在您的http中或服务器块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM