Apache2 + mod_wsgi + python 3.6 not responding - django

I'm deploying a Django app with python 3.6 and virtualenv in an Ubuntu 18.4 server. This is my apache config
<VirtualHost *:80>
ServerName myapp.com
Redirect / https://myapp.com/
</VirtualHost>
<VirtualHost *:443>
ServerName myapp.com
SSLEngine On
SSLCertificateFile "/etc/apache2/ssl/myapp.com.crt"
SSLCertificateKeyFile "/etc/apache2/ssl/myapp.comv.key"
SSLCertificateChainFile "/etc/apache2/ssl/myapp.com.crt"
SSLProxyEngine On
Alias /static/ /home/ubuntu/myapp/assets/
Alias /media/ /home/ubuntu/myapp/data/
<Directory /home/ubuntu/myapp/data>
Require all granted
</Directory>
<Directory /home/ubuntu/myapp/assets>
Require all granted
</Directory>
<Directory /home/ubuntu/myapp>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myapp python-path=/home/ubuntu/myapp:/home/ubuntu/myapp/venv/lib/python3.6/site-packages
WSGIProcessGroup myapp
WSGIScriptAlias / /home/ubuntu/myapp/wsgi.py
</VirtualHost>
if I run mod_wsgi-express module-config I get
LoadModule wsgi_module "/home/ubuntu/myapp/venv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/home/ubuntu/myapp/venv"
which is the virtualenv I want to use.
Nonetheless when I try to access myapp.com I get timedout and there is nothing in error.log or access.log
error.log
[Sun Sep 30 11:42:37.066273 2018] [core:notice] [pid 628:tid 140584565169088] AH00094: Command line: '/usr/sbin/apache2'
[Sun Sep 30 11:42:39.728096 2018] [mpm_event:notice] [pid 628:tid 140584565169088] AH00491: caught SIGTERM, shutting down
[Sun Sep 30 11:42:39.812550 2018] [mpm_event:notice] [pid 882:tid 140042546797504] AH00489: Apache/2.4.29 (Ubuntu) OpenSSL/1.1.0g mod_wsgi/4.6.4 Python/3.6 configured -- resuming normal operations
[Sun Sep 30 11:42:39.812642 2018] [core:notice] [pid 882:tid 140042546797504] AH00094: Command line: '/usr/sbin/apache2'
access.log
51.38.12.21 - - [30/Sep/2018:08:20:14 +0000] "GET / HTTP/1.1" 200 11192 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
37.135.181.196 - - [30/Sep/2018:10:40:24 +0000] "GET / HTTP/1.1" 200 3477 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
37.135.181.196 - - [30/Sep/2018:10:40:24 +0000] "GET /icons/ubuntu-logo.png HTTP/1.1" 200 3623 "http://3.120.98.140/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
37.135.181.196 - - [30/Sep/2018:10:40:25 +0000] "GET /favicon.ico HTTP/1.1" 404 503 "http://3.120.98.140/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
I tried adding WSGIApplicationGroup %{GLOBAL} to my apache config file but the result is the same.
What am I missing here?
Thanks in advance

