Dynamic proxy_pass

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/10.10.1.2/file.jpg) => nginx1 (at this point the
location directive catch the /under_the_covers and do what I want) =>
some-other-webserver (10.10.1.2) => nginx1 => interwebs => client.

In psuedo code I want something like:

location /under_the_covers/(.*) {
internal;
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/(.*) {
proxy_pass http://10.10.1.2;
}

http://article.gmane.org/gmane.comp.web.nginx.english/6206/match=proxy_pass

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

Here is some code maybe will help you…:slight_smile:

        if ($host ~* (.+)\.(.*)\.91\.com(.*))
        {
             set $dir_a $1;
             set $dir_b $2;

             rewrite ^(.*)$ /activity/$dir_b/$dir_a$1 last;
        }


        location /
        {
            proxy_pass      http://127.0.0.1:8080;
        }

I believe this is also a need for a mod_mogilefs to be able to do
dynamic upstream reproxying.

On Jul 29, 2008, at 6:34 PM, “W. Andrew Loe III”

I’m note sure how this helps me. I need to extract my host parameter
out of my request’s path
(/under_the_covers/host_I_want_to_pass_to/path) and not out of the
$host variable (or maybe I am missing something?).

cool!

so http://1.2.3.4/foo.jpg is actually the source file then?

For those interested I have a working solution.

location /under_the_covers/ {
if ($uri ~* ^/under_the_covers/(.)/.$) {
set $other_nginx $1;
rewrite ^/under_the_covers/./(.)$ $1;
proxy_pass http://$other_nginx/$uri;
break;
}
}

My client replies with X-Accel-Redirect =
/under_the_covers/1.2.3.4/foo.jpg and my other webserver gets the
request.

Yes, a webserver running on that IP get a request (that looks like it
came from the client) for foo.jpg, but the client will get its request
answered by the server it connected to. It basically lets you combine
webservers to create an ad-hoc cloud storage system.

On 8/1/08, W. Andrew Loe III [email protected] wrote:

Yes, a webserver running on that IP get a request (that looks like it
came from the client) for foo.jpg, but the client will get its request
answered by the server it connected to. It basically lets you combine
webservers to create an ad-hoc cloud storage system.

(or almost use it in combination with mogilefs!)

mod_mogilefs would be ideal as it wouldn’t require fastcgi/php or any
other scripting level language. but nginx would have to talk to a
tracker and resolve the URL to a key.

however, if X-Accel-Redirect: accepts another http:// URL that would
meet 99% of the need for mogilefs integration. i don’t believe it does
right now. could that be implemented easily?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs