Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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

2 Answers 2

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; 
}
share|improve this answer
    
Thanks for trying to help! (PS: rewrite ^(.*) http://example1.com$1 permanent; is BAD! Use return 301 http://domain.com$request_uri; instead. Read: wiki.nginx.org/Pitfalls) –  its_me Feb 26 '13 at 16:03

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!

share|improve this answer
    
There was no way we could possibly have discovered this for you. –  Michael Hampton Feb 26 '13 at 20:19
    
@MichaelHampton I know. If only I had realized this earlier... :) –  its_me Feb 27 '13 at 4:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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