Since no-one has attempted an answer and I've been trying to do the same thing (only on CentOS), for me it was SELinux.
If you have SELinux enabled (seems unlikely on Ubuntu), it might be denying read access to the wsgi file, though I'd expect your httpd error log and selinux audit log to show that. I had to change the context type:
$ ls -Z ~/myproject
drwxrwxr-x. admin admin unconfined_u:object_r:user_home_t:s0 myprojectenv
-rwxrw-r--. admin admin unconfined_u:object_r:user_home_t:s0 myproject.wsgi
$ chcon -t httpd_sys_content_t ~/myproject/myproject.wsgi
$ ls -Z ~/myproject/myproject.wsgi
-rwxrw-r--. admin admin unconfined_u:object_r:httpd_sys_content_t:s0 myproject.wsgi
I my case I also overlooked that my test project needed to define a function called 'application' instead of something else, and I renamed it to .wsgi from .py (though, I think that's just convention).
The mod_wsgi documentation's section on checking your installation was also quite helpful, specifically making sure I wasn't running a mod_wsgi compiled against a different version of python or something silly like that.
https://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html

Related

Deploy Django app on Apache server on CentOS 7

I am following this article to deploy a Django app on Apache in CentOS 7.
I have some differences compared to that article:
1 - I used port 443 for https (my machine already has port 443 open with my_app_dns)
2 - My virtual host config file /etc/httpd/conf.d/django.conf is as following:
<VirtualHost *:80>
ServerAdmin xxx#xxx.com
ServerName my_app_dns
DocumentRoot /home/centos/path_to_my_app
Alias /static /home/centos/path_to_my_app/static
<Directory /home/centos/path_to_my_app/static>
Require all granted
</Directory>
#ErrorLog /logs/apis_error.log
#CustomLog /logs/apis_access.log combined
WSGIPassAuthorization On
WSGIDaemonProcess my_app python-path=/home/centos/path_to_my_app:/home/centos/.local/share/virtualenvs/my_app-8BiokhAz/lib/python3.9/site-packages
WSGIProcessGroup my_app
WSGIScriptAlias / /home/centos/path_to_my_app/wsgi.py
<Directory /home/centos/path_to_my_app>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
3 - My app uses Postgres database instead of SQlite3.
The DocumentRoot is set to /var/www in /etc/httpd/conf/httpd.conf. I have also set up self-signed SSL certificate with the file etc/httpd/conf.d/ssl.conf). Below is some of the content of the ssl.conf file:
Listen 443 https
...
<VirtualHost _default_:443>
DocumentRoot "/var/www"
ServerName my_app_dns:443
...
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
...
</VirtualHost>
With the above setup, when I go to https://my_app_dns, I see the sample Apache Test page.
If I change <VirtualHost *:80> to <VirtualHost *:443> in the file /etc/httpd/conf.d/django.conf, going to https://my_app_dns yields this error:
my_app_dns sends an invalid response
ERR_SSL_PROTOCOL_ERROR
In folder /etc/httpd/conf.d/, I have two files : ssl.conf and django.conf. Both of these define virtual hosts. It seems only the config in ssl.conf takes effect and not the file django.conf).
I followed this article for the SSL setup.
What am I missing in this setup sequence in order to deploy the Django app content instead of the Apache Test page?
Update:
Following #Lingyan Meng's comment, I transfer the django.conf virtual host info to the ssl.conf virtual host and delete django.conf. After restarting my webpage, I see this error on the browser:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at root#localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
So I checked a few log files for clues. In this log file, /var/log/httpd/ssl_error_log, I see the following error:
[Wed Oct 13 22:37:05.642226 2021] [ssl:warn] [pid 2526] AH01906: RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
[Wed Oct 13 22:37:05.697737 2021] [ssl:warn] [pid 2526] AH01906: RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
[Wed Oct 13 22:37:11.890802 2021] [mime_magic:error] [pid 2531] [client xxx.x.xxx.xx:62352] AH01512: mod_mime_magic: can't read `/home/centos/path_to_my_app/my_app/wsgi.py'
[Wed Oct 13 22:37:11.891123 2021] [mime_magic:error] [pid 2531] [client xxx.x.xxx.xx:62352] AH01512: mod_mime_magic: can't read `/home/centos/path_to_my_app/my_app/wsgi.py'
[Wed Oct 13 22:37:11.899659 2021] [:error] [pid 2527] (13)Permission denied: [remote xx.x.xx.xx:116] mod_wsgi (pid=2527, process='my_app', application='my_app_dns|'): Call to fopen() failed for '/home/centos/path_to_my_app/my_app/wsgi.py'.
[Wed Oct 13 22:37:12.185359 2021] [mime_magic:error] [pid 2532] [client xx.x.xx.xx:51889] AH01512: mod_mime_magic: can't read `/home/centos/path_to_my_app/my_app/wsgi.py', referer: https://my_app_dns/
I found a thread with a similar problem here. The suggested solution was to change the ownership of my_app folder. So I did this:
sudo chown -R apache:apache ~/path_to_myapp
Then, I verify it with:
ls -l ~/path_to_my_app
which shows:
drwxrwxr-x. 2 apache apache 89 Oct 8 21:33 my_app
-rwxrwxr-x. 1 apache apache 661 Oct 7 19:57 manage.py
drwxrwxr-x. 3 apache apache 19 Oct 8 21:59 static
So, server apache now owns the folder. But when I restart the server, I still see the same error message in ssl_error_log file.
Also, below is the full content of my /etc/httpd/conf.d/ssl.conf
Listen 443 https
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout 300
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
<VirtualHost _default_:443>
DocumentRoot "/home/centos/path_to_my_app"
ServerName my_app_dns
ErrorLog logs/ssl_error_log
TransferLog logs/ssl_access_log
LogLevel warn
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
<Files ~ "\.(cgi|shtml|phtml|php3?)$">
SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
Alias /static /home/centos/path_to_my_app/static
<Directory /home/centos/path_to_my_app/static>
Options FollowSymLinks
Order allow,deny
Allow from all
Require all granted
</Directory>
WSGIPassAuthorization On
WSGIDaemonProcess my_app python-path=/home/centos/path_to_my_app:/home/centos/.local/share/virtualenvs/my_app-8BiokhAz/lib/python3.9/site-packages
WSGIProcessGroup my_app
WSGIScriptAlias / /home/centos/path_to_my_app/my_app/wsgi.py
<Directory /home/centos/path_to_my_app/my_app>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
BrowserMatch "MSIE [2-5]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
CustomLog logs/ssl_request_log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLProtocol All -SSLv2 -SSLv3
SSLHonorCipherOrder On
Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains"
Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff
SSLCompression off
SSLUseStapling off
SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
If someone could advise what is going on?
You may remove the django.conf, and move your settings in VirualHost in django.conf into ssl.conf and try again.

