# Reverse proxy : preserving base url

**URL:** <https://www.ruby-forum.com/t/reverse-proxy-preserving-base-url/228221>\
**Category:** NGINX\
**Created:** [April 14, 2013, 4:36am UTC](https://www.ruby-forum.com/t/reverse-proxy-preserving-base-url/228221 "2013-04-14T04:36:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mastah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@mastah](https://www.ruby-forum.com/u/mastah)\
**Post date:** [April 14, 2013, 4:36am UTC](https://www.ruby-forum.com/t/reverse-proxy-preserving-base-url/228221/1 "2013-04-14T04:36:06Z")

</div>

Hi.

I’m struggling since about 2days to create a reverse proxy with nginx.  
Here is my situation :  
I’ve a sub domain which we will call : [sub.example.com](http://sub.example.com)  
I’ve an internal application at 127.0.0.1:9090/app

What I would like :  
I would like to proxy [sub.example.com](http://sub.example.com) to 127.0.0.1:9090/app and i  
want to  
keep my URL as [sub.example.com](http://sub.example.com)  
So far I’ve done countless try without success.

The optimal situation would be that when I do :  
[http://sub.example.com/](http://sub.example.com/) I get the content from  
[http://127.0.0.1:9090/app](http://127.0.0.1:9090/app)  
but without any change in the sub domain URL.

So for example if the internal app has  
[http://127.0.0.1:9090/app/magical.css](http://127.0.0.1:9090/app/magical.css), my sub domain should be  
[http://sub.example.com/magical.css](http://sub.example.com/magical.css) (without app/)

Thanks in advance to anyone that is willing to help me.

Posted at Nginx Forum:

> **[Reverse proxy : preserving base url](https://forum.nginx.org/read.php?2,238350,238350#msg-238350)**
>
> Hi. I'm struggling since about 2days to create a reverse proxy with nginx. Here is my situation : I've a sub domain which we will call : sub.example.com I've an internal application at 127.0.0.1:9090/app What I would like : I would l

---

<div class="post-metadata">

**Author:** ![mastah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/67e7ee/32.png) [@mastah](https://www.ruby-forum.com/u/mastah)\
**Post date:** [April 15, 2013, 8:06pm UTC](https://www.ruby-forum.com/t/reverse-proxy-preserving-base-url/228221/2 "2013-04-15T20:06:03Z")

</div>

On Sat, Apr 13, 2013 at 10:35:47PM -0400, mastah wrote:

Hi there,

> I’ve a sub domain which we will call : [sub.example.com](http://sub.example.com)  
> I’ve an internal application at 127.0.0.1:9090/app
> 
> What I would like :  
> I would like to proxy [sub.example.com](http://sub.example.com) to 127.0.0.1:9090/app and i want to  
> keep my URL as [sub.example.com](http://sub.example.com)  
> So far I’ve done countless try without success.

There are two distinct parts to this.

You have the http headers, and you have the body (which will be html or  
css or jpg or something else).

nginx can reasonably be expected to adjust the response http headers.

nginx can’t reasonably be expected to adjust the response body.

Unless you put in special configuration, nginx with proxy\_pass will  
probably adjust the response headers for you.

You can try to adjust the body. You will have to match every string  
that your client browser will interpret as a url, and make sure that it  
works when the browser considers it relative to the non-/app/ url that  
it started with.

> The optimal situation would be that when I do :  
> [http://sub.example.com/](http://sub.example.com/) I get the content from [http://127.0.0.1:9090/app](http://127.0.0.1:9090/app)  
> but without any change in the sub domain URL.
> 
> So for example if the internal app has  
> [http://127.0.0.1:9090/app/magical.css](http://127.0.0.1:9090/app/magical.css), my sub domain should be  
> [http://sub.example.com/magical.css](http://sub.example.com/magical.css) (without app/)

The cleanest way to achieve this is for you to configure your  
application  
either (a) to believe that it is installed at “/”, not at “/app/”; or  
(b) to never create any links that start with “/”.

In each of those cases, the browser should interpret relative links  
correctly without nginx having to modify any body content.

(If you use option (b) and create links like “../../../app/other/place”,  
then that will break this. So don’t do that.)

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
