0

I am trying to get some redirection done via nginx maps. I have separate server blocks for individual domains,[and their aliases], say:

  • www.example.fr
  • www.example.uk
  • www.example.nl

Each within its conf file, say: www.example.fr.conf for example.

In each server configuration file, I am setting up the redirection using maps, so there is a [redirection] map for each domain like so:

include france-redirects.conf;
server {
   server_name  www.example.fr example.fr;
   ...
   if ($redirect_uri) {
        return 301 $redirect_uri;
    } 
} 

the file: france-redirects.conf looks like this:

map $request_uri $redirect_uri {        
    ~/news https://www.example.com/france/latest-news;
    ~/about http://www.example.com/france/about;
}

And this works as expected: so that traffic to http://www.example.fr/news gets redirected to https://www.example.com/france/latest-news

However, when I create similar configurations for other domains. The problem starts and seems to be that I am not able to use the same map signature/arguments/variables:

map $request_uri $redirect_uri {}

repeated in other server configuration files. Nginx seems to select the first map block [not sure what the order is] and 'obeys' that only. So when I have:

include netherlands-redirects.conf;
server {
   server_name  www.example.nl example.nl;

   if ($redirect_uri) {
        return 301 $redirect_uri;
    } 
}

with the map file [netherlands-redirects.conf] like so:

map $request_uri $redirect_uri {        
    ~/news https://www.example.com/netherlands/latest-news;
    ~/about http://www.example.com/netherlands/about;
}

and I try curling to see the final redirect:

curl -Ls -o /dev/null -w %{url_effective} http://www.example.nl/news

it would return something like:

http://www.example.com/france/latest-news

I am trying to avoid lumping all redirects into one long file but rather split them based on domains. Is this not possible or am I going about it the wrong way? Hope my explanation makes sense. Please help. Thanks

  • All of your map statements are in the same http block. You will need to give them unique variable names. – Richard Smith May 15 at 19:59
  • Does you redirects differ only in first part (france, netherland, etc...)? – Alexey Ten May 16 at 7:12
  • @RichardSmith: Thanks. could you give me an idea how to go about this? How and where do I set the individual variables? – pi. May 16 at 8:44
  • @AlexeyTen: Thanks Alexey, the redirects would have both. i.e. identical [/news /latest-new] as well as those which would be unique to the domain. – pi. May 16 at 8:46
  • The variable $redirect_uri is global, so you define it three times. You need to use a unique target name in all of your map blocks. – Richard Smith May 16 at 8:49
0

map directive is global for all server blocks. So you have to use different variable names. E.g.

map $request_uri $redirect_uri_fr {        
    ~/news https://www.example.com/france/latest-news;
    ~/about http://www.example.com/france/about;
}

map $request_uri $redirect_uri_nl {        
    ~/news https://www.example.com/netherlands/latest-news;
    ~/about http://www.example.com/netherlands/about;
}

and use them corresponding server blocks:

include france-redirects.conf;
server {
   server_name  www.example.fr example.fr;
   ...
   if ($redirect_uri) {
        return 301 $redirect_uri_fr;
    } 
} 

include netherlands-redirects.conf;
server {
   server_name  www.example.nl example.nl;

   if ($redirect_uri) {
        return 301 $redirect_uri_nl;
    } 
}
  • This is precisely what I have done and confirmed to be the solution, based on what @Richard mentioned earlier. This works. Thanks – pi. May 16 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.