我有以下基本 Nginx 配置(在 DigitalOcean droplet 上预装了 Ghost 平台):
server {
listen 80;
server_name xxx.com;
client_max_body_size 10M;
location / {
proxy_pass http://localhost:2368/;
proxy_set_header Host $host;
proxy_buffering off;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试为我的资产设置以下到期标题,但没有成功:
location ~ ^/assets/ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
Run Code Online (Sandbox Code Playgroud)
根据我发现的信息,Nginx 一次只使用一个位置路径,因此必须将 proxy_* 参数复制到资产位置块中。如果我只是复制它们,我会收到一个错误(regex with proxy_pass),可以通过在将 URL 传递给代理之前重写 URL 来解决该错误。我已经用它做了一些实验,但我也没有让它工作。
有没有人有一个示例,说明如何在存在 proxy_pass 时设置到期标头?我只是希望 xxx.com/assets/ 下的所有文件都有正确的到期日期。
location /assets/ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
proxy_pass http://localhost:2368/assets/;
# or proxy_pass http://localhost:2368;
proxy_set_header Host $host;
proxy_buffering off;
}
Run Code Online (Sandbox Code Playgroud)
Nginx doc for proxy_pass say that:
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive.
I your case /assets/ get replaced by / (which is an URI). To avoid that either use proxy_pass with URI equal to location prefix (proxy_pass http://localhost:2368/assets/;) or don't use URI at all (proxy_pass http://localhost:2368;). But in latter case nginx will proxy unnormalized URI.
| 归档时间: |
|
| 查看次数: |
11855 次 |
| 最近记录: |