nginx-proxy: Accessible on localhost but 502 through proxy

Hi, I think this could be potentially related to #490, but would appreciate if I could get someone to have a quick look at this.

Have my docker-compose file configured like this:

version: "2"

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "/etc/nginx/vhost.d"
      - "/usr/share/nginx/html"
      - "/var/run/docker.sock:/tmp/docker.sock:ro"
      - "./vhosts/mydomain.local:/etc/nginx/vhost.d/mydomain.local"
      - "./vhosts/mydomain.local:/etc/nginx/vhost.d/www.mydomain.local"

  mydomain:
    image: nginx:latest
    restart: always
    environment:
      - VIRTUAL_HOST=www.mydomain.local,mydomain.local
      - VIRTUAL_PORT=8000
    expose:
      - 8000
    ports:
      - 8000:80
    volumes:
      - ./mydomain.local/_site:/usr/share/nginx/html/

Now, if I try to access this via http://localhost:8000 I get the expected results. If I try to access it via http://mydomain.local I get 502 Bad Gateway.

The logs say:

nginx-proxy      | nginx.1    | 2016/12/30 12:36:53 [error] 27#27: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: mydomain.local, request: "GET / HTTP/1.1", upstream: "http://172.18.0.2:8000/", host: "mydomain.local"
nginx-proxy      | nginx.1    | mydomain.local 172.18.0.1 - - [30/Dec/2016:12:36:53 +0000] "GET / HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36"

As per @wader’s comment on this thread I tried to access my container to have a look as to whether it could access the upstream or not, and it doesn’t seem like it can.

root@fa1703cfa205:/app# curl -v http://172.18.0.2:8000
* Rebuilt URL to: http://172.18.0.2:8000/
* Hostname was NOT found in DNS cache
*   Trying 172.18.0.2...
* connect to 172.18.0.2 port 8000 failed: Connection refused
* Failed to connect to 172.18.0.2 port 8000: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 172.18.0.2 port 8000: Connection refused

Which makes me think I am probably missing some networking configuration that I wasn’t aware existed.

Would appreciate any help you can give me on this.

With kind regards,

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 21

Most upvoted comments

Hi, my guess is that the nginx:latest image listens on port 80 but in this compose config 8000 is exposed (which is just a hint) and VIRTUAL_PORT is used to tell nginx-proxy to use it. Try to remove the expose config and change VIRTUAL_PORT to 80.

As both services is in the same compose config they should have a shared network by default.

😄 Yes that is correct. The thing is that nginx-proxy forwards traffic to other container on internal docker networks so VIRTUAL_PORT should be the ports exposed/listened to by the container.

So mapping 8000 to 80 will be a port publish that will forward traffic without going thru nginx-proxy at all.

Maaaan you’re good! That’s exactly what it was…

Just for future reference, can you clarify why that is?

I checked nginx, and it does indeed default to port 80, but I understand that

ports:
      - 8000:80

Maps port 80 to 8000, so that’s accessible from outside the container on port 8000, and then that if I set - VIRTUAL_PORT=8000 I’m basically telling nginx-proxy to use that port. Is that not how it works?

Thanks again!