Fading Coder

One Final Commit for the Last Sprint

Home > Tools > Content

Configuring ALLOWED_HOSTS for Django Production Deployment

Tools 2

When setting DEBUG = False in Django's settings file, attempting to run the development server will result in an error.

python manage.py runserver 8888
CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.

This error indicates that the ALLOWED_HOSTS configuration is mandatory when debug mode is disabled for security reasons.

Setting ALLOWED_HOSTS

The ALLOWED_HOSTS setting accepts a list of strings representing valid host/domain names for the application. This prevents HTTP Host header attacks.

To allow a specific domain and its subdomains:

ALLOWED_HOSTS = [
    '.mydomain.com',  # Matches mydomain.com and any subdomain
]

For development or testing environments where you need to accept all hosts, you can use a wildcard:

ALLOWED_HOSTS = ['*']

Warning: Using '*' makes your application accept any host header. Ensure proper host validation is implemented elsewhere in production, such as at the web server layer.

After configuring ALLOWED_HOSTS, the development server will start normally.

Security Context

According to Django's documentation, ALLOWED_HOSTS provides protection against cache poisoning and malicious links in emails by validating the Host header. When DEBUG = True or during tesst execution, this validation is disabled.

In Django 1.7+, trailing dots in hostnames are automatically handled, so entries like '.example.com.' are no longer necessary. Host validtaion occurs through request.get_host(). Direct access to request.META['HTTP_HOST'] bypasses this security check.

For detailed specifications, refer to the official Django documentation on settings.

Related Articles

Efficient Usage of HTTP Client in IntelliJ IDEA

IntelliJ IDEA incorporates a versatile HTTP client tool, enabling developres to interact with RESTful services and APIs effectively with in the editor. This functionality streamlines workflows, replac...

Installing CocoaPods on macOS Catalina (10.15) Using a User-Managed Ruby

System Ruby on macOS 10.15 frequently fails to build native gems required by CocoaPods (for example, ffi), leading to errors like: ERROR: Failed to build gem native extension checking for ffi.h... no...

Resolve PhpStorm "Interpreter is not specified or invalid" on WAMP (Windows)

Symptom PhpStorm displays: "Interpreter is not specified or invalid. Press ‘Fix’ to edit your project configuration." This occurs when the IDE cannot locate a valid PHP CLI executable or when the debu...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.