Site icon Techolac – Computer Technology News

How to override content type with Nginx web server

I configured Nginx server. However, it is sending the wrong content type. I need to force Nginx to send specific “Content-Type: text/xml; charset=UTF-8“. How do I configure Nginx to override content type for given URL location?

Introduction – MIME is an acronym for Multipurpose Internet Mail Extensions (MIME). It is a standard that indicates the nature and format of a document, file, or assortment of bytes. All web browsers use the MIME type to determine how to process a URL. Hence, it is essential that Nginx send the correct MIME type in the response’s Content-Type header.

 

Adblock detected 😱
My website is made possible by displaying online advertisements to my visitors. I get it! Ads are annoying but they help keep this website running. It is hard to keep the site running and producing new content when so many people block ads. Please consider donating money to the nixCraft via PayPal/Bitcoin, or become a supporter using Patreon.

 

How to see Content-Type header

Use the following curl command:
curl -I url
curl -I curl -I https://techolac.com/wp-content/uploads/2019/06/nginx.gif

Sample outputs:

HTTP/2 200 
date: Mon, 21 Jan 2019 12:07:22 GMT
content-type: image/gif
content-length: 377
cache-control: max-age=315360000
...
.

The content-type: image/gif indicates that image and subtype is gif image.

How to find out default mine types in Nginx

Look for config file named mime.types inside nginx config directory:
# find /etc/nginx -name mime.types
Use cat command or vi command to view mime.types file:
vi /etc/nginx/mime.types
View and force nginx to send specific Content-Type

Override content type with Nginx web server

Say when I request atom.xml file, there is
content-type: text/xml
And I want it to be correct one as follows:
Content-Type: content-type: text/html; charset=UTF-8
Again, I used curl command:
curl -I https://www.cyberciti.biz/atom/atom.xml

To fix this update your mime.types file:
# vi /etc/nginx/mime.types
And make sure following config exists:
application/atom+xml atom;
Save and close the file. Reload/restart the Nginx service. For example, GNU/Linux user can run:
# systemctl reload nginx

How to force Nginx to send specific Content-Type

Another option is to add the following directly to config file:

types {
    application/atom+xml    atom xml;
}

types {
application/atom+xml atom xml;
}

Nginx override content type for URL

It is also possible to override content type for given URL pattern. For example, I edited /etc/nginx/domains/cyberciti.biz/default.conf and added the following in server context:

    ### force utf-8 and content type, good bots for SEO ##
    location = /atom/atom.xml {
        ## override content-type ##
        types { } default_type "application/atom+xml; charset=utf-8";
 
        ## override header (more like send custom header using nginx) #
        add_header x-robots-tag "noindex, follow";
    }

### force utf-8 and content type, good bots for SEO ##
location = /atom/atom.xml {
## override content-type ##
types { } default_type “application/atom+xml; charset=utf-8”; ## override header (more like send custom header using nginx) #
add_header x-robots-tag “noindex, follow”;
}

Save and close the file. Restart or reload nginx server:
# service nginx reload
Test it:
curl -I https://www.cyberciti.biz/atom/atom.xml

Conclusion

This page demonstrated how to overwrite default Content-Type in nginx. I strongly suggest that you read this page here and here for more information.