2
votes

I'm trying to set up nginx reverse proxy in a container to another container where my app is running. Here is my nginx.conf:

    daemon off;

    user  nginx;
    worker_processes  1;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;


    events {
        worker_connections  1024;
    }


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

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        sendfile        on;

        upstream appserver {
            server app:3000;
        }

        server {
            listen 80;
            server_name localhost;

            location / {
                proxy_pass http://appserver/;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;        
            }
        }
    }

My docker-compose.yml looks like this:

version: '3'
services:
  db:
    image: postgres
  redis:
    image: redis
  web:
    build: ./web
    image: web
    ports:
      - "8080:80"
  app:
    build: ./app
    image: app
    command: puma
    volumes:
      - ./app:/app
    expose:
      - "3000"
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
      - web

The Dockerfile for the reverse proxy simply copies the config file and starts the nginx service. Here's my issue:

When accessing localhost:8080 on the host's browser, nginx returns 502 Bad Gateway. The logs reveal "web_1 | 2017/02/26 22:55:15 [error] 12#12: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.25.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.53.53:3000/", host: "localhost:8080" web_1 | 172.25.0.1 - - [26/Feb/2017:22:55:15 +0000] "GET / HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-""

Now right away I'm thinking nginx can't access my app container, however, running "curl app:3000" within the "web" container returns the proper response. I can also access the app directly on port 3000 when the port is forwarded. So I feel like the issue is with my nginx.conf of how it's trying to access that resource. I've been banging my head against a wall on this for a while. Any ideas?

1
I don't know where "upstream: "127.0.53.53:3000"" is coming from in that error message from the logs. nslookup returns the proper ip for 'app' which is 172.25.0.*adam
Shouldn't your web container depends_on your app container, rather than the other way around? You might be seeing errors on boot.jkinkead
@jkinkead that was indeed the issue. Switching those around got it working right away. Thanks.adam

1 Answers

0
votes

Rewriting the docker-compose.yml as so elimates the issue by ensuring the application is started prior to the nginx reverse proxy.

version: '3'
services:
  db:
    image: postgres
  redis:
    image: redis
  web:
    build: ./web
    image: web
    ports:
      - "8080:80"
    depends_on:
      - app
  app:
    build: ./app
    image: app
    command: puma
    volumes:
      - ./app:/app
    expose:
      - "3000"
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis