Computing Magazine

Nginx Index Inside Conditional Location

Posted on the 21 October 2016 by Akahgy

Description:

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.

Solution:

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 = “mysite.fr”) { set $country “fr”; }
if ($http_host = “mysite.es”) { set $country “es”; }
index /local/files/$country/index.html
}


Back to Featured Articles on Logo Paperblog