Nginx – How to check for the existence of a response header in Nginx rules

httphttp-headersnginx

Setting up the rewriting rules for the request proved to be quite easy in Nginx. For the response, not so much (at least, not for me). I want to strip the Content-Type header from the response if the Content-Length header of the response isn't set. I have the NginxHttpHeadersMoreModule installed, so that should allow me to remove the header, but I can't seem to find a way to check for the existence of the Content-Length header of the response using a rule in Nginx's configuration. Any suggestions on how to do this would be most appreciated!

Best Answer

Looks like someone asked this on Stack Overflow, and there is a variable for each sent header called $sent_http_my_custom_header.

See https://web.archive.org/web/20140606142058/http://wiki.nginx.org/HttpCoreModule#.24sent_http_HEADER for the details.

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