Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

    set $root /srv/www/;

    listen 8080;

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

    location / {
            rewrite ^(.*)$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 (, 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:, server:, request: "GET / HTTP/1.1", host: ""
2012/10/24 17:26:26 [notice] 2244#0: *175 rewritten redirect: "", client:, server:, request: "GET / HTTP/1.1", host: ""

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

Edit: Not even this works:

   listen 8080;

   location / {
           rewrite ^(.*)$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
up vote 0 down vote accepted

I wouldn't use rewrite for this at all.

Instead, try this:

return 301 $scheme://$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:"; (same host as my vhost), but nginx config says return 301 – 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 {
      server_name;                # your domain name

      rewrite ^/(.*)$1 permanent;

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

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


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.