6

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!

1
  • 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. Aug 27, 2012 at 8:48

4 Answers 4

1

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

1

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

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

See $sent_http_name

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

1
  • if only applies to incoming client requests, you can't use upstream response headers in conditions because the response hasn't been received yet. Oct 12, 2021 at 12:06
0

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 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>

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

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

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';
3
  • 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? Jul 30, 2010 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! Jul 30, 2010 at 14:57
  • I probably just read too fast. Jul 30, 2010 at 15:09
0

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.

3
  • This variable contains the Content-Length of the request, not the response. So unfortunately, it doesn't really help me. Jul 30, 2010 at 14:42
  • Oh you right... :\
    – coredump
    Jul 30, 2010 at 16:38
  • Right now, documentation link to wiki returns 404, good urls don't change they say :( Also note that nginx says that if is evil you have been warned ;)
    – GabLeRoux
    Apr 14, 2017 at 14:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .