Linux

How to override content type with Nginx web server

[ad_1]

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

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:

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

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.

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.

[ad_2]
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close