我尝试为 prometheus 设置 nginx 导出器,但在正确配置 nginx 方面遇到了困难。
我有一个默认服务器块,它将所有请求从端口 80 重定向到 443。因此,所有不适合在 80 上监听的其他服务器块的请求都应重定向。但我对 status.localhost 的测试 curl 请求也被重定向到 https(这个不应重定向,但应返回 nginx 状态页面)。
redirect_80to443.conf 的内容
server {
listen 80 default;
location / {
return 301 https://$host$request_uri;
}
}
surveillance_status_page.conf的内容
server {
server_name status.localhost;
#listen localhost:80;
listen 127.0.0.1:80;
keepalive_timeout 0;
access_log off;
allow 127.0.0.1;
deny all;
location /nginx_status {
stub_status on;
}
}
卷曲http://status.localhost/nginx_status
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
curl -Ls -o /dev/null -w %{url_effective}\nhttp://status.localhost/nginx_status
(来源:获取 curl 重定向后的最终 URL)
https://status.localhost/nginx_status
我还添加了以下内容
events {
...
debug_connection 127.0.0.1;
}
在日志中我得到
2023/10/03 18:37:35 [debug] 20873#20873: *790 accept: 127.0.0.1:49742 fd:59
2023/10/03 18:37:35 [debug] 20873#20873: *790 event timer add: 59: 60000:31696116931
2023/10/03 18:37:35 [debug] 20873#20873: *790 reusable connection: 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 epoll add event: fd:59 op:1 ev:80002001
2023/10/03 18:37:35 [debug] 20873#20873: *790 http check ssl handshake
2023/10/03 18:37:35 [debug] 20873#20873: *790 http recv(): 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 https ssl handshake: 0x16
2023/10/03 18:37:35 [debug] 20873#20873: *790 tcp_nodelay
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL server name: "status.localhost"
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL ALPN supported by client: h2
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL ALPN supported by client: http/1.1
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL ALPN selected: http/1.1
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL_do_handshake: 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL: TLSv1.2, cipher: "ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH Au=$
2023/10/03 18:37:35 [debug] 20873#20873: *790 reusable connection: 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 http wait request handler
2023/10/03 18:37:35 [debug] 20873#20873: *790 malloc: 000055CC4997FD20:1024
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL_read: 0
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL_get_error: 6
2023/10/03 18:37:35 [debug] 20873#20873: *790 peer shutdown SSL cleanly
2023/10/03 18:37:35 [info] 20873#20873: *790 client closed connection while waiting for request, client: 127.0.0.1, $
2023/10/03 18:37:35 [debug] 20873#20873: *790 close http connection: 59
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL_shutdown: 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 event timer del: 59: 31696116931
2023/10/03 18:37:35 [debug] 20873#20873: *790 reusable connection: 0
2023/10/03 18:37:35 [debug] 20873#20873: *790 free: 000055CC4997FD20
2023/10/03 18:37:35 [debug] 20873#20873: *790 free: 000055CC49993930, unused: 48
2023/10/03 18:37:35 [debug] 20873#20873: *790 accept: 127.0.0.1:49742 fd:59
2023/10/03 18:37:35 [debug] 20873#20873: *790 event timer add: 59: 60000:31696116931
2023/10/03 18:37:35 [debug] 20873#20873: *790 reusable connection: 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 epoll add event: fd:59 op:1 ev:80002001
2023/10/03 18:37:35 [debug] 20873#20873: *790 http check ssl handshake
2023/10/03 18:37:35 [debug] 20873#20873: *790 http recv(): 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 https ssl handshake: 0x16
2023/10/03 18:37:35 [debug] 20873#20873: *790 tcp_nodelay
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL server name: "status.localhost"
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL ALPN supported by client: h2
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL ALPN supported by client: http/1.1
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL ALPN selected: http/1.1
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL_do_handshake: 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL: TLSv1.2, cipher: "ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH Au=$
2023/10/03 18:37:35 [debug] 20873#20873: *790 reusable connection: 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 http wait request handler
2023/10/03 18:37:35 [debug] 20873#20873: *790 malloc: 000055CC4997FD20:1024
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL_read: 0
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL_get_error: 6
2023/10/03 18:37:35 [debug] 20873#20873: *790 peer shutdown SSL cleanly
2023/10/03 18:37:35 [info] 20873#20873: *790 client closed connection while waiting for request, client: 127.0.0.1, $
2023/10/03 18:37:35 [debug] 20873#20873: *790 close http connection: 59
2023/10/03 18:37:35 [debug] 20873#20873: *790 SSL_shutdown: 1
2023/10/03 18:37:35 [debug] 20873#20873: *790 event timer del: 59: 31696116931
2023/10/03 18:37:35 [debug] 20873#20873: *790 reusable connection: 0
2023/10/03 18:37:35 [debug] 20873#20873: *790 free: 000055CC4997FD20
2023/10/03 18:37:35 [debug] 20873#20873: *790 free: 000055CC49993930, unused: 48
不幸的是,以我目前的知识,这并不能真正帮助我理解 nginx 如何决定服务器和位置块。
除了调试问题之外,我还知道我做错了什么或我还能做什么吗?