Take the 2-minute tour ×

I am attempting to create a 301 re-direct from one domain to another with nginx. Here is what I'm currently doing and it works as I want:

   server {
        listen      80;
        server_name domain.net;
        return 301 $scheme://www.domain.net$request_uri;

    server {
            listen       80;
            server_name  www.domain.net;

I'd like to modify the first server block to include a .co domain to also re-direct to www.domain.net:

   server {
        listen      80;
        server_name domain.net domain.co www.domain.co;
        return 301 $scheme://www.domain.net$request_uri;

However, when I type in www.domain.co, the site loads fine, but the address bar still shows www.domain.co instead of www.domain.net. Likewise, doing a curl -I www.domain.co shows a 200 instead of a 301. Omitting the www. and only typing domain.co sends me to www.domain.net. curl -I domain.co also returns a correct 301.

Any idea why this is not working when I add the www.? I've been trying for some time now and searching/trial-and-error have come up fruitless.

share|improve this question
Is it possible that the "www.domain.co" page is cached somewhere? Your browser or some cache in the middle may have cached the page and thus your browser really never hits the server to see the 301 redirect. –  snap Jul 30 '12 at 4:21
add comment

1 Answer

up vote 2 down vote accepted

Your nginx rewrite rules look fine.

The most likely cause of this issue is that your DNS records aren't yet up to date. Double check to ensure that you've pointed the DNS A records for domain.co and www.domain.co to the right address. If you've recently updated the DNS records, it may take additional time before the updates are propagated and the old records expire. This is reflected in the Time-To-Live (TTL) value in your DNS SOA record.

Finally, if you've updated your nginx configuration, you also need to reload/restart nginx.

share|improve this answer
Thanks for the quick reply. I set these up a while ago, so I just double checked the .co DNS settings and both are pointing to the same IP as www./domain.net. Pinging both .co names returns the proper IP. I also just tested pointing a different domain, www.domain2.com to www.domain.net and it worked. Now I'm really confused. –  Brian Jul 30 '12 at 3:02
In that case, it may help if you share the actual domain names in question. I don't see any obvious (or even non-obvious) issues with your nginx config as posted. –  Michael Hampton Jul 30 '12 at 3:04
Sure. I'm trying to point www.songmeanings.co to www.songmeanings.net -- the config is currently set up as I explained in my original post. –  Brian Jul 30 '12 at 3:08
Yep, the behavior is as you described. Are you quite sure you haven't put www.songmeanings.co into a different server block somewhere? Or that it isn't misspelled? And that you reloaded nginx after changing the configuration? –  Michael Hampton Jul 30 '12 at 3:10
Just checked the spelling again, and it looks good. Looking to see if I placed it in any other nginx conf files. I am fairly certain I didn't, but I won't rule it out without checking because at this point I'm not sure what else it could be. –  Brian Jul 30 '12 at 3:15
show 2 more comments

Your Answer


