【问题标题】:How to override expires header for certain urls in wordpress running on Nginx如何覆盖在 Nginx 上运行的 wordpress 中某些 url 的过期标头
【发布时间】:2016-01-10 01:22:29
【问题描述】:

我在 Nginx 平台上运行 wordpress,并在 .php 和静态资产上分别设置了 expires 标头。但现在的要求是使用 nginx 将自定义过期标头添加到 wordpress 中的某些 url。我曾尝试添加位置块,但似乎它被 .php 块中写入的 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;
}
}

【问题讨论】:

  • 在 Wordpress 中使用 send_headers action 不是更容易吗?
  • 是的,它可以完成,但在服务器上会更好,因为它可以避免额外的 php 调用和开发错误
  • IMO 这个逻辑属于应用程序而不是服务器,但这只是我个人的看法。

标签: php wordpress nginx header


【解决方案1】:

/sports/ 这样的URI 实际上被路由到/index.php,其参数包含$request_uri 的值。在nginx 中,它们全部.php 位置块处理,并使用该块内的expires 指令的值并且单独使用该块。

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

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

并根据原始请求 URI ($request_uri) 的值创建一个 map

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

请注意,map 指令位于 http 块中或与 server 块处于同一级别。

详情请参阅thisthis

【讨论】:

  • 试过了。但收到此错误:- nginx: [emerg] "expires" 指令在 /etc/nginx/sites-enabled/abc.com.conf 中的值无效
  • 更新:现在才知道, expires 指令仅支持 Nginx 版本
猜你喜欢
  • 2019-09-15
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-03
  • 2012-04-21
相关资源
最近更新 更多