【发布时间】:2021-08-08 14:34:06
【问题描述】:
我已经尝试解决这个错误将近 13 个小时,寻找成千上万的 stackoverflow 问题和其他社区的问题,但我仍然找不到答案。
我正在尝试做的是解决 nginx不提供静态文件。
正如我在顶部所说,我尝试了数千个答案,但似乎没有一个适合我。
我只在 nginx /sites-available/ 中发布脚本,因为我认为这是这个问题的主要脚本。
server {
listen 80;
server_name localhost 127.0.0.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
autoindex on;
alias /home/ngx/pid/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
我使用 gunicorn 来加载 django,而且前端是用 react 制作的,所以没有 main.js 我什么都做不了。ngx 不是默认用户 - 我做到了,我也关注了 @987654321 @教程。(虽然我没有全部关注 - 我在阅读其他人的答案后进行了编辑。
Directory
/home/ngx/pid (django root)
ㄴ diary (project root - settings.py, wsgi.py etc.)
ㄴ djenv (virtualenv)
ㄴ frontend (react)
ㄴ manage.py
ㄴ static (made by python3 manage.py collectstatic)
错误网址:http://example.com/static/css/index.csshttp://example.com/static/frontend/main.js
错误代码:404 Not Found
【问题讨论】:
-
代替
alias /home/ngx/pid/static/;可以试试alias /home/ngx/pid/;吗?你跑collectstatic了吗? -
location 和 alias 必须有相同的前导/尾随斜杠
-
不起作用...
-
试试
root /home/ngx/pid;而不是alias /home/ngx/pid/static/; -
我试过了,但也没有用...
标签: django nginx gunicorn django-staticfiles