2016-01-08 23 views
2

我在Nginx平台上运行wordpress,并分别在.php和静态资源上设置了过期标题。但现在需要使用nginx将自定义过期头添加到wordpress中的某些URL。我已经尝试添加位置块,但似乎它被覆盖在.php块中的expires标题覆盖。如何覆盖在Nginx上运行的wordpress中的某些URL的expires标题

我创建了一个名为sports的wordpress页面,并且希望提供没有过期标题的url,并且对于其他url的过期标头应为10分钟

我的配置以供参考:

server { 
    listen  0.0.0.0:80;    # your server's public IP address 
    server_name www.abc.com;     # your domain name 
    index index.php index.html ; 
    root   /srv/www; # absolute path to your WordPress installation 
    set $no_cache 0; 
    try_files $uri $uri/ /index.php; 




location ~*^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|css|woff|js|rtf|flv|pdf)$ { 
       access_log off; log_not_found off; expires 365d; 
     } 


    location/{ 
       try_files $uri $uri/ /index.php?$args; 
       expires  modified +10m; 

     } 

location ~ .php$ { 
       try_files $uri /index.php; 
       include fastcgi_params; 
       fastcgi_pass 127.0.0.1:9000; 
set $no_cache 0; 
add_header Cache-Control public; 
expires  modified +10m; 

} 

location ~* /sports 
{ 
    expires -1; 
} 
} 
+0

在Wordpress中使用[send_headers动作](http://codex.wordpress.org/Plugin_API/Action_Reference/send_headers)不会更容易吗? –

+0

是的,它可以做到,但会更好的服务器,因为它会逃脱额外的PHP电话和发展的错误 – user3136348

+0

IMO此逻辑属于应用程序而不是服务器,但这只是我个人的意见。 –

回答

0

的URI等/sports/实际上路由到/index.php用含有$request_uri的值的参数。在nginx之内,这些是由.php位置块处理的全部,并且在该块内使用expires指令的值并单独使用该块。

一种可能的解决方案是使expires指令的值的变量:

location ~ \.php$ { 
    expires $expires; 
    ... 
} 

而创建依赖于原始请求URI值的map$request_uri):

map $request_uri $expires { 
    default off; 
    ~^/sports +10m; 
} 
server { 
    ... 
} 

map指令位于http块中或与server块位于同一级别。

有关详细信息,请参见thisthis

+0

试过了。但得到这个错误: - nginx:[emerg]“expires”指令无效值在/etc/nginx/sites-enabled/abc.com.conf – user3136348

+0

更新:现在才知道,expires指令只支持变量,如果Nginx版本是<1.7.9。我没有尝试过,但它应该工作。 :) – user3136348