apache2 not showing flask app on local server

I was following this tutorial:
https://pythonprogramming.net/creating-first-flask-web-app/?completed=/flask-web-development-introduction/
On my local server (home machine on local network).
Flask itself starts properly
>>> python /var/www/FlaskApp/FlaskApp/__init__.py
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [31/Oct/2016 22:56:29] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [31/Oct/2016 22:56:29] "GET /favicon.ico HTTP/1.1" 404 -
flask starts
But Apache is showing its default webpage no matter what.
sudo a2enmod wsgi
Module wsgi already enabled
cat /var/www/FlaskApp/FlaskApp/init.py
from flask import Flask
app = Flask(__name__)
#app.route('/')
def homepage():
return "Hi There, how you're doin?"
if __name__ == "__main__":
app.run()(debug=True)
cat /etc/apache2/sites-available/FlaskApp.conf
<VirtualHost *>
ServerName dagzserv
ServerAdmin my#email.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Require all granted
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo a2ensite FlaskApp
Site FlaskApp already enabled
I have tried adding app.run(host='0.0.0.0', port=5000)
I do have /etc/hosts dagserv as my ip in localnetwork (10.0.1.xxx)
Also tried ServerName as localhost.
One thing i did not understood is how exactly Apache is finding that "FlaskApp.conf" but i assumed that it is scanning its config directory
(/etc/apache2/sites-available/FlaskApp.conf)
I am getting no errors in /var/logs/apache2
cat access.log
10.0.1.14 - - [31/Oct/2016:22:46:52 -0700] "GET / HTTP/1.1" 200 3524 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
10.0.1.14 - - [31/Oct/2016:22:49:14 -0700] "GET / HTTP/1.1" 200 3524 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
10.0.1.14 - - [31/Oct/2016:22:49:15 -0700] "GET / HTTP/1.1" 200 3523 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
10.0.1.101 - - [31/Oct/2016:22:56:18 -0700] "GET /favicon.ico HTTP/1.1" 404 499 "-" "Mozilla/5.0 (X11; Ubuntu; Linux armv7l; rv:49.0) Gecko/20100101 Firefox/49.0"
cat error.log
[Mon Oct 31 22:45:47.288264 2016] [wsgi:warn] [pid 982] mod_wsgi: Compiled for Python/2.7.11.
[Mon Oct 31 22:45:47.288623 2016] [wsgi:warn] [pid 982] mod_wsgi: Runtime using Python/2.7.12.
[Mon Oct 31 22:45:47.314433 2016] [mpm_prefork:notice] [pid 982] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Mon Oct 31 22:45:47.314616 2016] [core:notice] [pid 982] AH00094: Command line: '/usr/sbin/apache2'
[Mon Oct 31 22:48:52.171206 2016] [mpm_prefork:notice] [pid 982] AH00169: caught SIGTERM, shutting down
[Mon Oct 31 22:48:53.766824 2016] [wsgi:warn] [pid 2360] mod_wsgi: Compiled for Python/2.7.11.
[Mon Oct 31 22:48:53.766940 2016] [wsgi:warn] [pid 2360] mod_wsgi: Runtime using Python/2.7.12.
[Mon Oct 31 22:48:53.775947 2016] [mpm_prefork:notice] [pid 2360] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Mon Oct 31 22:48:53.776083 2016] [core:notice] [pid 2360] AH00094: Command line: '/usr/sbin/apache2'
1. How Apache is finding that "FlaskApp.conf"
There one line in /etc/apache2/apache2.conf to include virtual host configurations:
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
Apache2 server will load apache2.conf and also include the virtual host configurations.
2. Why Apache is showing its default webpage no matter what.
If you access the server via IP address, Apache will return based on its default site configuration in /etc/sites-available/000-default.conf. So if you want to access your WSGI server via IP address, you should disable the Apache default site configuration first:
sudo a2dissite 000-default.conf
service apache2 restart

