nginx add_header is not working

I'm using nginx-1.7.0 on windows 7. I am trying to use add_header to get to a json file I have residing in /data/TESTFOLDER.

This is what my location block looks like:

    location /data/TESTFOLDER/ {
        add_header 'Content-Type' 'application/json';
        autoindex on;
    }

When I run my website, where in my javascript I have a jquery ajax request to get json files from this folder, firebug gives me this information:

ResponseHeaders
Connection  keep-alive
Content-Type    text/html
Date    Wed, 09 Jul 2014 14:01:29 GMT
Server  nginx/1.7.0
Transfer-Encoding   chunked

Request Headers
Accept  application/json
Accept-Encoding gzip, deflate
Accept-Language en-us,is;q=0.7,en;q=0.3
Content-Type    application/json
Host    localhost
Referer http:// local host/index.html
User-Agent  Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0
X-Requested-With    XMLHttpRequest

so you see the content-type hasn't changed as I would like it to. I have no add_header's elsewhere in my config file and the default is set to octet_stream like it should be.

A couple of things to note:

  • The request "works", that is, I can access local host/data/TESTFOLDER/ from my browser
  • mime.types are included in my config file
  • mime.types contains application/json json;
  • my error log has no errors about this

Also, I'm very new to nginx and web servers in general, so please make your explanations simple to understand. :)


Solution 1:

add_header does not replace existing header but adds a new one, possibly leading to duplicate that might be filtered when it is supposed to be unique and/or only the first one of its type is read.

Another possibility is that the HTTP status code from your response does not match the list defined in the docs for this directive, since you did not use the always parameter. The text/html you see might then be nginx' default 404 page.

Anyhow, what you want to use for your need is the default_type directive:

location /data/TESTFOLDER/ {
    default_type application/json;
    autoindex on;
}