大网数据广告活动
服务器运维问答

The plain HTTP request was sent to HTTPS port

· 52CE 我爱测

I have the next NGINX proxy configuration 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"; …

问题原文

原帖链接 · user54 · CC BY-SA 4.0

I have the next NGINX proxy configuration

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 {}

If I enter in browser URL without specification of HTTPS by using port 7443, I get:

The plain HTTP request was sent to HTTPS port

For example: prometheus.example.com:7443/prometheus/ or prometheus.example.com:7443/#/alerts/

How can I make NGINX redirect to HTTPS automatically if I specify such URLs without HTTPS?

已采纳解答

原帖链接 · user54 · CC BY-SA 4.0

Actually the solution was pretty straightforward. In server section under "listen" string I was needed to add only:

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

497 code is HTTP Request Sent to HTTPS Port. error_page handles this code and redirects to https://$host:$server_port$request_uri;

where:

$host is reserved variable which represents hostname on which NGINX is being run.

$server_port is reserved variable which represents listining port which is declared in server section.

$request_uri is reserverd variable which represents full original request URI (with arguments).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-02-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。