# Proxy subdomain root to a different server url

**URL:** <https://www.ruby-forum.com/t/proxy-subdomain-root-to-a-different-server-url/219703>\
**Category:** NGINX\
**Created:** [May 12, 2012, 12:38pm UTC](https://www.ruby-forum.com/t/proxy-subdomain-root-to-a-different-server-url/219703 "2012-05-12T12:38:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gurdipe\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b77776/32.png) [@Gurdipe\_D](https://www.ruby-forum.com/u/Gurdipe_D)\
**Post date:** [May 12, 2012, 12:38pm UTC](https://www.ruby-forum.com/t/proxy-subdomain-root-to-a-different-server-url/219703/1 "2012-05-12T12:38:21Z")

</div>

Hi,

I need to alias a subdomain to contents of an url, e.g. I need  
[http://abcd.domain.com/](http://abcd.domain.com/) to proxy contents of  
[http://www.domain.com/path1/path2/xyz](http://www.domain.com/path1/path2/xyz)

All my attempts with proxy\_pass resulted in 301 to  
[http://abcd.domain.com/path1/path2/xyz](http://abcd.domain.com/path1/path2/xyz) and a 404 with strange duplicate  
uri /path1/path2/xyzpath1/path2/xyz

```
    server {
            listen 11.22.33.44:80;
            server_name abcd.domain.com;

            location / {
                    proxy_pass 

```

[http://www.domain.com/path1/path2/xyz](http://www.domain.com/path1/path2/xyz);  
}  
}

I’m clearly missing something, please help!

---

<div class="post-metadata">

**Author:** ![Vahan\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ce73a5/32.png) [@Vahan\_Y](https://www.ruby-forum.com/u/Vahan_Y)\
**Post date:** [May 12, 2012, 12:43pm UTC](https://www.ruby-forum.com/t/proxy-subdomain-root-to-a-different-server-url/219703/2 "2012-05-12T12:43:55Z")

</div>

Hi,

I think what you might need to set is ‘proxy\_redirect’. I’m running a  
similar setup which works this way.

Regards,  
Chris

Sent from BlackBerry Device

---

<div class="post-metadata">

**Author:** ![Vahan\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ce73a5/32.png) [@Vahan\_Y](https://www.ruby-forum.com/u/Vahan_Y)\
**Post date:** [May 12, 2012, 2:04pm UTC](https://www.ruby-forum.com/t/proxy-subdomain-root-to-a-different-server-url/219703/3 "2012-05-12T14:04:34Z")

</div>

Hi Chris,

I was missing $1 in the end of the uri so the rewrite works.  
Without it, it was being silently ignored and was creating a redirect  
loop.

For the mailing list archival purposes, here is what I ended up with:

```
    server {
            listen 11.22.33.44:80;
            server_name abcd.domain.com;

            location / {
                    rewrite ^(.*)$ /path1/path2/filename$1 break;
                    proxy_pass http://www.domain.com;
            }
            server_tokens off;
    }

```

Thanks again,  
Vahan

---

<div class="post-metadata">

**Author:** ![Vahan\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ce73a5/32.png) [@Vahan\_Y](https://www.ruby-forum.com/u/Vahan_Y)\
**Post date:** [May 12, 2012, 4:20pm UTC](https://www.ruby-forum.com/t/proxy-subdomain-root-to-a-different-server-url/219703/4 "2012-05-12T16:20:06Z")

</div>

proxy\_pass [http://www.domain.com/path1/path2/xyz/](http://www.domain.com/path1/path2/xyz/);  
slash (“/”) in the end.

Posted at Nginx Forum:

> **[Re: Proxy subdomain root to a different server url](https://forum.nginx.org/read.php?2,226328,226349#msg-226349)**
>
> Hi, I need to alias a subdomain to contents of an url, e.g. I need http://abcd.domain.com/ to proxy contents of http://www.domain.com/path1/path2/xyz All my attempts with proxy\_pass resulted in 301 to http://abcd.domain.com/path1/path2/xyz and a 404...

---

<div class="post-metadata">

**Author:** ![Vahan\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ce73a5/32.png) [@Vahan\_Y](https://www.ruby-forum.com/u/Vahan_Y)\
**Post date:** [May 12, 2012, 2:25pm UTC](https://www.ruby-forum.com/t/proxy-subdomain-root-to-a-different-server-url/219703/5 "2012-05-12T14:25:37Z")

</div>

On Sat, May 12, 2012 at 04:03:57PM +0400, Vahan Y. wrote:

> ```
> location / {
> rewrite ^(.*)$ /path1/path2/filename$1 break;
> proxy_pass http://www.domain.com;
> }
> server_tokens off;
> }
> 
> ```

This works without rewrites:

```
location / {
     proxy_pass http://www.domain.com/path1/path2/filename/;
}

```

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![Vahan\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ce73a5/32.png) [@Vahan\_Y](https://www.ruby-forum.com/u/Vahan_Y)\
**Post date:** [May 12, 2012, 4:33pm UTC](https://www.ruby-forum.com/t/proxy-subdomain-root-to-a-different-server-url/219703/6 "2012-05-12T16:33:43Z")

</div>

Damn, sometimes a single trailing slash can be a show-stopper 🙂
