django admin 在保存时重定向到错误的端口
我有一个使用 nginx+apache 设置的 django 项目。外部访问的http端口是20111,然后转发到服务器机器(有内部IP)的80端口。因此nginx监听80端口(并在5000端口将相关请求传递给apache)。
现在可以通过 http://externalip:20111 从外部访问初始登录 - 但是当我完成管理操作时,就像保存条目一样,我被重定向到 http://externalip/path/to/model - 没有端口 20111。结果是超时。我如何告诉 django 对所有管理重定向使用特定的主机名/端口(即 http://externalip:20111)?
I have a django project set up with nginx+apache. The http port for outside access is 20111 which is then forwarded to the server machine (which has an internal IP) to port 80. So nginx listens on port 80 (and passes relevant requests to apache on port 5000).
Now the initial login can be reached from the outside via http://externalip:20111 - but when I complete an admin action, like saving an entry, I get redirected to http://externalip/path/to/model -- without the port 20111. The result is a timeout. How can I tell django to use a specific hostname/port (i.e. http://externalip:20111) for all admin redirects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在代理或负载均衡器后面部署应用程序时,通常依赖 X-Forwarded-Host 标头。 Django 有支持
首先,你必须设置 nginx 来发送正确的标头。添加到您的 nginx 主机配置(在您的
location
部分中):其次,添加到您的 settings.py:
它将允许 django 信任来自 a 的
X-Forwarded-Host
标头要求。它应该适合你。出于安全原因,您不应信任
X-Forwarded-Host
中发送的每个值,因此将受信任的域/IP 添加到 ALLOWED_HOSTS 在settings.py
中When deploying applications behind a proxy or load balancer, it is common to rely on the X-Forwarded-Host header. Django has support for it
First of all, you have to setup nginx to send the proper headers. Add to your nginx host configuration (inside your
location
section):Second, add to your settings.py:
It will allow django to trust
X-Forwarded-Host
headers from a request.It should make it work for you. For security reasons, you should not trust every value sent in
X-Forwarded-Host
, so add your trusted domains/IPs to ALLOWED_HOSTS insettings.py