Nginx和原始标头

时间:2012-08-05 15:28:18

标签: nginx

我正在使用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

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

有没有告诉ngnix不要告诉任何标题并将它们原封不动?

2 个答案:

答案 0 :(得分:2)

您可以使用nginx的第三方模块修改(并删除)任何标头,https://github.com/agentzh/headers-more-nginx-module
但根据RFC 2616,在HTTP协议中,您只能删除服务器标头 连接:关闭 - 用于关闭持久性(HTTP / 1.1)连接。
在所有请求中,日期标头必须在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。

答案 1 :(得分:0)

以下是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';