You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django2+Python3.6访问127.0.0.1:8000遇DisallowedHost错误求助

Troubleshooting Django DisallowedHost Error Even With ALLOWED_HOSTS Set to *

Hey there, I know how frustrating it is to hit a DisallowedHost error even after setting ALLOWED_HOSTS = ['*'] in your Django settings. Let’s walk through the most likely fixes, especially since you mentioned your wsgi.py file is involved.

Common Causes & Fixes

1. Make Sure You’re Editing the Correct settings.py

It sounds obvious, but sometimes projects have multiple settings files (like settings/dev.py or settings/prod.py) and you might be modifying the wrong one.

  • Double-check which settings module your server is using. For the dev server, run python manage.py runserver --settings=your_project.settings explicitly to confirm.
  • Always restart your server after modifying settings—changes won’t take effect until you do this. If you’re using a production WSGI server (like Gunicorn or uWSGI), restart that process too.

2. Verify Your wsgi.py Points to the Right Settings

Take a look at your wsgi.py file. It should have a line that sets the Django settings module. Make sure it’s pointing to the exact file where you updated ALLOWED_HOSTS:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')

If you’re using a non-default settings file (e.g., settings.prod), update this line to match.

3. Check for Reverse Proxy/Server Interference

If you’re using a reverse proxy like Nginx in front of Django, it might be modifying the Host header before it reaches your app.

  • In your Nginx config, ensure you have this line in the location block that proxies to Django:
    proxy_set_header Host $host;
    

This ensures the original Host header from the client is passed through to Django. Without it, Django might see the proxy’s internal IP/hostname instead of 127.0.0.1:8000.

4. Debug the Actual Host Header Django Receives

To confirm what Host value Django is actually getting, add a quick debug snippet to your wsgi.py:

import os
from django.core.wsgi import get_wsgi_application

def debug_application(environ, start_response):
    # Print the received Host header to the console
    print(f"Received HTTP_HOST: {environ.get('HTTP_HOST', 'Not found')}")
    # Call the regular Django WSGI application
    app = get_wsgi_application()
    return app(environ, start_response)

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project.settings')
application = debug_application

Run your server and access 127.0.0.1:8000—check the console output to see what Host Django is processing. If it’s not 127.0.0.1:8000, that’s the root of the issue.

5. Rule Out Third-Party Middleware or Apps

Some security-focused middleware or third-party packages might override your ALLOWED_HOSTS setting. Try temporarily disabling non-essential middleware in your settings to see if the error goes away. If it does, you’ll know which app is causing the conflict.

6. Confirm Runserver Binding (For Dev Environment)

If you’re using Django’s dev server, make sure you’re binding to the correct address. By default, runserver uses 127.0.0.1:8000, but if you ran it with python manage.py runserver 0.0.0.0:8000, accessing 127.0.0.1:8000 should still work—but it’s worth double-checking you’re not binding to a different IP that doesn’t match your request.

Start with the first two checks (correct settings file and wsgi.py configuration)—those are the most common culprits. If those don’t fix it, move on to debugging the Host header to get more clarity.

内容的提问来源于stack exchange,提问作者Riken Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:28:31