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.

How to see Content-Type header

Use the following curl command:

curl -I url
curl -I curl -I

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

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

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;

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/ 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";

Save and close the file. Restart or reload nginx server:

service nginx reload


This page demonstrated how to overwrite default Content-Type in nginx

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.