Django DisallowedHost报错添加ALLOWED_HOSTS后连接被拒求助
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 addyourdomain.comunless 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_REDIRECTin yoursettings.py. If this is set toTruebut 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 setSESSION_COOKIE_SECURE = FalseandCSRF_COOKIE_SECURE = Falseto avoid cookie-related issues over HTTP.
- For local development, set
- 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:
This lets Django know that even though the request between the proxy and Django is HTTP, the original request to the proxy was HTTPS.SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', '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
Hostheader to Django. Addproxy_set_header Host $host;to your Nginx config to preserve the original request host.
- 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
- Confirm that the Django server is actually running! Run
python manage.py runserver(or your production server command likegunicorn) 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_REDIRECTis True but Django can't detect that the request is already HTTPS (fix this with theSECURE_PROXY_SSL_HEADERsetting 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

