【发布时间】:2022-01-14 06:26:17
【问题描述】:
我有一个 django 后端和响应前端。
我想在 / 上提供响应,并为 Django 使用 /admin、/api 和 /auth。这是我的 Nginx 中的内容。
upstream backend {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name x.x.x.x;
root /home/user/folder/frontend;
index index.html index.htm;
# for serving static
location /static {
alias /home/user/folder/backend/staticfiles;
}
# for serving react built files
location / {
try_files $uri $uri/ /index.html;
}
# for everything django
location ~^/(admin|api|auth) {
include snippets/proxyinfo.conf;
proxy_pass http://backend;
}
}
有了上述,预期的行为是
-
/使用默认根文件夹/home/user/folder/frontend并相应地从 react 加载构建的索引文件 -
/(admin|api|auth)指向 django -
/static加载保存在/home/user/folder/backend/staticfiles文件夹中的静态文件。
所以不知道为什么当我点击 example.com/static/myfile.css 时,Nginx 会转到 /home/user/folder/frontend/static/myfile.css
我希望上面的配置都没有说明它应该做的,那么到底发生了什么神奇的事情?
我认为这是answer was self explanatory enough,但 Nginx 一直在做它喜欢做的事情。
我正在使用 nginx/1.18.0(如果重要的话)
【问题讨论】:
-
看起来很奇怪。如果您在
location和alias指令中添加斜杠会发生什么:location /static/ { alias /home/user/folder/backend/staticfiles/; }?
标签: nginx