【问题标题】:add header to response in nginx location directive在 nginx 位置指令的响应中添加标头
【发布时间】:2017-05-15 00:06:06
【问题描述】:

我尝试将响应标头添加到仅提供的一个位置路径,并且 nginx 配置看起来像

server {
    ...
    add_header x-test test;
    location /my.img {
        rewrite ^/my.img$ /119.img;
        add_header x-amz-meta-sig 1234567890abcdef;
    }
}

但只有顶级标题(x-test)有效,位置指令中的那个不显示,如图所示

$ curl -v -o /tmp/test.img 'https://www.example.com/my.img'
< HTTP/1.1 200 OK
< Server: nginx/1.9.3 (Ubuntu)
< Date: Sun, 14 May 2017 23:58:08 GMT
< Content-Type: application/octet-stream
< Content-Length: 251656
< Last-Modified: Fri, 03 Mar 2017 04:57:47 GMT
< Connection: keep-alive
< ETag: "58b8f7cb-3d708"
< x-test: test
< Accept-Ranges: bytes
< 
{ [16104 bytes data]

如何仅为提供的特定文件发回自定义headrr。

【问题讨论】:

    标签: nginx nginx-location


    【解决方案1】:

    rewrite 语句是一个隐含的rewrite...last,这意味着最终的 URI /119.img 不会被这个 location 块处理。在计算响应标头时,nginx 位于不同的location 块中。

    您可以尝试使用 rewrite...break 语句从同一位置块中处理最终 URI。详情请见this document

    location = /my.img {
        root /path/to/file;
        rewrite ^ /119.img break;
        add_header x-amz-meta-sig 1234567890abcdef;
    }
    

    如果 location 只匹配一个 URI,请使用 = 格式。详情请见this document

    还要注意,add_header 语句在此 location 块中的存在意味着外部语句将不再被继承。有关详细信息,请参阅this document。 :

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2020-03-26
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      相关资源
      最近更新 更多