NGINX Docker reverse proxy not working on server (Works locally) - django

I am running a Django app on Docker with Nginx, the configuration works perfectly locally but on the server, it doesn't.
i get the error 502 Bad Gateway in the web page
and the logs I get
2023/01/13 07:55:56 [error] 28#28: *1 connect() failed (111: Connection refused) while connecting
to upstream, client: xx.xxx.xx.xxx, server: , request: "GET /favicon.ico HTTP/1.1", upstream:
"http://xxx.xx.x.x:8000/favicon.ico", host: "xx.xxx.xxx.xxx", referrer: "http://xx.xxx.xxx.xxx/"
This is how the configuration looks
Dockerfile
FROM nginx:1.23.3-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY ./default.conf /etc/nginx/conf.d/default.conf
default.conf
upstream apii {
server api:8000;
}
server {
client_max_body_size 20M;
listen 80;
location / {
proxy_pass http://apii;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
# location = /favicon.ico { access_log off; log_not_found off; }
location /api {
proxy_pass http://apii;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /realestateadmin {
proxy_pass http://apii;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /app/staticfiles/;
}
location /mediafiles/ {
alias /app/mediafiles/;
}
}
docker-compose.yml
version: "3.9"
services:
api:
build:
context: .
dockerfile: ./docker/local/django/Dockerfile
command: /start
volumes:
- .:/app
- static_volume:/app/staticfiles
- media_volume:/app/mediafiles
# ports:
# - "8000:8000"
expose:
- 8000
env_file:
- .env
depends_on:
- postgres-db
networks:
- real-estate
postgres-db:
image: postgres:12.0-alpine
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
networks:
- real-estate
nginx:
restart: always
depends_on:
- api
volumes:
- static_volume:/app/staticfiles
- media_volume:/app/mediafiles
build:
context: ./docker/local/nginx
dockerfile: Dockerfile
ports:
- "80:80"
networks:
- real-estate
networks:
real-estate:
driver: bridge
volumes:
postgres_data:
static_volume:
media_volume:
I have tried different things. I doesn't make sense to me.

Related

How to run django-channels + nginx + gunicorn + redis in docker?

I am trying run an django asgi app with nginx + gunicorn + redis in docker.so far my wsgi application is running by gunicorn smoothly but somehow my django channels consumers are not connecting.
Docker-Compose
version: '3.9'
services:
db:
image: postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=LiveSafer
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=demo1234
ports:
- "5432:5432"
web:
build: .
command: >
bash -c " python manage.py makemigrations &&
python manage.py migrate &&
gunicorn --bind 0.0.0.0:8000 LiveSafer.wsgi &&
daphne -b 0.0.0.0 -p 8001 LiveSafer.routing:application
"
volumes:
- .:/app
- static_files:/app/static
expose:
- 8000
ports:
- 8001:8001
links:
- redis
depends_on:
- db
nginx:
build: ./nginx/
ports:
- 80:80
volumes:
- ./nginx/conf.d/:/etc/nginx/conf.d/
- static_files:/home/app/static
depends_on:
- web
# database controller
adminer:
image: adminer:latest
restart: always
environment:
- ADMINER_DEFAULT_SERVER=db
- ADMINER_DESIGN=flat
- ADMINER_PLUGINS=tables-filter
ports:
- 8080:8080
redis:
image: redis:latest
command: ["redis-server", "--bind", "redis", "--port", "6379"]
volumes:
static_files:
db:
nginx
upstream django{
server web:8000;
}
upstream channels-backend {
server 127.0.0.1:8001;
}
server {
listen 80;
client_max_body_size 60M;
location / {
proxy_pass http://django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header HOST $host;
}
location /ws/ {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_pass http://127.0.0.1:8001;
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;
}
location /media/ {
alias /app/media/;
}
location /static/ {
alias /home/app/static/;
}
}
Terminal
lifvesafer-nginx-1 | 2022/12/20 07:00:56 [error] 31#31: *10 connect() failed (113:
No route to host) while connecting to upstream, client: 172.19.0.1, server: , request:
"GET /favicon.ico HTTP/1.1", upstream: "http://172.19.0.5:8000/favicon.ico", host:
"127.0.0.1", referrer: "http://127.0.0.1/"
now i am not understanding why this is happening and how do i solve this

Nginx: Connection refused after removing port bindings from docker-compose file

