当在带有子路径的nginx + uwsgi上配置时,Django app有错误的URL

时间:2016-11-03 09:47:32

标签: python django nginx uwsgi wagtail

我正在尝试配置nginx和uwsgi来在/blog子路径上提供django(wangtail为准确)app。这个问题的大多数答案(如this one)建议使用:

location /blog {
    include uwsgi_params;
    uwsgi_pass unix:/run/uwsgi/application.sock;
    uwsgi_param SCRIPT_NAME /blog;
    uwsgi_modifier1 30;
}

不幸的是,这对我不起作用。访问/blog重复子路径并重定向到/blog/blog/。 Wagtail然后显示其404页面,因为/blog/blog/不存在。我不确定它是否是nginx或django问题。

1 个答案:

答案 0 :(得分:0)

我有类似的问题,这就是我做的事情来解决它。使用Wagtail 1.13 + uwsgi + nginx / 1.10.3(Ubuntu)。

wagtail blog / settings / base.py

ROOT_URL = '/blog'
USE_X_FORWARDED_HOST  = True
FORCE_SCRIPT_NAME     = ROOT_URL
BASE_PATH             = ROOT_URL

APP_SUB_URL = ROOT_URL
if len(APP_SUB_URL):
    FORCE_SCRIPT_NAME = APP_SUB_URL

STATIC_URL = APP_SUB_URL + "/static/"
MEDIA_URL = APP_SUB_URL + "/media/"
ADMIN_MEDIA_PREFIX = APP_SUB_URL + "/static/admin/"
LOGIN_URL = APP_SUB_URL + "/login/"

在nginx blog.conf 文件中:

location /blog/ {
    include uwsgi_params;
    rewrite /blog/(.*) /$1 break;
    uwsgi_pass      unix:/run/uwsgi/application.sock;
}

另外,您可以让nginx处理静态文件以获得更好的性能:

location /blog/static/ {
    access_log off;
    expires 3600;
    alias /path/to/wagtail/app/blog/static/;
}

location /blog/media/ {
    access_log off;
    expires 864000;
    alias /path/to/wagtail/app/media/;
}

我还必须将符号链接添加到/ path / to / wagtail / app / blog / static /

中的管理静态文件
cd /path/to/wagtail/app/blog/static/
ln -s ../../../venv/lib/python3.5/site-packages/wagtail/wagtailadmin/static/wagtailadmin/
ln -s ../../../venv/lib/python3.5/site-packages/wagtail/wagtaildocs/static/wagtaildocs/
ln -s ../../../venv/lib/python3.5/site-packages/wagtail/wagtailembeds/static/wagtailembeds/
ln -s ../../../venv/lib/python3.5/site-packages/wagtail/wagtailimages/static/wagtailimages/
ln -s ../../../venv/lib/python3.5/site-packages/wagtail/wagtailsnippets/static/wagtailsnippets/