如何检查Nginx规则中的响应头的存在?

在Nginx中为请求设置重写规则certificate是相当简单的。 对于回应 ,不是那么多(至less不是我)。 如果未设置响应的Content-Length标头,我想从响应中去除Content-Type标头。 我已经安装了NginxHttpHeadersMoreModule ,所以应该允许我删除头,但我似乎无法find一种方法来检查是否存在使用Nginxconfiguration中的规则的响应的Content-Length头。 任何build议如何做到这一点将不胜感激!

看起来像有人问这个堆栈溢出,并有一个variables为每个发送头$ $ sent_http_my_custom_header。

有关详细信息,请参见https://web.archive.org/web/20140606142058/http://wiki.nginx.org/HttpCoreModule#.24sent_http_HEADER

参考: https : //stackoverflow.com/questions/12431496/nginx-read-custom-header-from-upstream-server

[个人说明:我不明白为什么,服务器故障,其他所有的都是单独的论坛,现在它很烦人。]

我想你想要一个location块像下面的东西:

 if ($sent_http_content_length ~ '') { more_clear_headers Content-Type ... } 

请参阅$ sent_http_name

(免责声明:我没有尝试过,我只是通过文档挖掘,因为我发现这个问题很有趣,这可能会也可能不行)

检查一些东西的不存在总是一个问题…但是…

我一般telnet到端口nginx正在聆听,并作出一个手工制作http repsonse:

 telnet www.stackoverflow.com 80 Trying 69.59.196.211... Connected to stackoverflow.com. Escape character is '^]'. GET /index.html HTTP/1.1 host: www.stackoverflow.com HTTP/1.1 301 Moved Permanently Server: nginx Date: Fri, 30 Jul 2010 14:31:02 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Location: http://stackoverflow.com/index.html Content-Length: 158 <head><title>Document Moved</title></head> <body><h1>Object Moved</h1>This document may be found <a HREF="http://stackoverflow.com/index.html">here</a></body> 

您也可以使用转储标题选项curl或wget。 例如wget支持:

  -S --server-response Print the headers sent by HTTP servers and responses sent by FTP servers. 

最后, 自定义日志logging可以让你添加特定的响应头,例如:

 log_format up_head '$remote_addr - $remote_user [$time_local] $request ' 'upstream_http_content_type $upstream_http_content_type'; 

有一个variables$content_length ,根据文档 等于请求的Content-Length头 。 你可能可以做

 if ($content_length = 0) { do stuff with header } 

我不确定$ content_length将会承担什么样的价值,或者即使头文件不存在,它也会存在,但是您可以使用它作为解决scheme的起点。