How to remove the "index.html" for the root and subfolders of a static site using NGINX. Note this is done in the server location block.

Scenario: When serving static pages, for example a @failover site via reverse proxy that is generated via a WGET like this (note it's all one line):

wget --limit-rate=555k --no-clobber --convert-links --restrict-file-names=windows --random-wait -r -p -E -e -U "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" https://www.example.org/

Inside of your server block in /etc/nginx/sites-available/ (by default) you can include a file. The default in this case is to locate the file in:

/etc/nginx/snippets/

by adding a file, perhaps creatively named as fixing-index.conf your file path looks like this:

/etc/nginx/snippets/fixing-index.conf

The contents of that file are:

location / {
if ( $request_uri ~ "/index.html" ) {
rewrite ^(|/(.*))/index\.html$ /$2 permanent;
}
}

location /index {
if ( $request_uri ~ "/index" ) {
rewrite ^(|/(.*))/index$ /$2 permanent;
}
}

Which you can then add to your /etc/nginx/sites-enabled/example-com inside of the server block with:

include /etc/nginx/snippets/fix-index.conf;

Other items of note, on the latest NGINX on Ubuntu 16.04 or later, you can't have a "locatioin" block in the /etc/nginx/conf.d/ folder as an include because it is processed in the /etc/nginx/nginx.conf http block as opposed to the server block in "sites-available". Hence the (new?) standard of using the /etc/nginx/snippets/ folder.

Credit to: https://stackoverflow.com/questions/5675743/nginx-rewrite-rule-to-re move-index-html-from-the-request-uri for having a useable answer that we could apply.

 

Contact us to upgrade to Tendenci

The open source solution chosen by associations around the world.

Want to talk? (281) 497-6567

Sign up for Tendenci - The Open Source AMS

No per user pricing. Unlimited admins.

Demo Now

Have Questions?

Contact us!

Site Search