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 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;
   }
}
share|improve this question
    
Seems reasonable (except for assigning $root, but that's another problem...) Did you clear your browser cache? –  Michael Hampton Oct 24 '12 at 20:30
    
I am assigning $root because I normally include a file called default.conf so I do not have to set some parameters again.. I tried it on different clients with different browsers, always the same problem :/ –  Jan Thurau Oct 24 '12 at 20:32
    
You don't have to do that. once you use the root directive, whatever you set is automatically assigned to $document_root :) –  Michael Hampton Oct 24 '12 at 20:32
    
oh okay, thanks. I tried out a new vhost (see last code block) .. Not even this works.. I tried both the latest stable and development release of nginx.. –  Jan Thurau Oct 24 '12 at 20:40
    
Okay, I finally solved the problem. I use POUND in front of my backend systems as a ssl-proxy. And pound is obviously automatically rewriting every location headers it receives by default. I disabled it (RewriteLocation 0) and now it works. Thank you all for you help! I can't self-answer my question now so I'm adding the solution as a comment. –  Jan Thurau Oct 24 '12 at 21:06

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer
    
Thank you for your answer. I tried it out, but I am still stuck in a redirection loop.. Not even an entry in the error.log (debug-mode).. My client just receives "Location: test.prod-web.hrm"; (same host as my vhost), but nginx config says return 301 google.de –  Jan Thurau Oct 24 '12 at 20:45
    
You have something else going on, then. Look at the rest of your environment. –  Michael Hampton Oct 24 '12 at 20:49

Just create an additional server for the rewrite:

   server {
      listen       192.168.1.234:80;
      server_name  example.com;                # your domain name

      rewrite ^/(.*) http://www.example.com/$1 permanent;
   }

That really should be it. Mind the forward slash you lack

share|improve this answer
1  
So no location stuff, just this.... check the nginx pitfalls page, I don't understand why you would set a $root var. –  Glenn Plas Oct 24 '12 at 20:50

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.