Jasmine Lognnes
Jasmine Lognnes

Reputation: 7087

How to set nginx reverse proxy?

On landing.example.com:10000 have I a webserver that works fine, which is a Docker container that exposes port 10000. Its IP is 172.17.0.2.

What I would like is having a nginx reverse proxy on port 80, and send the visitor to different Docker containers depending on the URL they visit.

server {
    listen 80;
    server_name landing.example.com;

    location / {
        proxy_pass http://172.17.0.2:10000/;
    }

    access_log /landing-access.log;
    error_log  /landing-error.log info;
}

When I do this, I get 502 Bad Gateway and the log says

2016/04/14 16:58:16 [error] 413#413: *84 connect()
failed (111: Connection refused) while connecting to upstream, client:
xxx.xxx.xxx.xxx, server: landing.example.com, request: "GET / HTTP/1.1",
upstream: "http://172.17.0.2:10000/", host: "landing.example.com"

Upvotes: 8

Views: 20162

Answers (1)

MrE
MrE

Reputation: 20768

The server doesn't answer because it is not defined as an upstream.

try this:

upstream my_server {
   server 172.17.0.2:10000;
}

server {
   listen 80;
   server_name landing.example.com;
   location / {
      proxy_pass                  http://my_server;
      proxy_set_header            Host $host;
      proxy_set_header            X-Real-IP $remote_addr;
      proxy_http_version          1.1;
      proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header            X-Forwarded-Proto http;
      proxy_redirect              http:// $scheme://;
   }
}

Here you define the upstream server (your server by IP or hostname) and make sure to forward the headers too so the server answering knowns who to answer to.

Upvotes: 7

Related Questions