I have a site set up this way: nginx as a proxy server, proxying requests tu a gunicorn instance serving a Django site through a UNIX socket.
This is my nginx configuration:
server {
listen 80;
server_name api.mysite.com;
location /static/ {
alias /webapps/mysite/static/;
autoindex off;
}
location / {
include proxy_params;
proxy_pass http://unix:/webapps/mysite/mysite.sock;
}
}
Is my understanding that nginx, when receiving a request, matches the Host
header against the server_name parameter of the server block and, if it matches, it serves it. However, nginx seems to be trying to serve (passing the request to my Django server) request with a Host
header different than api.mysite.com. Django has a setting named ALLOWED_HOSTS
(in my case set to ['api.mysite.com']
) that performs further checking of the Host
header and raises an error if the request Host
header doesn't match, which shouldn't happen because nginx is supposedly already filtering this. The thing is that I'm seeing errors raised by Django which look like this:
A couple of things:
Host
header set to the path on the local filesystem of my gunicorn/nignx UNIX socket.Any clue?
Turns out that if nginx doesn't encounter a matching server block, it will send the request to the first server block. So the solution was to set up a default server block that drops every request like this:
server {
listen 80 default_server;
return 444;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With