nginx 400: a plain HTTP request reached the HTTPS port
400 Bad RequestShort answer
nginx returns this 400 when a client speaks plain HTTP to a port that is configured for TLS. Either the URL uses http:// on the HTTPS port, or a proxy in front sends http:// to a TLS port. Use https:// in the URL or proxy_pass, or add an error_page 497 redirect.
Fix it fast
1Find out which cause you have
See what the failing request looks like and which listen line owns that port.
curl -i http://example.com:443/ sudo nginx -T | grep -n "listen"2Match what you see, then jump to its fix
- 1
Browsers show it for an address like http://example.com:8443, curl for http://host:443The client uses http:// on a port that expects TLSFix - 2
The 400 text comes from the backend, and the nginx doing the proxying uses proxy_pass http://host:443A proxy talks http:// to a backend that only accepts TLSFix
- 1
What does the error look like?
400 Bad RequestThe plain HTTP request was sent to HTTPS port
Cause 1 of 2
The client uses http:// on a port that expects TLS
1Check
Find the server block on that port. A listen line with the ssl parameter means every connection on that port must use TLS, so a plain request is rejected.
sudo nginx -T | grep -n -E "listen .*ssl"2Fix
Use https:// in the address. To redirect plain requests that arrive on the TLS port, nginx has an internal status 497 for exactly this case, and error_page can send those to https.
server {
listen 8443 ssl;
server_name example.com;
error_page 497 https://$host:$server_port$request_uri;
}3Verify
Reload and open the plain address again; it should redirect.
sudo nginx -t && sudo nginx -s reload
curl -I http://example.com:8443/Confirmed by nginx.org
Cause 2 of 2
A proxy talks http:// to a backend that only accepts TLS
1Check
Look at the scheme in proxy_pass. nginx accepts http or https there, and the scheme decides whether it opens a TLS connection to the backend.
grep -R -n "proxy_pass" /etc/nginx/2Fix
Change the scheme to https and, if the backend needs the host name for TLS, turn on SNI.
location / {
proxy_pass https://backend.example.com:443;
proxy_ssl_server_name on;
}3Verify
Reload and request the site through the proxy.
sudo nginx -t && sudo nginx -s reload
curl -I https://example.com/Confirmed by nginx.org
How do I stop it coming back?
- Serve HTTP on port 80 only to redirect, and keep TLS on 443; do not mix both on one port.
- Write the scheme and port together in every proxy_pass.
- Test redirects with curl -I from outside the server after each change.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Servers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
Argo CD behind an nginx Ingress loops with too many redirects
307 Temporary Redirect on every request to the Argo CD host
The browser and Argo CD disagree about who handles TLS.
3 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Docker php image has no php.ini and my edits do not stay
A setting changed in php.ini inside the container is gone after the container is recreated
The official php image ships php.ini-development and php.ini-production but no active php.ini.
2 causes, each with a fix