【问题标题】:Django keeps changing URL from http://localhost/ to http://127.0.0.1:8080/Django 不断将 URL 从 http://localhost/ 更改为 http://127.0.0.1:8080/
【发布时间】:2011-03-29 05:22:28
【问题描述】:

正如标题所描述的,Django 不断将我的 URL 从 /localhost/ 更改为 /127.0.0.1:8080/,这不断扰乱 Nginx 提供的静态文件。有什么想法为什么要这样做?谢谢!

/**编辑**/ 这里是 Nginx 配置:

server {

    listen   80; ## listen for ipv4
    listen   [::]:80 default ipv6only=on; ## listen for ipv6

    server_name  localhost;

    access_log  /var/log/nginx/localhost.access.log;

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$
    {
            root   /srv/www/testing;
    }

    location / {
            proxy_pass         http://127.0.0.1:8080/;
            proxy_redirect     off;
    }

    location /doc {
        root   /usr/share;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location /images {
        root   /usr/share;
        autoindex on;
    }

这里是 Apache 配置文件:

<VirtualHost *:8080>

    ServerName testing
    DocumentRoot /srv/www/testing

    <Directory /srv/www/testing>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIScriptAlias / /srv/www/testing/apache/django.wsgi

</VirtualHost>

【问题讨论】:

  • 你是如何构建你的 URL 的?

标签: python django django-forms nginx


【解决方案1】:

如果您使用的是 VirtualHost,则需要在 settings.py 中设置 USE_X_FORWARDED_HOST = True

这是参考:Django Doc for Settings.py

USE_X_FORWARDED_HOST Django 1.3.1 中的新功能:请看发布 笔记

默认值:假

一个布尔值,指定是否使用 X-Forwarded-Host 标头 对 Host 标头的偏好。仅当代理时才应启用此功能 设置此标头正在使用中。

下面是一些示例代码:

import os, socket
PROJECT_DIR = os.path.dirname(__file__)

on_production_server = True if socket.gethostname() == 'your.productionserver.com' else False

DEBUG = True if not on_production_server else False
TEMPLATE_DEBUG = DEBUG

USE_X_FORWARDED_HOST = True if not on_production_server else False

【讨论】:

  • +1:对于 HTTP 服务器配置正确但 Django 服务器配置不正确的任何人来说,这都是正确的答案。
【解决方案2】:

edit2:

http://wiki.nginx.org/HttpProxyModule#proxy_redirect

http://wiki.nginx.org/HttpProxyModule#proxy_pass

我认为正在发生的事情是,当您使用 httpresponseredirect 时,HTTP_HOST 标头会赋予它 127.0.0.1:8080,因为您的 proxy_pass 设置。

Django's HttpResponseRedirect seems to strip off my subdomain?

Django 总是有一些方法 适用于响应。其中之一是 django.utils.http.fix_location_header。 这确保了重定向 响应总是包含一个绝对值 URI(根据 HTTP 规范的要求)。

【讨论】:

  • 没有。我在所有链接和表单中使用相对 URL。我正在使用 Nginx 将所有请求(静态文件除外)转发到 127.0.0.1:8080 的 Apache。我知道 127.0.0.1 与 localhost 相同,即使我只使用 127.0.0.1 我的 css 文件也可以正常工作。它只是每当我在我的应用程序中发布一个表单时,它就会将它重定向到端口 8080,这会弄乱我的 css
  • 编辑您的帖子以包含您的 nginx/apache 配置文件,这可能是一个设置问题
  • 我可能需要指出,只有在我使用 HttpResponseRedirect 时才会这样做,这是唯一一次将其从 /localhost/ 更改为 /127.0.0.1:8080/
  • 你见过这个吗?可能和你的问题有关stackoverflow.com/questions/1457006/…
  • 谢谢大家!我现在已经通过在 settings.py 文件中定义 ROOT_URL 并仅使用 settings.ROOT_URL + '' 来实现我想要的来修复它。虽然每次我使用 HttpResponseRedirect 都会很麻烦.. :( 如果有人知道更好的方法,请告诉我!哈哈
【解决方案3】:

遇到了同样的问题(django 重定向到浏览器时附加了“:8080”)。经过进一步搜索,我找到了一些nginx信息。以下修复了它...

在你的 nginx 配置中,替换...

  proxy_redirect    off;

与....

  proxy_redirect   http://127.0.0.1:8080/ http://127.0.0.1/;

记得重启你的 nginx 守护进程。这会导致 nginx 剥离从 apache 流回浏览器的数据包上的 8080。例如通过 apache 从 django 重定向,http://127.0.0.1:8080/my/test/file.html 将在 nginx 将其发送回客户端后变为 http://127.0.0.1/my/test/file.html

现在您不必修改 django 代码了。

【讨论】:

    猜你喜欢
    • 2016-06-07
    • 2018-01-27
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    相关资源
    最近更新 更多