dockernginxssi

How do I map a location to an upstream server in Nginx?


I've got several Docker containers acting as web servers on a bridge network. I want to use Nginx as a proxy that exposes a service (web) outside the bridge network and embeds content from other services (i.e. wiki) using server side includes.

Long story short, I'm trying to use the configuration below, but my locations aren't working properly. The / location works fine, but when I add another location (e.g. /wiki) or change / to something more specific (e.g. /web) I get a message from Nginx saying that it "Can't get /wiki" or "Can't get /web" respectively:

events { 
    worker_connections 1024; 
}

http {
    upstream wiki {
        server wiki:3000;
    }

    upstream web {
        server web:3000;
    }

    server {
        ssi on;

        location = /wiki {
            proxy_pass http://wiki;
        }

        location = / {
            proxy_pass http://web;
        }
    }    
}

I've attached to the Nginx container and validated that I can reach the other containers using CURL- they appear to be working properly.

I've also read the Nginx pitfalls and know that using hostnames (wiki, web) isn't ideal, but I don't know the IP addresses ahead of time and have tried to counter any DNS issues by telling docker-compose that the nginx container depends on web and wiki.

Any ideas?


Solution

  • You must turn proxy_pass http://wiki; to proxy_pass http://wiki/;.

    As I know, Nginx would take two different way with/without backslash at the end of uri. You may find more details about proxy_pass directive on nginx.org.
    In your case, a backslash(/) is essential as a uri to be passed to server. You've already got error message "Can't get /wiki". In fact, this error message means that there is no /wiki in server wiki:3000, not in Nginx scope.
    Getting better knowing about proxy_pass directive with/without uri would help you much.
    I hope this would help.