Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Bad Request(400) Error in Deployment with Apache/NginX

I'm trying to lunch my app on VPS in Debug=False mode. Debug=True works fine but when I change it to false I got this error. I'm using Apache for rendering python pages and Nginx to serve my static files. I tried using this [answer]: Debugging Apache/Django/WSGI Bad Request (400) Error but it's not working at least for me. And this is my wsgi config:

#wsgi.py
import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'example.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
path = '/var/www/example'
if path not in sys.path:
    sys.path.append(path)

And also I've added below code to my settings file:

ALLOWED_HOSTS = [
    '.example.com', # Allow domain and subdomains
    '.example.com.', # Also allow FQDN and subdomains
]
like image 745
hamidfzm Avatar asked Nov 30 '22 19:11

hamidfzm


1 Answers

To discover your problem, first in settings.py set ALLOWED_HOSTS temporarily to:

ALLOWED_HOSTS = '*'

And then in somewhere in your view, try to print out and see output of this command:

print(request.META['HTTP_HOST']) # or print(request.get_host())

Then according to output, set that (just domain of it as an list) to your ALLOWED_HOSTS.

Notes:

  • Use ALLOWED_HOSTS = '*' may have security issue for you, read about that here.

  • After every change you need to restart your service(apache/nginx).

like image 62
Omid Raha Avatar answered Dec 04 '22 04:12

Omid Raha