Django不断将URL从http:// localhost /更改为http://127.0.0.1:8080/

rts*_*ker 4 python django nginx django-forms

正如标题所描述的,Django的不断更改我的网址/localhost/,以/127.0.0.1:8080/为什么老搞乱了我的nginx的静态文件服务.任何想法为什么这样做?谢谢!

/ **EDIT ** /这是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;
    }
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

Lio*_*nel 7

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

这是参考:Django Doc for Settings.py

USE_X_FORWARDED_HOST Django 1.3.1中的新功能:请参阅发行说明

默认值:False

一个布尔值,指定是否优先使用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
Run Code Online (Sandbox Code Playgroud)