Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
Someone did warn you that this is generally a bad idea, right? Omitting Content-Type in the HTTP response basically gives the recipient carte blanche to do whatever it wants, and you may not be able to predict that behavior. – Michael Hampton Aug 27 '12 at 8:48
up vote 1 down vote accepted

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

See for the details.


[Personal note: I don't really understand why SO, Server Fault, and all the rest are separate forums now, its rather annoying.]

share|improve this answer

I guess you want something like the following in a location block:

if ($sent_content_length ~ '') {
    more_clear_headers Content-Type

(Disclaimer: I haven't tried this; I just dug through the docs because I found the question interesting. This may or may not work)

share|improve this answer

There is a variable $content_length that according the documentation is equal to line Content-Length header of request. You probably can do

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

I am not sure what value $content_length will assume, or even if it will exist if the header is absent, but you may use that as a starting point to the solution.

share|improve this answer
This variable contains the Content-Length of the request, not the response. So unfortunately, it doesn't really help me. – Victor Welling Jul 30 '10 at 14:42
Oh you right... :\ – coredump Jul 30 '10 at 16:38

Well checking for the nonexistence of something is always a problem... but....

I generally telnet to the port nginx is listening on and make a hand crafted http repsonse:

telnet 80
Connected to
Escape character is '^]'.
GET /index.html HTTP/1.1

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
Content-Length: 158

<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="">here</a></body>

You can also use dump header options to curl or wget. For example wget supports:

       Print the headers sent by HTTP servers and responses sent by FTP

Lastly, custom logging might let you add specific response headers, for example:

log_format up_head '$remote_addr - $remote_user [$time_local]  $request '
  'upstream_http_content_type $upstream_http_content_type';
share|improve this answer
Oh I am not so sure this is what you wanted... are you looking to verify what you have done works or how to do it? – Kyle Brandt Jul 30 '10 at 14:48
Sorry, I guess I wasn't clear in my question. I want to know how to configure it, I already know how to see if it works ;) Thank you for your reply though! – Victor Welling Jul 30 '10 at 14:57
I probably just read too fast. – Kyle Brandt Jul 30 '10 at 15:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.