我有 django 应用程序在子路径中运行example.com/api/
。大部分是rest API(我使用django-rest-framework),所有请求都正常工作。但是静态文件到处都有错误的路径 - 在管理面板和请求页面中(在 django-rest-framework 中,你有前端来调查事情)。 Django 以某种方式认为,所有静态文件都在example.com/back/static/
,当他们在example.com/api/back/static/
.
Also example.com/api/admin
重定向到example.com/admin/login/
,但是手动转到之后example.com/api/admin/login/
一切都很顺利(但仍然没有样式)。
我的堆栈是在 docker 容器中运行的 nginx + Django 2.0.5。
Nginx 配置:
location /api/ {
proxy_pass http://localhost:8000/;
proxy_read_timeout 90;
proxy_connect_timeout 90;
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 Proxy "";
}
当谈到姜戈时STATIC_URL
被设定为os.path.join(BASE_DIR, "static/")
, FORCE_SCRIPT_NAME
未设置,但设置后根本不起作用。
有任何想法吗?
[Update]
经过几个小时的战斗,我放弃了。问题与 uwsgi 无法识别它在子路径中运行有关。以下是一些对于遇到类似问题的人可能有用的链接:
通过 nginx+uwsgi 在子路径中运行 django 应用程序 https://stackoverflow.com/questions/11292217/run-django-app-via-nginxuwsgi-in-a-subpath
在子路径上运行 Django https://integricho.github.io/2014/02/22/running-django-on-a-subpath/
使用 Nginx 和 Gunicorn 为多个 Django 应用程序提供服务 http://michal.karzynski.pl/blog/2013/10/29/serving-multiple-django-applications-with-nginx-gunicorn-supervisor/
Django WSGI 路由 http://spazzy757.com/wsgi-routing/
我放弃了,只是为这个项目创建了单独的子域。