Custom domain giving Bad Request (400)

Hello everyone.

I’ve a Django app deployed and is working fine on myapp.onrender.com.

I added my custom domain (Web service → Settings → Add custom domain). Followed the instructions given in render’s documentation to set up my dns on Google domains. Set up a CNAME, A, and CAA. Got my domain verified in my Render web service, but when I try to go to my domain (jeanpy.dev) I get a Bad Request (400).

I can see in my web service’s logs the requests when I try going to my domain jeanpy.dev (deleted some bits from the log):

May 17 11:44:16 PM  .... - - [17/May/2022:22:44:16 +0000] "GET / HTTP/1.1" 400 143 "-" "Moz......"

How to solve this?
Thanks in advance.

Hi Jean,

Thanks for reaching out.

A Bad Request (400) error from a Django app usually points to a missing ALLOWED_HOSTS environment variable, listing the allowed hosts.

More on this in the Django setting docs.

Hope that helps

Alan

1 Like

Ok, makes sense. What’s the best way to solve it:

  1. Add the information under ALLOWED_HOSTS, push the changes to my GH and deploy again in Render.

or…

  1. Changing manually in my Render web service shell where I can access the settings.py as well, but then changes won’t be on my local files.

With my inexperience in deployments, I’d go with number 1, but number 2 is also possible.

Thanks again in advance!!

Hi Jean,

I suggest keeping your config in the environment rather than the code. I think Django looks for ALLOWED_HOSTS by default, so it shouldn’t need any code changes.

I also wouldn’t set any vars in the shell as they would be lost on next deploy, I’d set it on the Render service in the “Environment” tab.

I don’t think you’d need ALLOWED_HOSTS locally, as it’ll likely be in Debug mode and you also won’t be using the onrender.com (or custom domains) in that environment. If you’re looking to keep a parity of project environment variables maybe consider a .env file & “dotenv” library.

Kind regards

Alan

Thanks.

I’ve done as suggested. Went to the ‘Environment’ tab in my web service, and clicked on ‘Add Environment Variable’, and added the following:

key: ALLOWED_HOSTS
value: www.jeanpy.dev

Did it about 4 hours ago and still receiving a bad request (400).

Not sure what else to try.

Thanks for your time.

Solved it by adding my domain in ALLOWED_HOSTS in my settings.py directly as adding it to the ‘Environment’ tab didn’t work for me.

Thanks al_ps for your help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.