1

I have a docker-compose.yaml similar to this (shortened for simplicity):

# ...
services:
  my-ui:
    # ...
    ports:
    - 5402:8080
  networks:
  - my-net

networks:
  my-net:
    external:
      name: my-net

and I'm trying to set up nginx as a reverse proxy with this configuration:

upstream client {
  server my-ui:5402;
}

server {
  listen 80;

  location / {
    proxy_pass http://client;
  }
}

and this is the docker-compose.yaml I have for nginx:

# ...
services:
  reverse-proxy:
    # ...
    ports:
    - 5500:80
    networks:
    - my-net

networks:
  my-net:
    external:
      name: my-net

What happens now is that when I run my-ui and reverse-proxy (each using its own docker-compose up), and I go to http://localhost:5500, I get a Bad Gateway message, and my nginx logs says this:

connect() failed (111: Connection refused) while connecting to 
upstream, client: 172.19.0.1, server: , request: "GET / HTTP/1.1", 
upstream: "http://172.19.0.5:5402/", host: "localhost:5500"

If I exec into my nginx container and use ping:

ping my-ui
ping 172.19.0.5

Both are successful, but if I want to, for example, curl:

curl -L http://my-ui
curl -L http://my-ui:5402
curl -L http://172.19.0.1

All of them fail with connection refused message. What am I missing here?

PS: I'm not sure, but it might be useful to add that my-ui is a basic vuejs application, running on Webpack dev server.

PS2: I also tried passing host headers etc. but same result

3
  • Have you tried server my-ui:8080 in your nginx config? Commented Jan 10, 2019 at 5:36
  • Thank you so much @Mathias, I wasted 2:30 hours last night, trying to make it work, but I had no idea that I needed to go to the actual port in the container, and not the one I map in docker-compose. Could you explain why's that the case? And maybe, if you want, you can do that as an answer so that I can accept it Commented Jan 10, 2019 at 17:41
  • You are welcome. Great to hear it's working now. This are the small detail that cost you most of the time. I added an answer. Let me know in case you need more information. Commented Jan 10, 2019 at 21:07

1 Answer 1

2

The name of the container (my-ui) resolves to the IP of the container. Therefor you have to provide in upstream the port of the container and not the port you have mapped to the host.

upstream client {
  server my-ui:8080;
}

server {
  listen 80;

  location / {
    proxy_pass http://client;
  }
}

You could also configure your upstream with the name of your host machine and use the mapped port. (server <name of host>:5402) But this could get quite messy and you would lose the advantage of isolating services with docker networks.

Furthermore you could also remove the port mapping unless you need to access the webservice without reverse proxy:

# ...
services:
  reverse-proxy:
    # ...
    # ports:
    # - 5500:80
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.