Nginx设置使用代理过期标头

时间:2014-07-31 15:44:38

标签: nginx proxy

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

现在我尝试为我的资产设置以下过期标头,但没有成功:

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

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

有没有人举例说明当proxy_pass存在时如何设置到期标头?我只是希望xxx.com/assets/下的所有文件都有正确的到期日期。

2 个答案:

答案 0 :(得分:2)

使用下面的脚本管理解决它(注意代理后的/ assets /)。

server {
    listen 80;                                                             
    server_name xaviertalpe.be;

    client_max_body_size 10M;

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

        proxy_pass http://localhost:2368/assets/;
        proxy_set_header Host $host;
        proxy_buffering off;
    }

    location / {
        proxy_pass http://localhost:2368/;
        proxy_set_header Host $host;
        proxy_buffering off;
    }
}

答案 1 :(得分:2)

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文档说:

  

如果使用URI指定了proxy_pass指令,那么当请求传递给服务器时,与该位置匹配的规范化请求URI的部分将被指令中指定的URI替换。

我的情况/assets//(这是一个URI)取代。为避免这种情况,请使用proxy_pass,其URI等于位置前缀(proxy_pass http://localhost:2368/assets/;),或者根本不使用URI(proxy_pass http://localhost:2368;)。但在后一种情况下,nginx将代理非标准化的URI。

相关问题