0atman
0atman

Reputation: 3374

Mis-setting django's ALLOWED_HOSTS setting does not give understandable errors

Django's ALLOWED_HOSTS setting, if set to a different host than the browser is connecting to, simply causes a 400, with no indication of the problem.

DEBUG = False
ALLOWED_HOSTS = ['example.com']

If you hit django by connecting straight to the IP, rather than through example.com, you get a plain 400 page.

This only happens (as documented) with DEBUG = FALSE, but it happens through wsgi and through manage.py.

This has taken me far longer than I care to admit to debug.

Where is the debug output that could have saved me from this vague error? Shouldn't at least manage.py's output give a hint?

The page simply says 400 Bad Request and the log is:

1.2.3.4 - - [23/Oct/2014 11:33:22] "GET / HTTP/1.1" 400 26

Upvotes: 1

Views: 1545

Answers (1)

Glen Swinfield
Glen Swinfield

Reputation: 638

Yes, Django error logs.

[ERROR] [2014-10-23 12:46:06,073] [base 6808 140062533326784] [Invalid HTTP_HOST header: 'whatver.host'.You may need to add u'whatever_host' to ALLOWED_HOSTS.]

Here's an example log config. you should be logging Djanogo errors if you hope to debug anything.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'error_file': {
            'level': 'ERROR',
            'filename': os.path.join(LOG_PATH, 'error.log'),
            'class':'logging.handlers.RotatingFileHandler',
            'maxBytes': 1 * 1024 * 1024,
            'backupCount': 2
        }
    },
    'loggers': {
        'django': {
            'handlers': ['error_file'],
            'level': 'ERROR',
            'propagate': True
        }
    }
}

Upvotes: 2

Related Questions