Python NGINX在停靠的Django项目中找不到静态文件
我不熟悉使用Docker和Nginx。我正在做一个我在本地工作的项目,其中包括flatpages和几个简单的应用程序,并将其与 由于某些原因,它找不到我的静态文件,甚至找不到标准的Django管理静态文件 控制台错误Python NGINX在停靠的Django项目中找不到静态文件,python,django,docker,nginx,gunicorn,Python,Django,Docker,Nginx,Gunicorn,我不熟悉使用Docker和Nginx。我正在做一个我在本地工作的项目,其中包括flatpages和几个简单的应用程序,并将其与 由于某些原因,它找不到我的静态文件,甚至找不到标准的Django管理静态文件 控制台错误 GET http://0.0.0.0:8000/static/flatpages/CSS/flatpages.css net::ERR_ABORTED 404 (Not Found) Django管理员 local.conf upstream hello_server {
GET http://0.0.0.0:8000/static/flatpages/CSS/flatpages.css
net::ERR_ABORTED 404 (Not Found)
Django管理员
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/;
}
}
设置.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/',
]
在许多其他事情中,到目前为止,我已经尝试过:
- 使用静态根目录和静态文件目录
- 我从Django文档中了解到,在
多个位置我需要使用STATICFILES\u DIRS - 如果我使用STATIC_ROOT并收集STATIC,Django管理员会发现 静态文件,但找不到任何其他文件
- 我从Django文档中了解到,在
- 导航apps Docker容器以手动查找文件。我 找不到它们或任何有效的模板。我发现这个 很奇怪李>
请让我知道,如果我需要澄清或包括任何其他内容。提前感谢您的帮助,非常感谢。假设您的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;
}
正如我所理解的,您需要在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
location /static/ {
root /opt/services/djangoapp;
}
首先,如果您使用nginx lika代理服务静态文件,您必须在端口80中加载页面,并且您必须显示如何运行容器,在我的情况下,如果我像卷一样传递静态目录(container\u path/static),您只需将位置设置为container\u path,因为nginx将在路径集中搜索静态目录,大概是这样的:
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
location /static/ {
root /opt/services/djangoapp;
}
请记住运行collectstatic命令将静态文件收集到静态目录首先,
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\u ROOT=os.path.join(BASE\u DIR,“STATIC\u ROOT”)
然后运行collectstatic命令
从静态根文件夹复制管理员文件夹,并将其粘贴到静态文件夹中
最后,运行
服务nginx restart
根据BASE_DIR变量,您的静态文件应与文件夹位于同一目录下,检查是否收集静态文件将“static”sir放在您的服务器上,并配置nginx来定位它。您仍然需要通过代理将其传递到django服务器感谢您的回复。我已尝试将建议的设置添加到location/static/{…}。不幸的是,仍然不起作用。我的理解是location/static/settings扩展了local.conf文件前面设置的location/settings。对吗?嗨,鲁德拉,谢谢你的评论。我已尝试更改docker compose文件以符合您的建议,但它仍然不起作用。请访问nginx
的外壳,转到目录/opt/services/djangoapp/static/
,然后键入ls
,查看该目录中是否存在任何内容?只需一个包含静态文件的管理目录即可德扬戈。我已经在Django容器中运行了collectstatic,但看起来它并没有找到我的应用程序的静态文件。这是否意味着管理站点现在有css?是的,我通过将静态根设置为:static\u root=os.path.join(os.path.dirname(os.path.dirname(BASE\u DIR)),“static”来获得管理静态文件我的理解是在多个应用程序中拾取静态文件我需要使用STATICFILES\u DIRS,当我尝试收集静态文件时,它会给我以下错误:FileNotFoundError:[Errno 2]没有这样的文件或目录:'/opt/services/djangoapp/static'Hi Juancarlos。不幸的是,你的建议不起作用。