Computing Magazine

Nginx Index Inside Conditional Location

Posted on the 21 October 2016 by Akahgy


On a server that delivers multiple sites, I need to set up the location of a request depending on the host. For example I need to serve different text depending on the site language.


The only problem here is that inside the location declaration there cannot be a root or index defined inside an if clause. The way to do it is to set up a variable from the if clause and use it outside of the if:

location /subpage/ {
if ($http_host = “”) { set $country “fr”; }
if ($http_host = “”) { set $country “es”; }
index /local/files/$country/index.html

Back to Featured Articles on Logo Paperblog