Des Cahill
Des Cahill

Reputation: 163

Docker compose nginx config setup to broadcast on port 8000

I am at the end of a project where I have an Nginx as a router in a docker container that works with a test node app on port 8000, but I need it to point to another Nginx Docker compose container which is setup to run a Django app. I don't think the second Nginx is configured right to run the app on port 8000, and am looking for any advise as this.

Basically I've been following two separate tutorials and this is where they meet.

Here are the files that I think you'll need but feel free to ask for anything additional.

docker-compose.prod.yml

version: '3.7'

services:
  web:
    build:
      context: ./dcahill
      dockerfile: Dockerfile.prod
    command: gunicorn dcahill.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/dchll/web/staticfiles
      - media_volume:/home/dchll/web/mediafiles
    expose:
      - 8000
    env_file:
      - ./.env.prod
    depends_on:
      - db
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db
  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/dchll/web/staticfiles
      - media_volume:/home/dchll/web/mediafiles
    ports:
      - 1337:80
    depends_on:
      - web

volumes:
  postgres_data:
  static_volume:
  media_volume:

nginx.conf (not he main router directing public traffic to 8000)

upstream dcahill.com {
    server web:8000;
}

server {

    listen 80;

    location / {
        proxy_pass dcahill.com;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        client_max_body_size 100M;
    }

    location /staticfiles/ {
        alias /home/dchll/web/staticfiles/;
    }

    location /mediafiles/ {
        alias /home/dchll/web/mediafiles/;
    }

}
server {

    listen 443 ssl;

    location / {
        proxy_pass dcahill.com;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        client_max_body_size 100M;
    }

    location /staticfiles/ {
        alias /home/dchill/web/staticfiles/;
    }

    location /mediafiles/ {
        alias /home/dchll/web/mediafiles/;
    }

}

My main nginx router config that works for the test node appis big but these are the only settings apart from the SSL and default html5 boilerplate:

# Redirect all HTTP traffic to HTTPS
server {
    listen 80;
    server_name www.dcahill.com dcahill.com;
    return 301 https://$host$request_uri;
}

# Listen for requests for dcahill on port 443 and return server open on port 8000
server {
listen 443 ssl;
server_name dcahill.com;

location / {
    proxy_pass http://111.222.111.222:8000;
}
}

# Listen for requests for dcahill on port 443 and return server open on port 8000
server {
    listen 443 ssl;
    server_name  www.dcahill.com;

    location / {
        proxy_pass http://111.222.111.222:8000;
    }
}

My test node script which runs fine is as follows

const http = require('http')
const port = 8000

const requestHandler = (request, response) => {
  console.log('received request')
  response.end('Hello from app1!')
}

const server = http.createServer(requestHandler)

server.listen(port, (err) => {
  console.log(`server is listening on ${port}`)
})

Upvotes: 2

Views: 12634

Answers (1)

doublesharp
doublesharp

Reputation: 27607

The example docker-compose config is mapping port 8000 to 80, and then the NGINX config is redirecting port 80 to 443 which is not being provided externally.

Your NGINX service needs to make port 80 and 443 into the container (it is listening on those ports).

  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/dchll/web/staticfiles
      - media_volume:/home/dchll/web/mediafiles
    ports:
      - 443:443
      - 80:80
    depends_on:
      - web

The NGINX service will then be reverse proxied to your app on port 8000 within the Docker container, but port 8000 will not be available externally.

Upvotes: 2

Related Questions