Proxy Retry Logic

Sirsiwal, Umesh usirsiwal at verivue.com
Thu Dec 23 04:51:12 MSK 2010


Thanks Marcus. 
I really like echo module. My module's subrequest handling is modeled after the excellent echo module. We have significant custom logic to deal with our specific needs. 

I did not realize try_files can use with proxy locations. I did see the internal redirect in the configuration example used proxy. But, I did not realize that I could use it in other places. 

I tried using
        location /sub1 {
           proxy_pass http://www.1google.com;
        }
        resolver 216.195.0.131;
        location /sub2 {
           proxy_pass http://www.google.com;
        }
        location @s {
           proxy_pass http://www.yahoo.com;
        }

       
         location / {
           try_files /sub1/$uri /sub2/$uri @s;
        }

I always get www.yahoo.com. Looking at the logs:
2010/12/22 20:44:55 [debug] 1607#0: *1 try to use file: "/sub1//" "/Users/umesh/nginx/install/html/sub1//"
2010/12/22 20:44:55 [debug] 1607#0: *1 http script copy: "/sub2/"
2010/12/22 20:44:55 [debug] 1607#0: *1 http script var: "/"
2010/12/22 20:44:55 [debug] 1607#0: *1 try to use file: "/sub2//" "/Users/umesh/nginx/install/html/sub2//"
This was followed by testing @s which served the content.

I tried replacing /sub1 and /sub2 with named location and I get:
2010/12/22 20:48:52 [debug] 1637#0: *3 try to use file: "@sub1" "/Users/umesh/nginx/install/html at sub1"
2010/12/22 20:48:52 [debug] 1637#0: *3 try to use file: "@sub2" "/Users/umesh/nginx/install/html at sub2"
This was followed by testing @s which served the content.

How can I configure try_files to use proxy_location?

-Umesh
________________________________________
From: Eugaia [ngx.eugaia at gmail.com]
Sent: Wednesday, December 22, 2010 8:19 PM
To: nginx-devel at nginx.org
Subject: Re: Proxy Retry Logic

Hi,

On 22/12/2010 21:26, Sirsiwal, Umesh wrote:
All,
I am trying to write a module which uses a number of subrequests to talk to backend servers. Combines the responses and sends them back to the client. The communication with the backend server is using HTTP and I am using proxy_pass for that.

The peculiarity in our setup is that on failure-retry I need to use a different URI than the original request. Current, proxy reinit request does not regenerate request hence cannot be used for the purpose. I tried using the error_page in the subrequest location to point to a different location. But, it seems that the error_page is not evaluated on subrequest. The only option I can think of is for my module to detect error and issue a new subrequest.

Is this the best solution for this problem? Will that affect subrequest response concatenation?
I think you can do this now using the echo module (http://wiki.nginx.org/HttpEchoModule), subrequests and try_files

e.g.

location    /main {
    echo_location    /sub1;    # could also be echo_location_async
    echo_location    /sub2;    # (ditto)
}

location    /sub1 {
    try_files ...
}

location    /sub 2 {
    try_files ...
}

Where ... in try_files are the separate proxy locations.

I'm not certain (and I've not tested / looked at the code) what happens when a proxied request returns a non-200 response, but I am assuming that try_files tries the next location on the list.  If it does (and it should if it doesn't), and you know the URL of the new testing location, then this method should work.

A word of caution about using both synchronous and asynchronous echo_ commands next to one another.  There used to be (and I'm not sure if they've been fixed yet or not) issues with this.  Use either sync or async combined and you should be fine (async is probably a better option, since it's likely to give a faster response to the client in this case).

Cheers,

Marcus.



More information about the nginx-devel mailing list