Nginx: How to serve different urls with different settings from one file

mime-typenginx

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.

Best Answer

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.