Hi,
I am working on a project and trying to deploy it with docker. I am using [nginx-proxy](https://github.com/nginx-proxy)
image and follow a tutorial, however couldnt figure out how to config the static files routing. It would be really appreciated if someone could give me some advice.
settings
Static files are in /usr/src/app/static
folder, already run collectstatic
.
static root and url:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
dockerfile:
FROM python:3.8.6-slim-buster
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y netcat-openbsd gcc && \
apt-get clean
ENV PYTHONUNBUFFERED=1
WORKDIR /usr/src/app
RUN addgroup --system admin && adduser --system --no-create-home --group admin && \
chown -R admin:admin /usr && chmod -R 755 /usr
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install --upgrade pip \
pip install -r requirements.txt
USER admin
COPY . /usr/src/app
docker-compose.prod:
version: "3.8"
services:
django:
restart: always
build:
context: .
dockerfile: ./dockerfile
container_name: django
command: gunicorn --workers=2 --bind=0.0.0.0:8000 --env DJANGO_SETTINGS_MODULE=core.settings.prod core.wsgi:application
volumes:
- .:/usr/src/app
- static_volume:/usr/src/app/static
- media_volume:/usr/src/app/media
# - ./nginx/certs/:/etc/certs
ports:
- "8000:8000"
env_file:
- ./core/env/env.prod
depends_on:
- redis
celery:
restart: always
build:
context: .
dockerfile: ./dockerfile
container_name: celery
entrypoint: /bin/sh
command: ./docker.celery.sh
volumes:
- .:/usr/src/app
- static_volume:/usr/src/app/static
- media_volume:/usr/src/app/media
env_file:
- ./core/env/env.prod
depends_on:
- django
- redis
redis:
restart: always
image: redis:alpine
container_name: redis
volumes:
- redisdata:/data
nginx-proxy:
container_name: nginx-proxy
build: ./nginx
restart: always
ports:
- 443:443
- 80:80
volumes:
- static_volume:/usr/src/app/static
- media_volume:/usr/src/app/media
- certs:/etc/nginx/certs
- html:/usr/share/nginx/html
- vhost:/etc/nginx/vhost.d
- /var/run/docker.sock:/tmp/docker.sock:ro
depends_on:
- django
nginx-proxy-letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
env_file:
- ./core/env/env.prod.proxy-companion
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
volumes:
redisdata:
static_volume:
media_volume:
certs:
html:
vhost:
nginx dockerfile:
FROM jwilder/nginx-proxy
COPY vhost.d/default /etc/nginx/vhost.d/default
COPY custom.conf /etc/nginx/conf.d/custom.conf
nginx/vhost.d/default
location /static/ {
alias /usr/src/app/static/;
add_header Access-Control-Allow-Origin *;
}
file tree:
.
├── LICENSE
├── README.md
├── apps
├── core
│ ├── __init__.py
│ ├── __pycache__
│ ├── asgi.py
│ ├── celery.py
│ ├── env
│ ├── settings
│ ├── urls.py
│ └── wsgi.py
├── docker-compose.prod.yml
├── docker-compose.staging.yml
├── docker-compose.yml
├── docker.celery.sh
├── docker.django.sh
├── dockerfile
├── dockerfile.prod
├── irecommerce.code-workspace
├── manage.py
├── media
├── nginx
│ ├── certs
│ ├── custom.conf
│ ├── default.conf
│ ├── dockerfile
│ └── vhost.d
├── requirements.txt
├── static
└── templates
Issue
The static files are not working properly. Both django and nginx-proxy containters return this 404 message.
django | Not Found: /static/js/front.js
nginx-proxy | nginx.1 | domain.xyz x.8x2.2x2.66 - - [28/Dec/2020:14:26:26 +0000] "GET /static/js/front.js HTTP/2.0" 404 10638 "https://domain.xyz/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
As my understanding, the static file’s routing config was created by nginx-proxy
automatically, thus we didnt create any default.conf
file to nginx-proxy
, but only the setting in vhost.d
as the router for static files?
As mentioned, VIRTUAL_HOST (and VIRTUAL_PORT) are needed by nginx-proxy to auto create the reverse proxy configuration.
Requests that match any of these patterns will be served from static or media folders. They won’t be proxied to other containers. The web and nginx-proxy containers share the volumes in which the static and media files are located:
location /staticfiles/ {
alias /home/app/web/staticfiles/;
add_header Access-Control-Allow-Origin *;
}