Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx 400: a plain HTTP request reached the HTTPS port

Error400 Bad Request

Short 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.

  • Applies to nginx 1.26, nginx 1.28
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1Browsers 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. 2The 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

What does the error look like?

  • 400 Bad Request
  • The 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

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.