尝试了一切但仍然无法通过 nginx+gunicorn 提供 Django 项目的静态文件
Tried everything but still cannot serve static files of Django project via nginx+gunicorn
我必须通过 nginx+gunicorn 部署 Django 项目(nginx 的静态文件和 gunicorn 的其他文件。)
dev.py 项目设置文件夹中的文件有这些设置 -
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
nginx.conf 看起来像这样 -
server {
listen 8000;
server_name localhost;
access_log /home/user/access.log;
error_log /home/user/error.log;
location / {
proxy_pass http://localhost:8001;
}
location /static/ {
autoindex on;
alias /home/user/project_revamped/project/static;
}
}
我是 运行 我的服务器 -
gunicorn project.wsgi:application --bind=127.0.0.1:8001
在 URL localhost:8001 访问服务器时,显示的页面没有任何静态文件。
nginx 的 error.log 未填充。错误是浏览器试图从 url
请求静态文件
localhost:8001/static/...
但是它应该从 URL -
请求文件
localhost:8000/static/...
因为 nginx 配置为通过 url.
提供服务
server {
listen 8000;
server_name localhost;
root /home/user/project_dir/project;
access_log /home/user/access.log;
error_log /home/user/error.log;
location /static/ {
alias /home/user/project_dir/project/static/;
}
location / {
proxy_set_header Host $http_host;
proxy_pass http://localhost:8001;
proxy_redirect default;
}
- 我认为这应该是适合您的情况的正确配置。
- 理想情况下,你应该在端口 80 上监听所有的 http 请求,所以监听
生产时8000改为80
- 如果你在代码之前写 location / 和静态的东西
永远不会到达位置 /static/
- 必须有"proxy_set_header Host $http_host;",这样在debug=
False(在生产中)允许主机获得一些值
我必须通过 nginx+gunicorn 部署 Django 项目(nginx 的静态文件和 gunicorn 的其他文件。)
dev.py 项目设置文件夹中的文件有这些设置 -
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
nginx.conf 看起来像这样 -
server {
listen 8000;
server_name localhost;
access_log /home/user/access.log;
error_log /home/user/error.log;
location / {
proxy_pass http://localhost:8001;
}
location /static/ {
autoindex on;
alias /home/user/project_revamped/project/static;
}
}
我是 运行 我的服务器 -
gunicorn project.wsgi:application --bind=127.0.0.1:8001
在 URL localhost:8001 访问服务器时,显示的页面没有任何静态文件。
nginx 的 error.log 未填充。错误是浏览器试图从 url
请求静态文件localhost:8001/static/...
但是它应该从 URL -
请求文件localhost:8000/static/...
因为 nginx 配置为通过 url.
提供服务server {
listen 8000;
server_name localhost;
root /home/user/project_dir/project;
access_log /home/user/access.log;
error_log /home/user/error.log;
location /static/ {
alias /home/user/project_dir/project/static/;
}
location / {
proxy_set_header Host $http_host;
proxy_pass http://localhost:8001;
proxy_redirect default;
}
- 我认为这应该是适合您的情况的正确配置。
- 理想情况下,你应该在端口 80 上监听所有的 http 请求,所以监听 生产时8000改为80
- 如果你在代码之前写 location / 和静态的东西 永远不会到达位置 /static/
- 必须有"proxy_set_header Host $http_host;",这样在debug= False(在生产中)允许主机获得一些值