use*_*r54 3 nginx reverse-proxy
我有下一个 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 {}
Run Code Online (Sandbox Code Playgroud)
如果我使用端口 7443 在没有指定 HTTPS 的浏览器 URL 中输入,我会得到:
The plain HTTP request was sent to HTTPS port
Run Code Online (Sandbox Code Playgroud)
例如:prometheus.example.com:7443/prometheus/ 或 prometheus.example.com:7443/#/alerts/
如果我在没有 HTTPS 的情况下指定此类 URL,如何使 NGINX 自动重定向到 HTTPS?
实际上,解决方案非常简单。在“listen”字符串下的服务器部分,我只需要添加:
error_page 497 https://$host:$server_port$request_uri;
Run Code Online (Sandbox Code Playgroud)
497 代码是发送到 HTTPS 端口的 HTTP 请求。error_page 处理此代码并重定向到 https://$host:$server_port$request_uri;
在哪里:
$host 是保留变量,表示运行 NGINX 的主机名。
$server_port 是保留变量,表示在 server 部分声明的列表端口。
$request_uri 是保留变量,代表完整的原始请求 URI(带参数)。
| 归档时间: |
|
| 查看次数: |
11148 次 |
| 最近记录: |