【问题标题】:Nginx and raw headersNginx 和原始标头
【发布时间】:2012-08-02 19:08:04
【问题描述】:

我正在使用 Play Framework 开发一个 web api。我使用 nginx 作为反向代理。由于api会被嵌入式系统使用,所以返回的信息应该尽可能的简洁。

Play Framework 在生产模式下的返回异常如下:(RAW HTTP 取自 Fiddler)

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Cache-Control: no-cache
Content-Length: 14

aTqYu1mxQPy|10

但是,当我在用户和 api 之间放置 nginx 时,响应变成了这样:

HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Sun, 05 Aug 2012 15:08:31 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 14
Connection: close
Cache-Control: no-cache

aTqYu1mxQPy|10

我根本不需要 ServerDateConnection 标头。它们由 nginx 自动添加。 (或者是因为我在之前的实验中弄乱了我的 nginx 配置)

有没有办法告诉 ngnix 不要告诉任何标头并原封不动地传递它们?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    这里是 nginx 源删除“Connection”标头的补丁:http://mailman.nginx.org/pipermail/nginx-devel/2017-February/009440.html

    diff -r d2b2ff157da5 -r 25129d5509b8 src/http/ngx_http_header_filter_module.c
    --- a/src/http/ngx_http_header_filter_module.c  Tue Jan 31 21:19:58 2017 +0300
    +++ b/src/http/ngx_http_header_filter_module.c  Thu Feb 02 02:14:06 2017 +0800
    @@ -389,7 +389,9 @@
             }
    
         } else {
    -        len += sizeof("Connection: close" CRLF) - 1;
    +        if (clcf->keepalive_header != 0) {
    +            len += sizeof("Connection: close" CRLF) - 1;
    +        }
         }
    
     #if (NGX_HTTP_GZIP)
    @@ -560,8 +562,10 @@
             }
    
         } else {
    -        b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
    -                             sizeof("Connection: close" CRLF) - 1);
    +        if (clcf->keepalive_header != 0){
    +             b->last = ngx_cpymem(b->last, "Connection: close" CRLF,
    +                                  sizeof("Connection: close" CRLF) - 1);
    +        }
         }
    
     #if (NGX_HTTP_GZIP)
    

    这里是 nginx 删除其他头的模块: https://github.com/openresty/headers-more-nginx-module

    keepalive_timeout 0;
    keepalive_requests 0;
    
    chunked_transfer_encoding off;
    
    more_clear_headers 'Cache-Control';
    more_clear_headers 'Content-Type';
    more_clear_headers 'Date';
    more_clear_headers 'Expires';
    more_clear_headers 'Server';
    more_clear_headers 'X-Debug-Token';
    more_clear_headers 'X-Debug-Token-Link';
    more_clear_headers 'X-Powered-By';
    more_clear_headers 'X-Robots-Tag';
    

    【讨论】:

      【解决方案2】:

      您可以使用 nginx 的第三方模块来修改(和删除)任何标头,https://github.com/agentzh/headers-more-nginx-module
      但根据 RFC 2616,在 HTTP 协议中,您只能删除 Server 标头。
      Connection: close - 用于关闭持久 (HTTP/1.1) 连接。
      Date 标头必须在 HTTP/1.1 中出现,在所有请求中,以下情况除外:

        1. If the response status code is 100 (Continue) or 101 (Switching
           Protocols), the response MAY include a Date header field, at
           the server's option.
      
        2. If the response status code conveys a server error, e.g. 500
           (Internal Server Error) or 503 (Service Unavailable), and it is
           inconvenient or impossible to generate a valid Date.
      
        3. If the server does not have a clock that can provide a
           reasonable approximation of the current time, its responses
           MUST NOT include a Date header field
      

      据我所知,nginx 严格遵循 RFC。

      【讨论】:

      • “如果响应是通过代理转发的,代理应用程序不得修改Server response-header。相反,它应该包含一个Via 字段。”根据您提到的同一文档,您不得让 NGINX 将其名称放在 Server 标头中。
      猜你喜欢
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 2018-06-23
      • 2011-06-08
      • 2017-11-19
      • 2012-08-27
      • 2013-03-19
      • 2018-03-29
      相关资源
      最近更新 更多