gpt4 book ai didi

Nginx 设置过期 header 与代理

转载 作者:行者123 更新时间:2023-12-01 09:56:45 25 4
gpt4 key购买 nike

我有以下基本 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;
}
}

现在我尝试为我的 Assets 设置以下到期标题,但没有成功:

location ~ ^/assets/ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}

根据我找到的信息,Nginx 一次只使用一个位置路径,因此必须复制 Assets 位置 block 内的 proxy_* 参数。如果我只是复制它们,我会收到一个错误(带有 proxy_pass 的正则表达式),可以通过在将 URL 传递给代理之前重写 URL 来解决。我已经对此进行了一些实验,但我也没有让它工作。

有没有人有一个例子来说明当 proxy_pass 存在时如何设置过期 header ?我只是希望 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;
}

proxy_pass 的 Nginx 文档说:

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.

我的情况 /assets// (这是一个 URI)替换。为避免这种情况,要么使用 URI 等于位置前缀 (proxy_pass http://localhost:2368/assets/;) 的 proxy_pass 或根本不使用 URI (proxy_pass http://localhost:2368;)。但在后一种情况下,nginx 将代理非规范化的 URI。

关于Nginx 设置过期 header 与代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25063361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com