I’m trying to set up Nginx with my Docker containers. I want to route the standard listening port 80 to the port where my Docker runs my Django app.
server {
listen 80;
location / {
proxy_pass https://127.0.0.1:5000;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
docker-compose
proxies my app from port 5001 to 5000:
version: '3.7'
services:
app:
build:
context: ./app
dockerfile: Dockerfile
volumes:
- './app:/usr/src/app'
ports:
- 5001:5000
environment:
...
depends_on:
- db
...
nginx:
build:
context: ./app/nginx
dockerfile: Dockerfile
restart: always
ports:
- 80:80
depends_on:
- app
I’m not sure what I’m doing wrong.
If you navigate to localhost:5001
, the app works (it runs on port 5000).
But if you try localhost
without specifying a port, you hit a 502 error on Nginx.
Here is a guide to Nginx, but I haven’t found the solution to my problem yet.
Edit: I figured it out.
You need to give NGinx the name of your service.
server {
listen 80;
location / {
proxy_pass https://app:5000; // change this line
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}