Offline reverse-proxy with Nginx

user157148 asked:

I’m using a Nginx reverse proxy to access content on an online drupal server.
The fact is, if Nginx loses his internet connection, when i’m trying to call the server through the reverse proxy, I expect him to send me back an error code saying that he didn’t manage to connect the distant server and timed out, but the only thing I got back is a 302 code, so not catchable using proxy_intercept_errors on;.

What I want is : if Ngnix can’t access the server for any reason (404, 50x, simply no internet), I want him to send me back a nice, specific error page (this page is correctly sent back if error code 400 or higher, but not if Nginx doesn’t have access to internet).

The conf i’m using is :

# Reverse proxy for remote hosts
# regex capture, $1 is hostname $2 is the rest of the URI
location ~* /reverseproxy/([^/]*)(.*) {

    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    proxy_redirect off;
    proxy_buffering off;
    proxy_set_header        Host    $1;

    proxy_cache_bypass 1;
    proxy_no_cache 1;
    proxy_intercept_errors on;

    # Tranform custom X-Cookie into regular Cookie header
    set $cookieValue "";
    if ($http_x_cookie) {
        set $cookieValue $http_x_cookie;
    proxy_set_header Cookie $cookieValue;

    # CORS headers
    proxy_hide_header Access-Control-Allow-Origin;
    proxy_hide_header Access-Control-Allow-Methods;
    proxy_hide_header Access-Control-Allow-Headers;
    proxy_hide_header Access-Control-Allow-Credentials;

    add_header  Access-Control-Allow-Credentials true;
    add_header  Access-Control-Allow-Origin http://localhost:8080;
    add_header  Access-Control-Allow-Methods 'OPTIONS, GET, POST';
    add_header Access-Control-Allow-Headers 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control, Cookie';

    send_timeout 10s;
    proxy_connect_timeout 5s;

    proxy_pass http://$1$2$is_args$query_string;

Exemple of an answer given when internet was down : – – [01/Feb/2013:14:54:02 +0100] “GET /reverseproxy/ >HTTP/1.1” 302 160 “-” “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like >Gecko) Chrome/24.0.1312.57 Safari/537.17”

Thanks in advance,

My answer:

It looks like you are trying to create a normal forward proxy, not a reverse proxy. Unfortunately, as you’ve already discovered, nginx is poorly suited for working as a forward proxy. Instead, use software which is designed for this purpose, such as squid or wwwoffle.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.