nginx exporter 配置
docker compose 中的配置
开启 nginx 状态页
server {
listen 8080;
server_name localhost;
location /stub_status {
stub_status;
access_log off;
#allow 172.31.0.0/16;
#deny all;
}
}
nginx 镜像,挂载配置
nginx:
image: nginx:1.22.1
volumes:
- ./nginx/stub_status-server.conf:/etc/nginx/conf.d/stub_status-server.conf:ro
networks:
- monitoring
expose:
- 8080
- 80
ports:
- 80:80
nginx-exporter
nginx-exporter:
image: nginx/nginx-prometheus-exporter:0.11
command:
- '-nginx.scrape-uri=http://nginx:8080/stub_status'
networks:
- monitoring
ports:
- '9113:9113'
depends_on:
- nginx
k8s 中的配置
nginx 的配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.22.1
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx/conf.d/stub_status-server.conf
subPath: stub_status-server.conf
name: nginx-config
- name: nginx-exporter
image: nginx/nginx-prometheus-exporter:0.11
args:
- "-nginx.scrape-uri=http://127.0.0.1:8080/nginx_status"
ports:
- containerPort: 9113
volumes:
- name: nginx-config
configMap:
name: nginx-config
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
stub_status-server.conf: |
# 这里是上面展示的 NGINX 配置
server {
# 监听 80 端口访问会报 404,换成 8080 端口就可以。
listen 8080;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
在 Nginx 配置中,可能有多个原因导致/nginx_status
路径在端口80上无法正常工作。其中一个原因是:
- 端口冲突或者重写规则
如果你在 Nginx 中有其它配置或者虚拟主机,也在监听端口 80,并且这些配置没有正确定义/nginx_status
路径,可能会导致请求被其他配置截获,从而返回 404 错误。
解决方法:
确保stub_status
配置文件是唯一在端口 80 上处理/nginx_status
请求的配置,或者明确定义其它服务器块不处理此路径。
产生 404 的原因是被 nginx 原有的 location 拦截了,去 /usr/share/nginx/html 目录下找 nginx_status 文件了。