[英]django+gunicorn+nginx gives 404 while serving static files
django+gunicorn+nginx gives 404 while serving static files django+gunicorn+nginx 在提供 static 文件时给出 404
I am trying to deploy a Django project using nginx + gunicorn + postgresql.我正在尝试使用 nginx + gunicorn + postgresql 部署 Django 项目。 All the configuration is done, my admin panel project static file will serve, but other static files;所有配置完成,我的管理面板项目 static 文件将服务,但其他 static 文件; it returns a 404 error.(iam use run python manage.py collectstatic)它返回 404 错误。(我使用运行 python manage.py collectstatic)
my error.log nginx :: "/blogpy/static/home/test.css" failed (2: No such file or directory)"
Structure:
blogpy
-blogpy
-config
-nginx
-nginx.conf
-docker-compose.yml
-Dockerfile
-home
-static
-home
-test.css(not working)
- requirements
-static
-templates
-.env
-docker-compose.yml
-Dockerfile
setting.py:
DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
nginx configuration:
---- nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
upstream blogpy{
server blogpy:8000;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_pass http://blogpy;
}
location /static/ {
alias /blogpy/static/;
}
}
}
Try In ---- nginx.conf:试用----nginx.conf:
location /static/ {
autoindex off;
alias /home/ubuntu/blogpy/static/; #add full path of static file directry
}
To get /blogpy/home/static/ files been copied into /blogpy/static/ by collectstatic command, you need to specify STATICFILES_DIRS setting要通过 collectstatic 命令将 /blogpy/home/static/ 文件复制到 /blogpy/static/ 中,您需要指定 STATICFILES_DIRS 设置
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [
BASE_DIR / 'home' / 'static',
]
It is recommended to serve static files directly from nginx
.建议直接从nginx
提供 static 文件。
Add the following to your nginx site config.将以下内容添加到您的 nginx 站点配置中。
location /static/ {
alias /path/to/static/directory/;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.