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

Django DisallowedHost报错添加ALLOWED_HOSTS后连接被拒求助

Troubleshooting Django DisallowedHost + Connection Refused + Forced HTTPS Issues

Hey there, let's work through this problem together. You mentioned fixing the DisallowedHost error by adding your domain to ALLOWED_HOSTS, but now you're getting connection refused and being forced into HTTPS. Let's break down the likely causes and fixes:

1. Double-Check Your ALLOWED_HOSTS Configuration

First, let's confirm your ALLOWED_HOSTS is set correctly—even a small typo can throw things off:

  • Make sure the domain you're accessing exactly matches what's in the list. For example, if you're visiting www.yourdomain.com, don't just add yourdomain.com unless you've set up redirects between the two.
  • For testing purposes, you can temporarily set ALLOWED_HOSTS = ['*'] (never do this in production!) to rule out whether this is still the root cause. If the connection works with this, you know you had a domain mismatch.
  • Avoid trailing spaces or extra characters in list entries—keep them clean, like ['yourdomain.com', 'www.yourdomain.com'].

2. Fix the Forced HTTPS Redirect Issue

The forced HTTPS is almost certainly coming from your Django security settings. Here's how to adjust this:

  • Look for SECURE_SSL_REDIRECT in your settings.py. If this is set to True but your server isn't configured for HTTPS (or you're testing locally with HTTP), Django will try to redirect you to HTTPS, which can result in a connection refused error.
    • For local development, set SECURE_SSL_REDIRECT = False, and also set SESSION_COOKIE_SECURE = False and CSRF_COOKIE_SECURE = False to avoid cookie-related issues over HTTP.
  • If you're using a reverse proxy (like Nginx or Apache) in production that handles HTTPS, you need to tell Django to trust the proxy's HTTPS signal. Add this line to your settings.py:
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
    
    This lets Django know that even though the request between the proxy and Django is HTTP, the original request to the proxy was HTTPS.

3. Verify Server/Port & Proxy Configuration

Connection refused often points to a networking issue between your client and the Django server:

  • If you're running Django locally, make sure you're accessing the correct port (default is 8000). If you changed the port, update your URL accordingly (e.g., http://localhost:8080).
  • If you're using a reverse proxy:
    • Check that the proxy is configured to forward requests to Django's running port. For example, in Nginx, your location block should have something like proxy_pass http://127.0.0.1:8000;.
    • Ensure the proxy is passing the correct Host header to Django. Add proxy_set_header Host $host; to your Nginx config to preserve the original request host.
  • Confirm that the Django server is actually running! Run python manage.py runserver (or your production server command like gunicorn) and check for startup errors.

4. Dig Into the Error Logs

Looking at your error logs, keep an eye for these clues:

  • If logs mention "Connection refused", check if Django is listening on the expected port, or if a firewall is blocking the port.
  • If you see "Too many redirects", that's a sign the HTTPS redirect is looping—this happens when SECURE_SSL_REDIRECT is True but Django can't detect that the request is already HTTPS (fix this with the SECURE_PROXY_SSL_HEADER setting above).

Once you work through these steps, you should be able to resolve both the connection refused and forced HTTPS issues. Let me know if you hit any specific snags!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:35:54