nginx django static cannot fully serve my files(403 permission denied) - python

I've been dealing with nginx for about 1 week. I have 4 services that I set up with docker, django postgresql fastapi and nginx, but nginx does not serve django's static files. I'm facing 403 error. I tried the solutions of all similar error fields, but it doesn't work (file permission granting file path control) below I am sharing the files I use please help.
docker-compose.yml:
django_gunicorn:
build: .
command: gunicorn sunucu.wsgi:application --bind 0.0.0.0:7800 --workers 3
volumes:
- ./static:/root/Kodlar/kodlar/static
env_file:
- .env
environment:
- DATABASE_URL="**"
ports:
- "7800"
depends_on:
- db
nginx:
build: ./nginx
volumes:
- ./static:/root/Kodlar/kodlar/static
ports:
- "80:80"
depends_on:
- django_gunicorn
volumes:
static_files:
Django Docker File:
FROM python:3.8-slim-buster
WORKDIR /app
ENV PYTHONUNBUFFERED=1
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
RUN python manage.py migrate --no-input
RUN python manage.py collectstatic --no-input
CMD ["gunicorn", "server.wsgi:application", "--bind", "0.0.0.0:7800","--workers","3"]
Django Settings py:
STATIC_URL = '/static/'
STATIC_ROOT = '/root/Kodlar/kodlar/static/'
DEBUG = False
Nginx Docker File:
FROM nginx:1.19.0-alpine
COPY ./default.conf /etc/nginx/conf.d/default.conf
Nginx Conf File:
upstream django {
server django_gunicorn:7800;
}
server {
listen 80;
server_name mydomain.com;
error_page 404 /404.html;
location = /404.html {
root /root/Kodlar/kodlar/templates;
internal;
}
if ($host != 'mydomain.com') {
return 404;
}
location / {
proxy_pass http://django;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static/ {
alias /root/Kodlar/kodlar/static/;
}
}
I want my django service to run actively with gunicorn and serve static files in nginx

Related

django and nginx is not working on docker

