djangogunicornproduction-environment

Django 400 Bad Request when run in production with gunicorn


I am getting a 400 error on all my pages when I moved my django app from one hosting service to another, the hosting service I moved to is Scalingo.

My settings.py file:

DEBUG = os.getenv("DEBUG") == "True"
ALLOWED_HOSTS = [x.strip() for x in os.getenv("ALLOWED_HOSTS").split(",")]

My env variables

ALLOWED_HOSTS="my-app.osc-fr.scalingo.fr,.localhost,127.0.0.1,[::1]"
DEBUG="False"

I contacted customer support who have advised me that it must be a configuration issue with my app.

All existing answers here I have found have advised to make sure the ALLOWED_HOSTS variable is correct, and I have already checked that it includes the required addresses according to the hosting platform's django-specific docs.


Solution

  • In the end I debugged the problem by doing a print(ALLOWED_HOSTS) in the settings.py file and inspecting the build logs.

    Turns out the environment variables on this platform were being loaded with the quotation marks included, resulting in ['"my-app.osc-fr.scalingo.fr','.localhost','127.0.0.1','[::1]"'].

    I edited the environment variables to remove the quotation marks and it works!

    Not sure why the quotation marks were taken into account though, given that I can have them in on other hosting platforms and locally and they get ignored. If anyone knows I'd be interested to find out!