0

I am trying to set up 301 redirection for all the domains hosted on my Nginx server, and every attempt to get it to work has failed this far. My Nginx configuration is below.

File: /etc/nginx/sites-available/all-my-sites

# 301 Redirects
server {
    server_name www.example1.com;
    return 301 $scheme://example1.com$request_uri;
}

server {
    server_name www.example2.com;
    return 301 $scheme://example2.com$request_uri;
}

# The Real Deal
server {
    listen 80;
    server_name example1.com example2.com;

    [...]

}

I've also tried these configurations to no avail.

# 301 Redirects
server {
    listen 80;
    server_name www.example1.com;
    return 301 $scheme://example1.com$request_uri;
}

server {
    listen 80;
    server_name www.example2.com;
    return 301 $scheme://example2.com$request_uri;
}

# The Real Deal
server {
    listen 80;
    server_name example1.com example2.com;

    [...]

}

and

# 301 Redirects
server {
    listen 80 default_server;
    server_name www.example1.com;
    return 301 $scheme://example1.com$request_uri;
}

server {
    listen 80 default_server;
    server_name www.example2.com;
    return 301 $scheme://example2.com$request_uri;
}

# The Real Deal
server {
    listen 80 default_server;
    server_name example1.com www.example1.com example2.com www.example2.com;

    [...]

}

What am I doing wrong? I really have no clue. (I tried my best, googling and referring to the documentation.)

UPDATE: Actually, www.example2.com redirects to example2.com just fine. But www.example1.com doesn't. This is weird, and I still have no clue.

its_me
  • 225
  • 1
  • 7
  • 23
  • Is there a configuration error when you restart the nginx service ? – mimipc Feb 26 '13 at 15:36
  • @mimipc NO, none. It restarts/reloads just fine. In fact I also checked `sudo nginx -t` and it says everything's OK. – its_me Feb 26 '13 at 15:37
  • I checked with my nginx server. It didn't work so I replaced 301 with a simple 404, restarted, replaced 404 with 301 again, restarted, and it worked. I don't understand why... – mimipc Feb 26 '13 at 15:44
  • @mimipc Tried that. Neither did the `404` redirect work, nor did the `301` after it. :( – its_me Feb 26 '13 at 15:52
  • @mimipc Updated my question with more info. Please see if that makes sense. – its_me Feb 26 '13 at 16:23
  • Are you absolutely sure your HTTP request is reaching the good server ? – mimipc Feb 26 '13 at 16:33
  • @mimipc The problem is on the DNS-end, and I sure this time. Fixed it! – its_me Feb 26 '13 at 16:48

2 Answers2

0

I am assuming that you want to redirect your domains from www to non www.

If it is so you can try the following way, which i am already using in my nginx configuration.

    server {
        listen 80;
        server_name  www.example1.com;
        rewrite ^(.*) http://example1.com$1 permanent; 
}
sandeep.s85
  • 2,119
  • 1
  • 18
  • 27
  • Thanks for trying to help! (**PS:** `rewrite ^(.*) http://example1.com$1 permanent;` is BAD! Use `return 301 http://domain.com$request_uri;` instead. Read: http://wiki.nginx.org/Pitfalls) – its_me Feb 26 '13 at 16:03
0

The Nginx configuration is indeed correct. The problem's with my DNS configuration. I was using wildcard subdomain mapping for A and AAAA records on example1.com (e.g. *.example.com A 123.456.67.8), when it ALREADY has a TXT record for www.example1.com.

You can't map a wildcard subdomain when one of the subdomains for that domain already has a DNS record!

its_me
  • 225
  • 1
  • 7
  • 23