Apache2 get 403 error when get Django project's static css file

I am a very newbie to Django and I just finished the 1st Tutorial (which is a poll website). Now I run it with
python3 manage.py runserver
and everything works fine. Now I am trying to deploy this project with apache2 and mod_wsgi, and I have tried the same documents, but how hard I have tried, I can't make the apache2 to access my static css file, even thought the django contents works fine (including access the DB file).
here is my django site conf:
<VirtualHost *:80>
ServerName localhost
Alias /static/ /home/coeus/projects/django/mysite/polls/static/
<Directory /home/coeus/projects/django/mysite/mysite/static>
Require all granted
</Directory>
WSGIScriptAlias / /home/coeus/projects/django/mysite/mysite/wsgi.py
<Directory /home/coeus/projects/django/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
I have tried to set the ownership for "mysite" to www-data:www-data, but it not help.
the Apache's access.log shows below:
127.0.0.1 - - [25/Jan/2015:16:18:40 +0800] "GET /static/polls/style.css HTTP/1.1" 403 522 "http://127.0.0.1/polls/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36"
I am totally stuck here...
Please help. thanks a lot!
What I have done was store my static files on public_dir where your index.fcgi and .htaccess lives and maybe in a folder 'static' then all you need to do is point to that folder from your html
<img src="/static/images/love.png" />
or
<img src="http://mydoman.me/static/images/love.png" />
It works fine from my side

Apache 2.4 module deployment not working

I little new to Apache HTTP 2.4 server and its module development. I have been playing around and created one sample mod_hello.so module to test. But facing the issue while deployment : -
Issue : - Not able to deploy the mod_hello.so file in Apache http 2.4 server
Steps performed : -
Copied the mod_hello.so file into the /usr/local/apache/modules/
directory
edited the httpd.conf file to load the module :- LoadModule
foo_module modules/mod_hello.so
Restarted the httpd service
But when i try to access using URL :localhost/foo i get 404
not found error on browser. In Actual it should produce 'Hello Word'
Message on browser.
In the logs i also checked but couldn't be able to find anything useful
Prior to above activity i have done the following
Build the Apache server 2.4 from source and intalled on fedora 15 system
For building used APR, APR-UTIL, PCRE
Everything went properly and able to compile the sample code and got the desired mod_hello.so file
Any help to debug this issue is highly appreciated
Logs
error_log
AH00558: httpd: Could not reliably determine the server's fully
qualified domain name, using localhost.localdomain. Set the
'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully
qualified domain name, using localhost.localdomain. Set the
'ServerName' directive globally to suppress this message
[Wed Nov 12 13:48:17.753498 2014] [core:warn] [pid 13514:tid
3077613776] AH00098: pid file /usr/local/apache/logs/httpd.pid
overwritten -- Unclean shutdown of previous Apache run?
access_log
::1 - - [12/Nov/2014:12:58:06 -0800] "GET / HTTP/1.1" 200 45
::1 - - [12/Nov/2014:12:58:06 -0800] "GET /favicon.ico HTTP/1.1" 404
209
::1 - - [12/Nov/2014:12:58:06 -0800] "GET /favicon.ico HTTP/1.1" 404
209
::1 - - [12/Nov/2014:13:48:24 -0800] "GET / HTTP/1.1" 200 45
::1 - - [12/Nov/2014:13:48:32 -0800] "GET /foo HTTP/1.1" 404 201
::1 - - [12/Nov/2014:13:48:34 -0800] "GET /foo HTTP/1.1" 404 201
Solution
I have created one fresh conf file in apache/conf/extra/mod_hello.conf and added following lines in it
LoadModule helloworld_module modules/libhello.so
<Location /helloworld>
SetHandler helloworld
</Location>
After the above change put added following line into httpd.conf file
Include conf/extra/mod_hello.conf
Restarted the server
./httpd -X

