NGINX在Dockered Django项目中找不到静态文件

问题描述 投票:1回答:4

我是使用Docker和Nginx的新手。我正在参与一个我在本地工作的项目,其中包括flatpages和几个简单的应用程序,并将它与this guide to utilise Docker, NGINX and Gunicorn相结合。

由于某种原因,它找不到我的静态文件甚至标准的Django管理静态文件。

控制台错误

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.朋友

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/',
]  

在许多其他事情中,到目前为止我已经尝试过:

  • 使用STATIC_ROOT和STATICFILES_DIRS 我对Django文档的理解是使用静态文件 多个位置我需要使用STATICFILES_DIRS 如果我使用STATIC_ROOT并收集静态,Django Admin会找到静态文件,但不会找到任何其他文件。
  • 浏览应用程序Docker容器以手动查找文件。我无法找到它们或任何有效的模板。我觉得这很奇怪。

如果我需要澄清或包含任何其他内容,请与我们联系。在此先感谢您的帮助,我们非常感谢。

python django docker nginx gunicorn
4个回答
0
投票

假设您的docker容器中的静态文件夹位于/ opt / services / djangoapp中,那么您的位置/静态配置仍应代理传递给django服务器:

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

0
投票

据我所知,您需要在NGINX和Django文件夹之间同步静态卷。为此,您需要更新您的docker compose,如下所示:

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

0
投票

首先,如果您使用nginx lika代理来提供静态文件您必须在端口80中加载页面,并且必须显示如何在我的情况下运行容器,如果我像卷一样传递静态目录(container_path / static),你需要将位置设置为container_path,因为nginx会在路径集中创建一个静态目录,如下所示:

location /static/ {
        root /opt/services/djangoapp;
    }

并记住运行collectstatic命令将静态文件收集到静态目录


0
投票

首先,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/; 
     }    
}

在settings.py中STATIC_ROOT = os.path.join(BASE_DIR,'static_root')

然后运行collectstatic命令

从static_root文件夹中复制admin文件夹并将其粘贴到静态文件夹中

最后,运行service nginx restart


0
投票

根据您的BASE_DIR变量,您的静态文件应该与您的文件夹位于同一目录中,checkstatic将“静态”先生放在您的服务器上并配置nginx以找到它。

© www.soinside.com 2019 - 2024. All rights reserved.