Bad Request and Invalid HTTP_HOST header in deployment NGINX + Gunicorn + Django?

The server at DigitalOcean with Ubuntu 24.04 (LTS) x64

If to input http and https into command line

# curl -v http://likarnet.com
* Host likarnet.com:80 was resolved.
* IPv6: (none)
* IPv4: 64.225.77.248
*   Trying 64.225.77.248:80...
* Connected to likarnet.com (64.225.77.248) port 80
> GET / HTTP/1.1
> Host: likarnet.com
> User-Agent: curl/8.5.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.24.0 (Ubuntu)
< Date: Tue, 25 Jun 2024 14:46:35 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 6308
< Connection: keep-alive
< X-Frame-Options: DENY
< Vary: Cookie
< X-Content-Type-Options: nosniff
< Referrer-Policy: same-origin
< Cross-Origin-Opener-Policy: same-origin
< 
<!doctype html> here is expected html-code
# curl -v https://likarnet.com
* Host likarnet.com:443 was resolved.
* IPv6: (none)
* IPv4: 64.225.77.248
*   Trying 64.225.77.248:443...
* connect to 64.225.77.248 port 443 from 64.225.77.248 port 39420 failed: Connection refused
* Failed to connect to likarnet.com port 443 after 2 ms: Couldn't connect to server
* Closing connection
curl: (7) Failed to connect to likarnet.com port 443 after 2 ms: Couldn't connect to server
# curl -v 64.225.77.248
*   Trying 64.225.77.248:80...
* Connected to 64.225.77.248 (64.225.77.248) port 80
> GET / HTTP/1.1
> Host: 64.225.77.248
> User-Agent: curl/8.5.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.24.0 (Ubuntu)
< Date: Tue, 25 Jun 2024 14:49:33 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 6308
< Connection: keep-alive
< X-Frame-Options: DENY
< Vary: Cookie
< X-Content-Type-Options: nosniff
< Referrer-Policy: same-origin
< Cross-Origin-Opener-Policy: same-origin
< 
<!doctype html> and here - expected html-code

The questions above are referencing what you’re doing in the browser. (You’ve said that curl is working locally, so we can skip that for now. We need to find out why your browser reference is not working.)

On the server:

  • What is the output of a ufw status verbose command?

  • What is the output of a netstat -anp | grep nginx command?

(Both these commands need to be run as root.)

1 Like

Wooow, I am really sorry that I do not understand some obvious things.
When I use directly http://likarnet.com - it works. My static files, favicon aren’t loaded yet but I get html page)))

And now,

# ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp (OpenSSH)           ALLOW IN    Anywhere                  
80,443/tcp (Nginx Full)    ALLOW IN    Anywhere                  
22/tcp (OpenSSH (v6))      ALLOW IN    Anywhere (v6)             
80,443/tcp (Nginx Full (v6)) ALLOW IN    Anywhere (v6)
# netstat -anp | grep nginx
Command 'netstat' not found, but can be installed with:
apt install net-tools

Should I install it?

Cool!

Then no, at this point I don’t think you need to install anything else. You’ve got a functional web site!

The next step then would be to get your environment variables working.

1 Like

Thank you, sir! Very much! You helped me a lot! I got a bunch of new knowledge and skills with your advise

To view static files, you might need to run collectstatic command before deployment. Also, you might need to point to STATIC_ROOT variable.

1 Like