i am a beginner in the docker and i want to deploy my django project
using nginx and postgres on vps using docker so I create a dockerfile and docker-compose but it is not working it means that postgres is on the port but django and nginx is not working i don't have any idea can you help me
my dockerfile
FROM python:3.8-slim-buster
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update && apt-get install -y build-essential libpq-dev
RUN rm -rf /var/lib/apt/lists/*
COPY . .
RUN pip install --upgrade pip && pip install -r requirements.txt
my docker compose
version: '3.8'
services:
database:
container_name: database
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
volumes:
- postgres:/var/lib/postgresql/data
restart: always
app:
build:
context: .
container_name: django-app
command: >
sh -c "python3 manage.py migrate &&
gunicorn config.wsgi:application --bind 0.0.0.0:8000"
volumes:
- static:/usr/src/app/static
- media:/usr/src/app/media
depends_on:
- database
environment:
- DEBUG=False
- ALLOWD_HOST=*
- DATABASE-NAME=postgres
- DATABASE-USER=postgres
- DATABASE-PASSWORD=postgres
- DATABASE-HOST=database
- DATABASE-PORT=5432
nginx:
image: nginx
container_name: nginx
ports:
- "80:80"
volumes:
- ./nginx:/etc/nginx/conf.d
- static:/var/www/static
- media:/var/www/media
volumes:
postgres:
static:
media:
I presume you want to serve Django static files in Nginx (reverse proxy feature in that case).You lack binding Nginx with Gunicorn served port and static file catalogue here.
So you will need to configure nginx to do that in the conf file.
For example. file default.conf
upstream backend {
server app:8000;
}
server {
listen 80;
# Django admin (if implemented)
location /admin {
proxy_pass http://backend;
autoindex off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
# Django static assests.
location /static/ {
autoindex off;
alias /usr/src/app/staticfiles/; # if thats where you copied your app files in docker container
}
Also will need static files to be configured in settings.py
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
static files regards catalogue in project root.
You will also have to make some dirs in Dockerfile for Django before you will copy project - in order to avoid error in collectstatic.
$WORKDIR your_workdir_path_in_docker_cotainer
# Make static files dirs in order to avoid error from collectstatic.
RUN mkdir $WORKDIR/staticfiles && \
mkdir $WORKDIR/staticfiles/admin && \ # if you implemented - to keep css/js
mkdir $WORKDIR/staticfiles/rest_framework #if you using DRF - to keep css/js
Last thing is to add add collectstatic command in you Docker-compose section where Gunicorn is started like.
command: >
sh -c "python manage.py migrate --noinput &&
python manage.py collectstatic --no-input &&
gunicorn config.wsgi:application --bind 0.0.0.0:8000"
Maybe its worthy to create separate Dockerfile for Nginx
FROM nginx:1.21-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY your_path_for_file/default.conf /etc/nginx/conf.d
CMD ["nginx", "-g", "daemon off;"] # To start Nginx
You can start that aditional container in Docker-compose by specifying path to Dockerfile
dockerfile: your_path_to_nginx_dockerfile/Dockerfile.nginx

Can't serve Django static files with NGINX

I'm trying to deploy a Django and React website using gunicorn, nginx and docker.
I can't get nginx to read my static files for example the django admin panel. I already ran python manage.py collecstatic and the files are in recommendations-be/backend/static
Here is the docker-compose.yml file:
version: '3'
services:
backend:
build:
context: ./recommendations-be
command: gunicorn backend.wsgi:application --bind 0.0.0.0:8000 --timeout 0
ports:
- "8000:8000"
volumes:
- static:/django/backend/static
frontend:
build:
context: ./recommendations-fe
volumes:
- react_build:/react/build
nginx:
image: nginx:latest
ports:
- "80:8080"
volumes:
- ./nginx/nginx-setup.conf:/etc/nginx/conf.d/default.conf:ro
- react_build:/var/www/react
- static:/django/backend/static
depends_on:
- backend
- frontend
volumes:
static:
react_build:
Here is my nginx conf file:
upstream api {
server backend:8000;
}
server {
listen 8080;
location / {
root /var/www/react;
}
location /api/ {
proxy_pass http://api;
proxy_set_header Host $http_host;
}
location /static/ {
alias /django/backend/static;
}
}
Here's the Dockerfile in backend directory recommendations-be:
FROM python:3.10.8-slim-buster
ENV PYTHONUNBUFFERED 1
WORKDIR /django
COPY requirements.txt requirements.txt
RUN pip install --upgrade pip --no-cache-dir
RUN pip install -r requirements.txt --no-cache-dir
COPY . .
And the django settings.py:
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "backend", "static")
Here is the file structure in my project: file structure
In your nginx configuration, please make the following change:
location /static/ {
alias /static/
}
Then, in your docker-compose.yml file, in the volume definitions of Nginx, please replace the line
- static:/django/backend/static
with the following:
- static:/static
Nginx should now be able to serve your static files.

docker nginx django gunicorn not serving static files in production

Trying to deploy website with nginx + gunicorn + docker + django. But ngingx isn't serving static files. Following are the configurations:
Django project structure
settings file production.py
STATIC_URL = "/static/"
"""STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)"""
STATIC_ROOT = "/app/forex/static/admin/"
Docker file for nginx
FROM nginx:1.19.0
COPY ./default.conf /etc/nginx/conf.d/default.conf
nginx configurations
upstream django {
server website:8000;
}
server {
listen 80;
client_max_body_size 100M;
proxy_set_header X-Forwarded-Proto $scheme;
location / {
proxy_pass http://django;
}
location /media/ {
alias /app/media/;
}
location /static/ {
alias /app/forex/static/admin/;
}
}
Gunicorn docker file
FROM python:3
ADD requirements.txt /app/requirements.txt
ADD . /app
WORKDIR /app
EXPOSE 8000:8000
RUN pip install --upgrade pip && pip install -r /app/requirements.txt
RUN python manage.py collectstatic --no-input --settings=forex.settings.production
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "forex.wsgi:application", "DJANGO_SETTINGS_MODULE=forex.settings.production"]
docker-compose.yml
services:
website:
build:
context: .
dockerfile: Dockerfile.app
env_file:
- env
container_name: website_container_8
nginx:
build: ./nginx
volumes:
- static:/app/forex/static/admin/
ports:
- "80:80"
depends_on:
- website
volumes:
static:
FROM nginx container, it isn't copying static files.
What do I need to change to make it working?
Your files are located at your website container, you need to share it, with the nginx container:
services:
website:
build:
context: .
dockerfile: Dockerfile.app
env_file:
- env
container_name: website_container_8
volumes:
- static:/app/forex/static/admin/ #<-- you want to share this
nginx:
build: ./nginx
volumes:
- static:/app/forex/static/admin/ #<-- with this folder
ports:
- "80:80"
depends_on:
- website
volumes:
static: #<-- you can do it through this

Static files not served when using docker + django + nginx

I am trying to dockerize a django project and use nginx to serve static files. Everything seems to be working except the static files are not served. When I go to the admin page, always get Not Found: /django_static/rest_framework/css/prettify.css for files in django_static folder.
folder structure:
backend
server
apps
django_static
media
...
docker
backend
Dockerfile
wsgi-entrypoint.sh
nginx
production
default.conf
development
default.conf
Dockerfile
Here is my docker-compose:
services:
nginx:
build: ./docker/nginx
ports:
- 1234:80
volumes:
- ./docker/nginx/production:/etc/nginx/conf.d
- django_static:/app/backend/server/django_static
- media:/app/backend/server/media
depends_on:
- backend
restart: "on-failure"
backend:
build:
context: .
dockerfile: ./docker/backend/Dockerfile
entrypoint: /app/docker/backend/wsgi-entrypoint.sh
ports:
- "8001:8001"
volumes:
- django_static:/app/backend/server/django_static
- media:/app/backend/server/media
expose:
- 8001
volumes:
django_static:
media:
dockerfile for django:
FROM python:3.7-slim
WORKDIR /app
ADD ./backend/requirements.txt /app/backend/
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
RUN pip install gunicorn
RUN pip install -r backend/requirements.txt
ADD ./backend /app/backend
ADD ./docker /app/docker
wsgi-entrypoint.py
#!/bin/sh
until cd /app/backend/server
do
echo "Waiting for server volume..."
done
until ./manage.py migrate
do
echo "Waiting for db to be ready..."
sleep 2
done
./manage.py collectstatic --noinput
gunicorn server.wsgi --bind 0.0.0.0:8001 --workers 1 --threads 1
#./manage.py runserver 0.0.0.0:8003
nginx docker file:
FROM nginx:1.19.0-alpine
RUN rm /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]
nginx config file:
upstream glf {
server 0.0.0.0:8001;
}
server {
listen 80;
location / {
proxy_pass http://0.0.0.0:8001;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /django_static/ {
alias /app/backend/server/django_static/;
}
location /media/ {
alias /app/backend/server/media/;
}
}
settings.py
MEDIA_URL = '/media/'
STATIC_URL = '/django_static/'
STATIC_ROOT = BASE_DIR / 'django_static'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Any hint is appreciated!

Nginx not serving my Flask website

I am following this example and this answer on stackoverflow and I am stuck.
I am running this example on a digitalocean VPS. My file structure is as follows:
project structure
docker-compose.yml
mainweb/
nginx/
README
docker-compose.yml
version: '2'
services:
app:
restart: always
build: ./mainweb
command: gunicorn -w 2 -b :5000 wsgi:app
networks:
- mainnet
expose:
- "5000"
ports:
- "5000:5000"
nginx:
restart: always
build: ./nginx
networks:
- mainnet
links:
- app
volumes:
- /www/static
expose:
- 8080
ports:
- "8880:8080"
networks:
mainnet:
mainweb/
app.py
Dockerfile
requirements.txt
templates/
wsgi.py
mainweb/app.py
from flask import Flask, render_template
app=Flask(__name__)
#app.route('/')
def home()():
return render_template('templates/home.html')
if __name__=="__main__":
app.run(host="0.0.0.0", port=5000)
mainweb/Dockerfile
FROM python:3.5
MAINTAINER castellanprime
RUN mkdir /mainweb
COPY . /mainweb
WORKDIR /mainweb
RUN pip install -r requirements.txt
mainweb/templates/
home.html
mainweb/templates/home.html
<!doctype html>
<html>
<head>
<title> My website </title>
</head>
<body>
<h1> I am here </h1>
</body>
</html>
mainweb/wsgi.py
from app import app
if __name__=="__main__":
app.run()
nginx
Dockerfile
sites-enabled.conf
static/
nginx/Dockerfile
FROM nginx:1.13.1-alpine
MAINTAINER castellanprime
ADD sites-enabled.conf /etc/nginx/conf.d/sites-enabled.conf
ADD static/ /www/static/
nginx/sites-enabled.conf
server{
listen 8080;
server_name app; # Should I put my actual www.XXXXXX.XXXXX address here
charset utf-8;
location /static{
alias /www/static/;
}
location / {
proxy_pass http://app:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X_Forwared-For $proxy_add_x_forwarded_for;
}
}
nginx/static
css/
js/
After I run the command docker-compose up -d, I check the www.XXXXXX.com:8880, or www.XXXXXX.com:8080 from another web client on another system.
I get the standard nginx web page.
How do I redirect it to the home.html?
Take a step back and run the Flask app alone.
You have some syntax errors.
from flask import Flask, render_template
app=Flask(__name__)
#app.route('/')
def home(): # Remove double brackets
return render_template('home.html') # The templates folder is already picked up
if __name__=="__main__":
app.run(host="0.0.0.0", port=5000)
Then in a Docker container, and without gunicorn
FROM python:3.5
RUN mkdir /mainweb
COPY . /mainweb
WORKDIR /mainweb
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python3","/mainweb/app.py"]
And run it, and see if it works.
cd mainapp
docker build -t flask:test .
docker run --rm -p 5000:5000 flask:test
Open http://server:5000
Then start on docker-compose with just that container and define nginx if you want.
nginx/Dockerfile
FROM nginx:1.13.1-alpine
ADD flask.conf /etc/nginx/conf.d/
EXPOSE 8080
nginx/flask.conf (I changed this based on a file that I have in a project)
server {
listen 8080; # This is the port to EXPOSE in nginx container
server_name app; # You can change this, but not necessary
charset utf-8;
location ^~ /static/ {
alias /usr/share/nginx/html/;
}
location / {
try_files $uri $uri/ #flask;
}
location #flask {
proxy_pass http://app:5000; # This is the port Flask container EXPOSE'd
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X_Forwared-For $proxy_add_x_forwarded_for;
}
}
And finally, the compose. You don't want to have your site exposing both 5000 and 80 (you don't want people to bypass nginx), so just don't expose 5000
version: '2'
services:
app:
restart: always
build: ./mainweb
networks:
- mainnet
nginx:
restart: always
build: ./nginx
networks:
- mainnet
links:
- app
volumes:
- ./mainweb/static:/usr/share/nginx/html
ports:
- "80:8080"
networks:
mainnet:

Categories

Resources