How to override content type with Nginx web server

How to see Content-Type header

Use the following curl command:
curl -I url
curl -I curl -I https://www.cyberciti.biz/media/new/category/old/nginx.gif

Sample outputs:

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
How to override content type with Nginx on Linux
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/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";
    }

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

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

This content was originally published here.