Nginx http 到 http 重定向 400 纯 HTTP 请求已发送到 HTTPS 端口

Nginx http 到 http 重定向 400 纯 HTTP 请求已发送到 HTTPS 端口

我有一个应用程序在端口 httphttp://app1.internal.example.com:8080和端口 https 上公开https://app1.internal.example.com:8443

所以我有 80 和 443 nginx 服务器块来帮助将来自 http 端口的请求重定向到应用程序的 https 端口

$ curl http://app1.internal.example.com:8080

<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
</body>
</html>

$ curl -IL http://app1.internal.example.com:8080

HTTP/1.1 400 Bad Request
Date: Wed, 09 Nov 2022 00:39:30 GMT
Content-Type: text/html
Content-Length: 220
Connection: close

以下是 https 端口上的 curl,返回 200

$ curl -IL https://app1.internal.example.com:8443

HTTP/1.1 200 OK
Date: Wed, 09 Nov 2022 00:38:00 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 1274335
Connection: keep-alive
Keep-Alive: timeout=20
Vary: Accept-Encoding

以下是我的 nginx 服务器块,用于 http 到 https 重定向

我该如何修复此错误以便 curl 可以在 http 请求中返回 200

server {
    listen 80;
    listen [::]:80;

    server_name app1.internal.example.com;

    return 301 https://app1.internal.example.com:8443$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name app1.internal.example.com;

    location ^~ / {
        proxy_pass http://localhost:5000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}

答案1

问题已修复,恰巧有另一个配置文件的 80 端口正在监听 SSL,这导致了错误

server {
    listen 80 ssl;
    listen [::]:80 ssl;

    server_name another-app1.internal.example.com;

    return 301 https://another-app1.internal.example.com:8443$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name another-app1.internal.example.com;

    location ^~ / {
        proxy_pass http://localhost:5000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}

所以我只需要从端口 80 中删除 SSL,现在一切似乎都运行正常

因此,没有 ssl 的 http 块应该是这样的,一些较旧的 nginx 可能没有 ssl ssl on;,您必须从端口 80 或 http 端口中删除它。检查整个配置文件,确保没有遗漏任何内容,这样就没问题了

server {
    listen 80;
    listen [::]:80;

    server_name another-app1.internal.example.com;

    return 301 https://another-app1.internal.example.com:8443$request_uri;
}

...
...

相关内容