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

Somehow, Google has indexed my site via IP address as well as by domain. E.g., as well as www.mysite.com/index.php

I want to 301 all those ip address urls to the appropriate hostname urls, but can't figure out how to do it in nginx.conf.

Thank you for your help...

share|improve this question
Neither of these answers work for me with the latest version of nginx. I just end up in an infinite redirect loop. I've even tried to regex the IP hoping it would stop future requests but to no avail. –  user100531 Nov 9 '11 at 23:14

3 Answers 3

Add another server block to your config file

server {
    rewrite .* http://www.mysite.com$request_uri permanent;
share|improve this answer
Nailed it. THANK YOU. –  Arron Dec 5 '09 at 21:05

if you have latest version of nginx:

server {
  listen 80 default;
  rewrite ^ http://mysite.com$request_uri permanent;
share|improve this answer
in this case default means bind to all ips i think, which might give you unexpected results? –  djangofan Nov 9 '11 at 23:23

The above 2 answers didn't work for me either and resulted in an infinite redirect loop. Adding the ip address to my server_name worked:

server {
    listen 80;
    server_name mydomain.com www.mydomain.com 67.20x.xxx.xx;
share|improve this answer

Your Answer


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