trying to get something like proxypassreverse going.
francis at daoine.org
Sat Aug 21 19:35:21 MSD 2010
"timuckun" <nginx-forum at nginx.us> wrote:
> I am still struggling to get this proxy going. I have followed example
> after example on the web and nothing seems to work. I have also read
> through the documentation on proxy_pass and proxy_redirect but I still
> can't make it work.
You probably have it working perfectly for the HTTP headers, which is what
both apache proxypassreverse and nginx proxy_redirect handle. What you
seem to want is a way to additionally modify the HTTP response body --
the main stuff that the backend application sends.
In order to do that, you'll likely need a body filter. But it's almost
certainly much much easier just to configure the backend application
not to need that filter.
> I want anything in the /foo path to be handled by backend
> http://localhost:3000 This almost works. Then I browse
> http://my.server.com/foo it sends the request to the proper backend but
> the stylesheets, images etc don't load. The reason is because the links
> from the backend point to /stylesheets, /images etc and nginx is not
> proxying these.
the browser asks nginx "get http://my.server.com/foo/"; nginx asks the
backend "get http://localhost:3000/"; the backend replies with some
headers (which may include the string "localhost:3000") and the body;
nginx sends the headers (possibly modified, possibly with the string
"localhost:3000" replaced with the string "my.server.com/foo") and the
body (probably unmodified) to the browser.
"the links from the backend" that you are concerned about are within
the html -- in the body, which nginx didn't touch.
Can you configure the backend (using something like a "baseurl" option)
to believe that it is installed at /foo, so that it will write the image
links as /foo/images?
Alternatively, can you configure or write the backend application never to
start local links with "/", but instead to use exactly the correct number
of "../"s so that all local links are relative to the starting
location? That would allow the browser to correctly create the correct
If that is impossible, then can you install the backend to be at
http://localhost:3000/foo? Again, that should get the internal links to
have the desired "local part" of the url.
If that is also impossible, then you may need to get into writing the
> Somehow I need nginx to append /foo to /stylesheets when they come from
If you can get localhost:3000 to generate the correct-for-you links,
it'll be easier.
> According to the documantation the proxy redirect is supposed to do this
> Let us suppose the proxied server returned line Location:
> The directive
> proxy_redirect http://localhost:8000/two/ http://frontend/one/;
> will rewrite this line in the form Location:
Assume that "/images" is a browseable directory on the backend. Then:
the browser asks nginx "get http://my.server.com/foo/images"; nginx
asks the backend "get http://localhost:3000/images"; the backend
replies with a HTTP redirect to "http://localhost:3000/images/" (or
possibly to just "/images/"); nginx rewrites that Location: header to be
"http://my.server.com/foo/images/" and sends it to the browser.
The browser then asks "get http://my.server.com/foo/images/"; nginx
asks "get http://localhost:3000/images/"; the backend replies with
the directory listing wrapped in html (typically); nginx sends that to
All works well, because no links written in the html body content were
> Surely there is an easy way to this pretty simple thing.
Arrange that the backend application generates the correct-for-you urls
directly. That way, nginx doesn't have to do any special processing of
the body content, and it can just do what it does best.
Good luck with it,
Francis Daly francis at daoine.org
More information about the nginx