Setting up Django, Nuxt, with Nginx and docker - django

I am trying to create a docker image for all of the above services. I was able to get django set-up with a database and Nginx, but I am having a lot of trouble adding Nuxt to the mix and cannot get the Nuxt app to run properly and it is giving me constant errors that do not occur if I manually start the node server. I suspect that most of the issues are from auth and/or improper routing in Nginx, likely stemming from incorrectly setting up the appropriate files. Any advise on how to set-up these configuration files would be greatly appreciated!
My folder structure is as follows. The dockerfile inside the app directory corresponds to the django app
WebsiteProject/
├── app/
│ ├── frontend-nuxt/
| | ├──Dockerfile
| | └──nuxt.config.js
│ ├── nginx/
| | ├──Dockerfile
| | └──nginx_local.conf
| └──Dockerfile
└── docker-compose.yml
docker-compose.yml
version: '3.7'
services:
web:
build: ./app
command: gunicorn webapp.wsgi:application --bind 0.0.0.0:1339 --reload
entrypoint: ./entrypoint.sh
volumes:
- ./app/:/usr/src/app/
- static_volume:/usr/src/app/static
- media_volume:/usr/src/app/media
expose:
- 1339
env_file:
- ./.env.dev
depends_on:
- db
# - redis
frontend:
build: ./app/frontend-nuxt
volumes:
- ./app/frontend-nuxt/:/user/src/app/
expose:
- 3000
ports:
- 3000:3000
depends_on:
- web
command: npm run dev
db:
image: postgres:12-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=webapp
- POSTGRES_PASSWORD=webapp
- POSTGRES_DB=webapp_dev
nginx:
build: ./app/nginx
volumes:
- static_volume:/usr/src/app/static
- media_volume:/usr/src/app/media
ports:
- 8085:80
depends_on:
- web
volumes:
postgres_data:
static_volume:
media_volume:
nginx_local.conf
upstream webapp {
server web:1339;
}
upstream frontend {
server frontend:3000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server_name webapp.com localhost:8085;
location / {
proxy_pass http://frontend;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host:8085;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ /(api|admin|static|account-auth|accounts|api-auth)/ {
proxy_pass http://webapp;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host:8085;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static/ {
alias /usr/src/app/static/;
}
location /media/ {
alias /usr/src/app/media/;
}
}
Nuxt Dockerfile
FROM node:11.13.0-alpine
ENV HOST 0.0.0.0
ADD . /app
WORKDIR /app
EXPOSE 3000
RUN npm rebuild node-sass
nuxt.config.js auth and axios settings
axios: {
baseURL: 'http://localhost:8085/',
},
auth: {
strategies: {
local: {
endpoints: {
login: {
url: 'account-auth/token/login/',
method: 'post',
propertyName: 'auth_token',
},
logout: { url: 'account-auth/token/logout/', method: 'post' },
user: {
url: 'api/data/',
method: 'get',
propertyName: false,
},
},
tokenType: 'Token',
tokenName: 'Authorization',
},
},
redirect: {
login: '/accounts/login',
logout: '/',
callback: '/accounts/login',
home: '/'
},
},

Related

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

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.

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 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;

wsgi.url_scheme http in docker using nginx

I am using Apache on CentOS. On this server I have a Django project with docker. There are two containers in docker (nginx and python).
In the Apache I have .conf that have proxy to nginx container that is exposed on port 803. SSL is set in the Apache conf as well.
ProxyPreserveHost On
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Scheme "https"
ProxyPass / http://127.0.0.1:803/
ProxyPassReverse / http://127.0.0.1:803/
On the docker I have app.conf for nginx that looks like this:
upstream project {
server project-python:5000;
}
server {
listen 80;
server_name _;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
client_max_body_size 64M;
location / {
gzip_static on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Scheme "https";
proxy_set_header X-Forwarded-Proto "https";
proxy_set_header X-Forwarded-Protocol "ssl";
proxy_set_header X-Forwarded-Ssl=on;
proxy_set_header Host $host;
proxy_pass http://project;
proxy_redirect off;
}
}
In the Dockerfile Python is exposed on port 5000 and in the docker-compose.prod.yml file for production the python is started with gunicorn with this command:
gunicorn project.wsgi:application --preload --bind 0.0.0.0:5000
So I have two issues.
In the Django when I dump request.META I got wsgi.url_scheme that is http.
The second one is that I don't even understand how nginx is communicating with gunicorn because when I set app.conf to be just like below it is working also. How the nginx know that Python is exposed on port 5000.
server {
listen 80;
server_name _;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
client_max_body_size 64M;
location / {
proxy_pass http://project;
proxy_redirect off;
}
}
docker-compose.yml
version: '3'
services:
project-python:
build:
context: .
dockerfile: docker/python/Dockerfile
container_name: project-python
volumes:
- .:/var/www:rw
- .aws:/home/www/.aws
project-nginx:
build:
context: docker/nginx
dockerfile: Dockerfile
container_name: project-nginx
ports:
- "127.0.0.1:803:80"
depends_on:
- project-python
docker-compose.prod.yml
version: '3'
services:
project-python:
restart: unless-stopped
env_file:
- ./.env.prod
command: gunicorn project.wsgi:application --preload --bind 0.0.0.0:5000
expose:
- 5000
project-nginx:
restart: unless-stopped
environment:
APP_ENV: "production"
APP_NAME: "project-nginx"
APP_DEBUG: "False"
SERVICE_NAME: "project-nginx"

No connection with dockerized nginx gunicorn

I've created simple example for dockerized django with nginx and gunicorn. But for some reason connection doesn't go through nginx.
Here is nginx.conf contents
worker_processes 1;
error_log /var/log/nginx/error.log info;
events {
worker_connections 1024;
}
http {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name www.example.dj example.dj;
location = /favicon.ico {
alias /app/static_root/favicon.ico;
}
location /static/ {
alias /app/static_root/;
}
location / {
proxy_set_header Host $http_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-Proto $scheme;
proxy_pass http://unix:/app/example.sock;
}
}
}
docker-compose.yml file
version: '3'
services:
web:
build: .
hostname: web
command: bash -c "python manage.py migrate && gunicorn example.wsgi:application --workers 3 --bind unix:/app/example.sock"
volumes:
- ./src:/app
expose:
- "8000"
nginx:
image: nginx:latest
ports:
- "80:8000"
volumes:
- ./src:/app
- ./config/nginx/nginx.conf:/etc/nginx/nginx.conf
depends_on:
- web
In /etc/hosts I have
127.0.0.1 www.example.dj example.dj
So when I run docker-compose up I expect to see django start page by http://example.dj url, but there is no connection. Can you guys help me with it?
Code available on the github