NGINX not finding static files in Dockered Django project

末鹿安然 提交于 2019-12-23 03:57:28

问题


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:

  • Using STATIC_ROOT and STATICFILES_DIRS
    • My understanding from the Django docs is that with static files in
      multiple locations I need to use STATICFILES_DIRS
    • If I use STATIC_ROOT and collect static the Django Admin does find the static files but won't find any others.
  • Navigating the apps Docker container to find the files manually. I can't locate them or any of the templates that do work. I find this very odd.

Please let me know if I need to clarify or include anything else. Thanks in advance for any help, it's really appreciated.


回答1:


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



回答2:


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



回答3:


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




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/55054261/nginx-not-finding-static-files-in-dockered-django-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!