Bad request 400: nginx / gunicorn

I have followed this tutorial: http://blog.wercker.com/2013/11/25/django-16-part3.html and I am just trying to make it work locally with Vagrant for now. I am not trying to use Wercker.
After everything is installed, I try to access the website but I get a Bad Request (400) error every time. I do not know if that is due to a problem in nginx or in gunicorn.
They both have a log entry so at least I know that the request goes all the way through gunicorn and is not stopped at the nginx level.
Where is the problem located? Gunicorn? nginx?
Here are the logs of gunicorn and nginx.
I see that the favicon is missing but that only should not stop the page from being displayed right?
Gunicorn:
>>> cat /var/local/sites/hellocities/run/gunicorn.error.log
10.0.0.1 - - [28/Jan/2014:07:05:16] "GET / HTTP/1.0" 400 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:43] "GET / HTTP/1.0" 400 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
Nginx:
>>> cat /var/log/nginx/hellocities-access.log
10.0.0.1 - - [28/Jan/2014:07:05:16 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:05:20 +0000] "GET /favicon.ico HTTP/1.1" 404 200 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:43 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:44 +0000] "GET /favicon.ico HTTP/1.1" 404 200 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
>>> cat /var/log/nginx/hellocities-error.log
2014/01/28 07:05:20 [error] 13886#0: *1 open() "/var/local/sites/hellocities/static/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "10.0.0.200"
2014/01/28 07:09:44 [error] 13886#0: *3 open() "/var/local/sites/hellocities/static/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "10.0.0.200"
I had the same problem and adding ALLOWED_HOSTS = ("yourdomain.com",) to settings fixed it.
UPDATE: there few other possibilities:
Nginx (or whatever web server you use) doesn't pass the $host variable to the app
Host contains underscores
See details: https://blog.anvileight.com/posts/how-to-fix-bad-request-400-in-django/
As I was having the same issue (400 error code when trying to share with vagrant share), I stumble upon this question. The answer and comments are right, as the obvious solution is to set ALLOWED_HOSTS list, but I was already setting it correctly (I thought).
I can't speak for nginx as I'm running this on apache2, but here's what solved the issue:
Take a look at the ALLOWED_HOSTS doc to find what's best for your case.
With vagrant, you might find it useful to accept all the vagrantshare.com subdomain, so just add '.vagrantshare.com' (notice the dot) to the ALLOWED_HOSTS list.
Not sure if it is really necessary, but I changed the modified date of the wsgi.py file
touch wsgi.py
As I'm using apache2, I needed to restart the service.
sudo service apache2 restart
And then it worked.
I ran into this issue. It was because I forgot to add the proxy_set_header settings in the nginx config:
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
So Django didn't see the original hostname that was requested, so it didn't match with what was in ALLOWED_HOSTS. Then it gave back the 400 response.
After adding this to my nginx config (at the spot where you do the proxy_pass to Gunicorn) and then restarting nginx, it worked.
More info: https://docs.gunicorn.org/en/stable/deploy.html#nginx-configuration