Does Nginx honor DNS TTLs for proxy upstreams?

csg nginx-forum at nginx.us
Wed Sep 28 16:05:07 UTC 2011


Maxim Dounin Wrote:
-------------------------------------------------------
> Errors suggest you don't have $backend set, and
> resulting 
> proxy_pass tries to go to "http://" which is
> invalid.

Yup, I am just curious how to accomplish that if you want to perform
some rewriting before, for example to strip off some leading parts of
the URL path. I came up with following test case:

worker_processes                        3;

events {
    worker_connections                  1024;
}

http {
    server_tokens                       off;

    include                             nginx.mime.types;
    default_type                        application/octet-stream;

    sendfile                            on;
    keepalive_timeout                   15;

    server {
        listen                          0.0.0.0:90;
        server_name                     _;

        location                        / {
            root                        html;
            index                       index.html;
        }
    }

    server {
        listen                          0.0.0.0:91;
        server_name                     _;

        location                        / {
            rewrite                     ^/foo/(.*)$  /$1  break;

            proxy_pass                  http://localhost:90;
            proxy_redirect              off;
        }
    }

    server {
        listen                          0.0.0.0:92;
        server_name                     _;

        location                        / {
            rewrite                     ^/foo/(.*)$  /$1  break;

            resolver                    127.0.0.1;

            set                         $backend  "localhost:90";
            proxy_pass                  http://$backend;
            proxy_redirect              off;
        }

        error_log                       /var/log/nginx/error.log 
debug;
    }
}

The port 91 server is a reverse proxy to the port 90 server with static
upstream (DNS lookup only on startup/reload). I delivers happily the
index.html of the port 90 server, regardless if I request
http://localhost:91/ of http://localhost:91/foo/.

Bringing the resolver into play doesn't seem to be that straight forward
if you look at the port 92 reverse proxy. It works when no rewriting has
to be performed, i.e., http://localhost:92/ works. But if I want to
strip off the leading /foo/ URL path segment of the request
http://localhost:92/foo/.

2011/09/28 17:55:51 [notice] 1490#0: *2 "^/foo/(.*)$" matches "/foo/",
client: 127.0.0.1, server: _, request: "GET /foo/ HTTP/1.1", host:
"localhost:92"
2011/09/28 17:55:51 [notice] 1490#0: *2 rewritten data: "/", args: "",
client: 127.0.0.1, server: _, request: "GET /foo/ HTTP/1.1", host:
"localhost:92"
2011/09/28 17:55:51 [warn] 1490#0: *2 using uninitialized "backend"
variable, client: 127.0.0.1, server: _, request: "GET /foo/ HTTP/1.1",
host: "localhost:92"
2011/09/28 17:55:51 [error] 1490#0: *2 invalid URL prefix in "http://",
client: 127.0.0.1, server: _, request: "GET /foo/ HTTP/1.1", host:
"localhost:92"

Any hints how to solve this the easiest way? Basically me requirement is
to reverse proxy http://proxy.example.com/foo/bar/quux to
http://upstream.example.com/bar/quux  (so strip off "foo/") while the IP
of upstream.example.com can change over time without prior announcement.

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,215830,215979#msg-215979



More information about the nginx mailing list