Reputation: 71
I'm new to using Docker and Nginx. I'm taking a project I have working locally which includes flatpages and a couple of simple apps and combining it with this guide to utilise Docker, NGINX and Gunicorn.
For some reason, it can't find my Static files or even the standard Django admin static files.
Console error
GET http://0.0.0.0:8000/static/flatpages/CSS/flatpages.css
net::ERR_ABORTED 404 (Not Found)
Django Admin Django Admin
local.conf
upstream hello_server {
server djangoapp:8000;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://hello_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
autoindex on;
alias /opt/services/djangoapp/static/;
}
location /media/ {
alias /opt/services/djangoapp/media/;
}
}
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)),
"static"),
'/static/flatpages/',
]
Amongst many other things, so far I've tried:
Please let me know if I need to clarify or include anything else. Thanks in advance for any help, it's really appreciated.
Upvotes: 3
Views: 3534
Reputation: 1217
Your static file should be on same directory as your folder according to your BASE_DIR variable, check were collectstatic puts the "static" sir on your server and configure nginx to locate it.
Upvotes: 0
Reputation: 261
First, sudo nano /etc/nginx/sites-available/default
server {
listen 80;
server_name your_ip:8000 www.your_ip:8000;
root /var/www/html;
location / {
proxy_pass http://your_ip:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
location /static/admin {
alias /path/to/static/admin/;
}
location /static/ {
alias /path/to/static/;
}
location /media/ {
autoindex on;
alias /path/to/media/;
}
}
In settings.py STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
then run collectstatic command
copy admin folder from static_root folder and paste it into static folder
Finally, run service nginx restart
Upvotes: 0
Reputation: 631
first if you ar using nginx lika a proxy to serve static files You, must be load the page in port 80 and you must show how run the container, nut in my cases if i pass static directory like a volume (container_path/static), you onli need set location to container_path because nginx will searc an static directory in the path set, something like this:
location /static/ {
root /opt/services/djangoapp;
}
and remember run collectstatic command to collect the statics files to static directory
Upvotes: 1
Reputation: 52018
As I can understand, you need to sync the static volume between NGINX and Django folder. For that you need to update your docker compose like this:
version: '3'
services:
djangoapp:
build: .
volumes:
- .:/opt/services/djangoapp/src
networks:
- nginx_network
nginx:
image: nginx:1.13
ports:
- 8000:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- ./static:/opt/services/djangoapp/static/ # Syncing your current static directory to the Docker
depends_on:
- djangoapp
networks:
- nginx_network
networks:
nginx_network:
driver: bridge
Upvotes: 1
Reputation: 3103
Assuming on your docker container the static folder is in /opt/services/djangoapp, then your location /static config should still proxy pass to the django server:
location /static/ {
alias /opt/services/djangoapp/static/;
proxy_pass http://hello_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect default;
}
Upvotes: 0