nginx rewriting location of permanent rewrites

Jan Thurau asked:

I am trying to redirect from mydomain.com to www.mydomain.com .. However, nginx is rewriting the location-header to $host.

server{
    server_name mydomain.de;
    set $root /srv/www/mydomain.de;

    listen 8080;

    root $root;
    index index.html index.htm;

    location / {
            rewrite ^(.*) http://www.google.de$1 permanent;
    }

    add_header      Cache-Control   private;
}
server_tokens off;


proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 2048 8k;

client_max_body_size 100M;
client_body_buffer_size 100M;
server_names_hash_max_size 1024;

That’s how my server-configuration looks like. But I am not redirected to google but stuck in a redirection loop. I do have lots of rewrite rules in another server-host (www.mydomain.de), but they seem not being related as this vhost isnt even used.

Error.log reads as follows:

2012/10/24 17:26:26 [notice] 2244#0: *175 "^(.*)" matches "/", client: 127.0.0.1, server: mydomain.de, request: "GET / HTTP/1.1", host: "mydomain.de"
2012/10/24 17:26:26 [notice] 2244#0: *175 rewritten redirect: "http://www.google.de/", client: 127.0.0.1, server: mydomain.de, request: "GET / HTTP/1.1", host: "mydomain.de"

Does anyone know how to solve my problem? I have absolutely no idea what’s going wrong.. Thank you.

Edit:
Not even this works:

server{
   server_name test.prod-web.hrm;
   listen 8080;

   location / {
           rewrite ^(.*) http://www.google.de$1 permanent;
   }
}

My answer:


I wouldn’t use rewrite for this at all.

Instead, try this:

return 301 $scheme://www.example.com$request_uri$is_args$args;

If this fails, you probably have something else in your environment causing the issue.


View the full question and any other answers on Server Fault.

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