Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have thousands of text files named like ABC. I'd like to access those files from different URL: - if it is ABC or ABC.txt I'd like to add default_type text/plain and serve that file - if it is ABC.html I'd like to add default_type text/html and serve that file (so that I have two different versions of the same file).

Is it possible to do that in nginx? The fact that one url could only be matched with one location directive causes me trouble in finding a "clean" way.

share|improve this question
I've modified the question to clarify that it should match a pattern. – jcisio Dec 8 '12 at 6:18

You should try to create a separate location for each file extension and use a internal redirect. It'll be something like that:

location /ABC.txt {
  add_header Content-Type text/plain;
  rewrite /ABC last;
location /ABC.html {
  add_header Content-Type text/html;
  rewrite /ABC last;
location /ABC {
  alias /var/www/ABC.txt

I don't promise it'll work because I have not tested it on a real nginx.

share|improve this answer
I could modify your settings to match a pattern with regular expression. However the tricky part is there is no content-type header if it is accessed without extension, and nginx uses "Content-Type: application/octet-stream". – jcisio Dec 8 '12 at 6:21
To add a content type even when file is accessed without extension just add one more location with add_header parameter. – Vladimir Smotesko Dec 8 '12 at 9:45
I tested and it did not work. I always get the text/plain, with or without .html – jcisio Dec 8 '12 at 10:24

nginx processes the suggested mimetype based on the URI. I tried the following combination and it works:

    root /usr/share/nginx/www;

    default_type text/plain;

    location = /abc.txt {
      alias /usr/share/nginx/www/abc;

    location /abc.html {
      alias /usr/share/nginx/www/abc;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.