Dynamic proxy_pass

W. Andrew Loe III andrew at andrewloe.com
Wed Jul 30 05:34:50 MSD 2008

I have a relatively complex setup but I believe nginx has the ability
to do everything I am looking for. I want to proxy requests for files
to different servers using x-accel-redirect.

A request cycle might look like this:
client => interwebs => nginx1 => mongrel (which does some lifting and
then replies back with x-accel-redirect and a url like
/under_the_covers/ => nginx1 (at this point the
location directive catch the /under_the_covers and do what I want) =>
some-other-webserver ( => nginx1 => interwebs => client.

In psuedo code I want something like:

location /under_the_covers/(.*) {
  proxy_pass http://$1;

I have tried a few methods but I cannot seem to get it correct.

location /under_the_covers/ {
  rewrite ^/under_the_covers/(.*)/(.*)$ $2;
  proxy_pass http://$1;

I have no trouble if I manually set the host in the proxy pass. So the
mongrel replies with x-accel-redirect = /under_the_covers/file.jpg and
location looks like so:

location /under_the_covers/(.*) {


There is discussion there on using proxy_pass with variables but I
cannot either construct the uri correctly or I can't get the hostname
set from a regular expression.

Thank you for your time,
-- Andrew

More information about the nginx mailing list