Nginx Wildcard for try_files

SyphonxZA asked:

So I’m trying to setup a default nginx config file that can be deployed across numerous docker image with different website configs in each container. It is currently set as below:

server {
   listen 80 default_server;

   root /var/www/;
   index index.php index.html;

   server_name localhost;
   include php5-fpm.conf;

   location / {
           try_files $uri $uri/ /index.php?q=$request_uri;

   location /status {
           stub_status on;
           access_log   off;
           deny all;


The problem is that it doesn’t work for static sites that use index.htm or index.html

I can edit the location section like below and then the site will work. Is there any way to get nginx to use a wildcard, so index.* and it will use whichever index file it finds? index.* by itself does not work so not sure what needs to be added/removed…

       location / {
           try_files $uri /index.htm?q=$request_uri;

My answer:

You’re fixated on the wrong part of try_files.

The part relevant to what you want is $uri/, with the trailing slash, which you seem to have removed. This bit causes nginx to try all of the files listed in the index directive.

You should put this part back in try_files and then edit the index directive to include the filenames you want.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.