如何在nginx后面的子url下部署django
我有一个 Django 应用程序在 http://localhost:12345 上运行。 我希望用户通过网址 http://my.server.com/myapp 访问它。 我使用 nginx 对其进行反向代理,如下所示:
... ...
server_name my.server.com;
location /myapp {
rewrite /myapp(.*) $1 break;
... ... # proxy param
proxy_pass http://localhost:12345;
}
... ...
问题是,当按照上述配置时,如何使响应页面中的 url 具有“/myapp”前缀,以便 nginx 可以将它们正确定向到 myapp 。例如,像“/foo/far”这样的页面中的 url 应该更改为“/myapp/foo/bar”,以允许 nginx 代理到 myapp。 为了实现这个目的,正确的 nginx 配置是什么?
我可以使用 django 的设置变量来指定根 url 前缀,但在我看来这并不灵活,因为必须根据不同的 nginx 配置修改该变量(比如有一天 nginx 可能会将子 URL 从“/myapp”更改为“ /另一个应用程序”)。
I have a django application running on http://localhost:12345 .
I'd like user to access it via url http://my.server.com/myapp .
I use nginx to reverse proxy to it like the following:
... ...
server_name my.server.com;
location /myapp {
rewrite /myapp(.*) $1 break;
... ... # proxy param
proxy_pass http://localhost:12345;
}
... ...
The question is, when configured like the above, how to make the urls in my response pages to have a prefix of "/myapp" so that the nginx can direct them correctly to myapp. E.g., the urls in a page like "/foo/far" ought to be changed to "/myapp/foo/bar" to allow nginx proxy to myapp.
what is the right nginx configure to use to achieve this ?
I can use settings variables of django to specify the root url prefix, but it's not flexiable to my mind, since the variable have to be modified according to different nginx configuration(say one day nginx may change the suburl from "/myapp" to "/anotherapp").
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于前缀是在 Nginx 中设置的,因此托管 Django 应用程序的 Web 服务器无法知道 URL 前缀。正如orzel所说,如果您使用apache+mod_wsgi甚至nginx+gunicorn/uwsgi(带有一些额外的配置),您可以使用WSGIScriptAlias值,该值由Django自动读取。
当我需要使用 URL 前缀时,我通常将其自己放在我的根 urls.py 中,其中只有一行,以前缀为前缀并包含其他 urls.py
但我猜这与设置一个 URL 具有相同的瓶颈settings.py 中的前缀,在 nginx 和 Django 中有冗余配置。
您需要在托管 Django 应用程序的服务器(地址:12345)中执行某些操作。您可以在那里设置前缀,并使用 WSGIScriptAlias 或其等效的 mod_wsgi 外部将其传递给 Django。我无法提供更多信息,因为我不知道您的 Django 应用程序是如何运行的。另外,也许您应该考虑使用 uWSGI 或 Gunicorn 直接从 Django 运行 Django 应用程序。
要将前缀从网络服务器传递给 Django,您可以使用以下命令:
更多信息 此处
As the prefix is set in Nginx, the web server that hosts the Django app has no way of knowing the URL prefix. As orzel said, if you used apache+mod_wsgi of even nginx+gunicorn/uwsgi (with some additional configuration), you could use the WSGIScriptAlias value, that is automatically read by Django.
When I need to use a URL prefix, I generally put it myself in my root urls.py, where I have only one line, prefixed by the prefix and including an other urls.py
But I guess this has the same bottleneck than setting a prefix in settings.py, you have redundant configuration in nginx and Django.
You need to do something in the server that hosts your Django app at :12345. You could set the prefix there, and pass it to Django using the WSGIScriptAlias or its equivalent outside mod_wsgi. I cannot give more information as I don't know how your Django application is run. Also, maybe you should consider running your Django app directly from Django, using uWSGI or gunicorn.
To pass the prefix to Django from the webserver, you can use this :
More information here
您需要更新您的设置:
并相应地更新您的
MEDIA_URL
和STATIC_URL
。我没有在nginx下部署的经验,但是在apache下,工作正常。
请参阅:https://docs.djangoproject。 com/en/dev/ref/settings/#use-x-forwarded-host
You'll need to update your setting:
And update your
MEDIA_URL
andSTATIC_URL
accordingly.I haven't had the experience of deploying under nginx, but under apache, it works fine.
refer to: https://docs.djangoproject.com/en/dev/ref/settings/#use-x-forwarded-host
这是我的 nginx 配置的一部分,诚然,它没有设置 FORCE_SCRIPT_NAME,但是我没有使用子目录。也许它对于在 nginx 而不是 Django 中设置与 USE_X_FORWARDED_HOST 相关的选项很有用。
Here is part of my config for nginx which admittedly doesn't set FORCE_SCRIPT_NAME, but then, I'm not using a subdirectory. Maybe it will be useful for setting options related to USE_X_FORWARDED_HOST in nginx rather than Django.