Warm tip: This article is reproduced from serverfault.com, please click

nginx-原始HTTP请求已发送到HTTPS端口

(The plain HTTP request was sent to HTTPS port)

发布于 2021-03-19 07:43:04

我有下一个NGINX代理配置

http {
    server_tokens off;
server {
    listen     7443 ssl;
    ssl_certificate     /etc/nginx/ssl/star.crt;
    ssl_certificate_key /etc/nginx/ssl/star.key;
    location /prometheus/ {
      auth_basic           "Prometheus";
      auth_basic_user_file /etc/nginx/.htpasswd;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      # I leave this hardcoded for now
      proxy_pass http://prometheus:9090/prometheus/;
   }

    location / {
      auth_basic           "Prometheus";
      auth_basic_user_file /etc/nginx/.htpasswd;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      # I leave this hardcoded for now
      proxy_pass http://alertmanager:9093;
   }
 }
}
events {}

如果通过使用端口7443输入未指定HTTPS的浏览器URL,则会得到:

The plain HTTP request was sent to HTTPS port

例如:prometheus.example.com:7443/prometheus/或prometheus.example.com:7443/#/alerts/

如果我指定不带HTTPS的URL,如何使NGINX自动重定向到HTTPS?

Questioner
user54
Viewed
55
user54 2019-02-06 17:41:35

实际上,解决方案非常简单。在服务器部分的“听”字符串下,我只需要添加:

error_page 497 https://$host:$server_port$request_uri;

497代码是发送到HTTPS端口的HTTP请求。error_page处理此代码并重定向到https:// $ host:$ server_port $ request_uri;

在哪里:

$ host是保留变量,代表正在运行NGINX的主机名。

$ server_port是保留变量,表示在服务器部分中声明的侦听端口。

$ request_uri是reserverd变量,代表完整的原始请求URI(带有参数)。

最新帖子

最新文章