Basically, I am trying to get access to MariaDB on the website. I needed to run a docker container so that it looks as if its network requests originate from the instance on which it runs (AWS Ubuntu Instance). For that, I used network_mode: "host" within my docker-compose file for each container. Also, I had to comment out the port bindings since they are not compatible with host network mode. However, now I get:
Connection refused error. I believe I have to change something within the nginx config file which is project.conf, but I am not sure what.
This is the docker-compose file:
version: '3'
services:
app:
restart: always
build: ./app
#ports:
# - "8501:8501"
env_file:
- .env
command: streamlit run Main.py
network_mode: "host"
mariadb:
image: mariadb:10.9.3
#ports:
# - "3306:3306"
volumes:
- db_data:/var/lib/mysql
- db_conf:/etc/mysql/conf.d
environment:
MYSQL_ROOT_PASSWORD: "xxx"
MYSQL_DATABASE: "xxx"
MYSQL_USER: "xxx"
MYSQL_PASSWORD: "xxx"
MYSQL_ROOT_HOST: "xxx"
network_mode: "host"
nginx:
restart: always
build: ./nginx
#ports:
# - "80:80"
depends_on:
- app
- mariadb
volumes:
db_data:
db_conf:
And, this is the project.conf file:
server {
listen 80;
server_name helloworld-st-app;
location / {
proxy_pass http://app:8501/;
}
location ^~ /static {
proxy_pass http://app:8501/static/;
}
location ^~ /healthz {
proxy_pass http://app:8501/healthz;
}
location ^~ /vendor {
proxy_pass http://app:8501/vendor;
}
location /stream {
proxy_pass http://app:8501/stream;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;
}
}
I tried removing 8501 from the url, still nothing. Any help on this would be much appreciated. Thanks in advance.

Docker and Django Postgresql issue

