Configuring Nginx to redirect different servers

Vojtěch asked:

I am trying to setup Nginx to redirect different domains to a single one – http://knyt.tl:

server {
  listen 81;
  server_name _;
  return 302 http://knyt.tl/;
  rewrite ^ http://knyt.tl permanent;
}

server {
    listen 81;
    server_name knyt.tl;        
}

However nginx just returns contents of the domain knyt.tl and the different domains are not redirected with neither return 302, nor rewrite ^.

I’ve also tried following:

server {
    listen 81;

    if ($host != 'knyt.tl') {
        rewrite ^ http://knyt.tl/ permanent;
    }
 }

But the clause seems to be completely ignored as well.

Only time I managed to get it running was when redirecting from www.knyt.tl to knyt.tl.

My answer:


You have nginx listening on port 81. But you have Apache actually answering requests on port 80. That’s where you need to look for the problem, since all of your incoming requests come in on port 80.


View the full question and answer on Server Fault.

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