如何覆盖 Nginx 上 wordpress 运行 中某些 url 的过期 header
How to override expires header for certain urls in wordpress running on Nginx
我是 运行 Nginx 平台上的 wordpress,并且在 .php 和静态资产上分别设置了过期 header。但现在的要求是使用 nginx 将自定义过期 header 添加到 wordpress 中的某些 urls 。我试过添加位置块,但似乎它被写在 .php 块
中的过期 header 覆盖了
我创建了一个名为 sports 的 wordpress 页面,并希望提供 url 没有过期 header 并且 url 的其余部分过期 header 应该是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;
}
}
像 /sports/
这样的 URI 实际上被路由到 /index.php
,其参数包含 $request_uri
的值。在 nginx
中,这些 all 由 .php
位置块处理,并在该块中使用 expires
指令的值,并且单独使用该块。
一个可能的解决方案是使 expires
指令的值成为一个变量:
location ~ \.php$ {
expires $expires;
...
}
并根据原始请求 URI ($request_uri
) 创建 map
个值:
map $request_uri $expires {
default off;
~^/sports +10m;
}
server {
...
}
请注意,map
指令位于 http
块中或与 server
块处于同一级别。
我是 运行 Nginx 平台上的 wordpress,并且在 .php 和静态资产上分别设置了过期 header。但现在的要求是使用 nginx 将自定义过期 header 添加到 wordpress 中的某些 urls 。我试过添加位置块,但似乎它被写在 .php 块
中的过期 header 覆盖了我创建了一个名为 sports 的 wordpress 页面,并希望提供 url 没有过期 header 并且 url 的其余部分过期 header 应该是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;
}
}
像 /sports/
这样的 URI 实际上被路由到 /index.php
,其参数包含 $request_uri
的值。在 nginx
中,这些 all 由 .php
位置块处理,并在该块中使用 expires
指令的值,并且单独使用该块。
一个可能的解决方案是使 expires
指令的值成为一个变量:
location ~ \.php$ {
expires $expires;
...
}
并根据原始请求 URI ($request_uri
) 创建 map
个值:
map $request_uri $expires {
default off;
~^/sports +10m;
}
server {
...
}
请注意,map
指令位于 http
块中或与 server
块处于同一级别。