# Nginx rewrite infinite loop

**URL:** <https://www.ruby-forum.com/t/nginx-rewrite-infinite-loop/210047>\
**Category:** NGINX\
**Created:** [July 29, 2011, 6:33am UTC](https://www.ruby-forum.com/t/nginx-rewrite-infinite-loop/210047 "2011-07-29T06:33:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 29, 2011, 6:33am UTC](https://www.ruby-forum.com/t/nginx-rewrite-infinite-loop/210047/1 "2011-07-29T06:33:26Z")

</div>

Hi.

I tried to rewrite all non-www url to www url, but when i tried to  
access the site, i got the infinite loop. following is my config

server {  
server\_name [www.example.com](http://www.example.com);

}  
location / {  
…

rewrite ^ http://example.com$request\_uri? permanent;  
proxy\_pass [http://example.com](http://example.com)

}

but if i change to redirect to the www url with locale(like, en, zh\_CN),  
it works fine. following is the config.

server {  
server\_name [www.example.com](http://www.example.com);  
}

location / {  
…

rewrite ^ [http://example.com/zh\_CN$request\_uri](http://example.com/zh_CN$request_uri)? permanent;

}

location ^~ /zh\_CN {  
…

proxy\_pass [http://example.com](http://example.com)  
}

any help is appreciated.

Thank You

Steve

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [July 29, 2011, 8:06am UTC](https://www.ruby-forum.com/t/nginx-rewrite-infinite-loop/210047/2 "2011-07-29T08:06:56Z")

</div>

On Fri, Jul 29, 2011 at 11:33 AM, Steve Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> …
> 
> rewrite ^ http://example.com$request\_uri? permanent;  
> proxy\_pass [http://example.com](http://example.com)

do you want to proxy or rewrite? R

Also why do people like posting snippet of their config file? This one  
isn’t even a valid one with location block outside sever block.

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [July 31, 2011, 2:17am UTC](https://www.ruby-forum.com/t/nginx-rewrite-infinite-loop/210047/3 "2011-07-31T02:17:00Z")

</div>

hi

i would to have both rewrite and proxy. Rewrite is for redirecting  
non-www url to www urls, proxy\_pass is passing to my upstream block.

Thank You

Steve

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [July 31, 2011, 3:32am UTC](https://www.ruby-forum.com/t/nginx-rewrite-infinite-loop/210047/4 "2011-07-31T03:32:33Z")

</div>

On 29 Jul 2011 05h33 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi.
> 
> I tried to rewrite all non-www url to www url, but when i tried to  
> access the site, i got the infinite loop. following is my config
> 
> server {  
> server\_name [www.example.com](http://www.example.com);

> }  
> location / {  
> …
> 
> rewrite ^ http://example.com$request\_uri? permanent;  
> proxy\_pass [http://example.com](http://example.com)
> 
> }

> …
> 
> any help is appreciated.
> 
> Thank You
> 
> Steve

server {  
server\_name [www.example.com](http://www.example.com);

location / {  
proxy\_pass [http://example.com](http://example.com);  
}

}

server {  
server\_name \*.example.com;

```
rewrite ^ http://www.example.com$request_uri? permanent;

```

}

— appa