I am currently learning how to implement docker with Django and Postgres DB. Everytime I tried I run the following command:
docker compose up --build -d --remove-orphans
Although I can see all my images are started. Every time I tried to open my Django admin site, I cannot sign in using already registered superuser credentials. In Postgres DB PGAdmin all the data that are created previously are stored and saved correctly. But after closing my computer an I start Docker compose up previously saved data's are not recognized by the new start up even if the data still visible in my Postgres DB. How can I make the data to be recognized at start up?
Here is my docker-compose.yml configurations:
version: "3.9"
services:
api:
build:
context: .
dockerfile: ./docker/local/django/Dockerfile
# ports:
# - "8000:8000"
command: /start
volumes:
- .:/app
- ./staticfiles:/app/staticfiles
- ./mediafiles:/app/mediafiles
expose:
- "8000"
env_file:
- .env
depends_on:
- postgres-db
- redis
networks:
- estate-react
client:
build:
context: ./client
dockerfile: Dockerfile.dev
restart: on-failure
volumes:
- ./client:/app
- /app/node_modules
networks:
- estate-react
postgres-db:
image: postgres:12.0-alpine
ports:
- "5432:5432"
volumes:
- ./postgres_data:/var/lib/postgresql
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
networks:
- estate-react
redis:
image: redis:5-alpine
networks:
- estate-react
celery_worker:
build:
context: .
dockerfile: ./docker/local/django/Dockerfile
command: /start-celeryworker
volumes:
- .:/app
env_file:
- .env
depends_on:
- redis
- postgres-db
networks:
- estate-react
flower:
build:
context: .
dockerfile: ./docker/local/django/Dockerfile
command: /start-flower
volumes:
- .:/app
env_file:
- .env
ports:
- "5557:5555"
depends_on:
- redis
- postgres-db
networks:
- estate-react
nginx:
restart: always
depends_on:
- api
volumes:
- static_volume:/app/staticfiles
- media_volume:/app/mediafiles
build:
context: ./docker/local/nginx
dockerfile: Dockerfile
ports:
- "8080:80"
networks:
- estate-react
networks:
estate-react:
driver: bridge
volumes:
postgres_data:
static_volume:
media_volume:
Here is Docker nginx default.conf file setup:
upstream api {
server api:8000;
}
upstream client {
server client:3000;
}
server {
client_max_body_size 20M;
listen 80;
location /api/v1 {
proxy_pass http://api;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /admin {
proxy_pass http://api;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /app/staticfiles/;
}
location /mediafiles/ {
alias /app/mediafiles/;
}
location /sockjs-node {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location / {
proxy_pass http://client;
proxy_redirect off;
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;
}
}
Your answer is highly appreciated!!!

Nginx support multiple hostnames

I am working on my django + nginx + docker-compose project
I want to access my site via ip and mysite.com
Problem -- ip url is working, but mysite.com returns error:
403 Forbidden Nginx
My code - docker-compose.yml
services:
django:
build: ./project # path to Dockerfile
command: sh -c "
sleep 3 && gunicorn --bind 0.0.0.0:8000 core_app.wsgi"
...
expose:
- 8000
env_file:
- ./.env
depends_on:
- db
nginx:
image: nginx:1.19.8-alpine
depends_on:
- django
env_file:
- ./.env
ports:
- "80:80"
volumes:
- ./project/nginx-conf.d/:/etc/nginx/conf.d
...
nginx-conf.conf
upstream app {
server django:8000;
}
server {
listen 80;
server_name 127.0.0.1 mysite.com www.mysite.com;
location / {
proxy_pass http://django:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /var/www/html/static/;
}
}
UPDATE
I was trying to replace proxy_pass http://django:8000; with proxy_pass http://app; but it didn't help
Value of proxy_pass is incorrect.
When you're referencing an upstream group, you've to pass the name of the group to proxy_pass.
In your case, the name of upstream group is "app". So the value of proxy_pass should look like this:
proxy_pass http://app;

Access Pgadmin4 in Production within dockerized django application

I'm not sure how much sense it would make, but I was learning docker to deploy Django app with Gunicorn + Nginx + AWS.
So far, it works fine, where I have unit tested it in production.
My question is how can I access pgAdmin4 now?
docker-compose.staging.yml
version: '3.8'
# networks:
# public_network:
# name: public_network
# driver: bridge
services:
web:
build:
context: .
dockerfile: Dockerfile.prod
# image: <aws-account-id>.dkr.ecr.<aws-region>.amazonaws.com/django-ec2:web
command: gunicorn djangotango.wsgi:application --bind 0.0.0.0:8000
volumes:
# - .:/home/app/web/
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
expose:
- 8000
env_file:
- ./.env.staging
networks:
service_network:
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env.staging.db
networks:
service_network:
# depends_on:
# - web
pgadmin:
image: dpage/pgadmin4
env_file:
- ./.env.staging.db
ports:
- "8080:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
depends_on:
- db
links:
- "db:pgsql-server"
environment:
- PGADMIN_DEFAULT_EMAIL=pgadmin4#pgadmin.org
- PGADMIN_DEFAULT_PASSWORD=fakepassword
- PGADMIN_LISTEN_PORT=80
networks:
service_network:
nginx-proxy:
build: nginx
# image: <aws-account-id>.dkr.ecr.<aws-region>.amazonaws.com/django-ec2:nginx-proxy
restart: always
ports:
- 443:443
- 80:80
networks:
service_network:
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- vhost:/etc/nginx/vhost.d
- /var/run/docker.sock:/tmp/docker.sock:ro
labels:
- "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy"
depends_on:
- web
nginx-proxy-letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
env_file:
- .env.staging.proxy-companion
networks:
service_network:
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- vhost:/etc/nginx/vhost.d
depends_on:
- nginx-proxy
networks:
service_network:
volumes:
postgres_data:
pgadmin-data:
static_volume:
media_volume:
certs:
html:
vhost:
I can access the django application through my domain name like xyz.example.com. I have just shown the docker-compose here.
Also within local I can access pgadmin4 via localhost:8080.
Is it possible to do it in production? If yes how?
I would be using AWS RDS for database, but for now my database is within docker container, so I'm thinking how to access it now?
I found some documentation.
https://www.pgadmin.org/docs/pgadmin4/development/container_deployment.html
The url to access your pgadmin page would be configured in nginx. This example:
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name _;
ssl_certificate /etc/nginx/server.cert;
ssl_certificate_key /etc/nginx/server.key;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
location /pgadmin4/ {
proxy_set_header X-Script-Name /pgadmin4;
proxy_set_header X-Scheme $scheme;
proxy_set_header Host $host;
proxy_pass http://localhost:5050/;
proxy_redirect off;
}
}
The important parts I am catching here are the location /pgadmin4/ redirecting to the localhost:5050. In your case, it would be localhost:8080.
It looks like in your other post you included your nginx config:
https://www.digitalocean.com/community/questions/no-live-upstream-while-connecting-to-upstream-jwilder-ngnix-proxy
upstream djangotango.meghaggarwal.com {
server web:8000;
}
server {
listen 80;
listen 443;
server_name djangotango.meghaggarwal.com
location / {
proxy_pass http://djangotango.meghaggarwal.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /home/app/web/static/;
add_header Access-Control-Allow-Origin *;
}
location /media/ {
alias /home/app/web/media/;
add_header Access-Control-Allow-Origin *;
}
}
I would suggest adding a section like :
location /pgadmin4/ {
proxy_set_header X-Script-Name /pgadmin4;
proxy_set_header X-Scheme $scheme;
proxy_set_header Host $host;
proxy_pass http://localhost:8080/;
proxy_redirect off;
}
It might not be the only configuration you need to add... I have only skimmed the documentation. I am sure the link may help you more if this doesn't do the trick.