nginx配置监控模块(--with-http_stub_status_module)

516 阅读1分钟

编译

./configure --with-debug --with-http_stub_status_module
make -j 8
make install

nginx配置文件

#user  nobody;
worker_processes  2;

error_log  logs/debug.log  debug;

events {
    worker_connections  1024;
}

http {
    #include       mime.types;
    #default_type  application/octet-stream;
    
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  60;
	
    server {
        client_max_body_size 4G;
        listen 80;
        server_name www.haha.com;
        root /data;
		
        location / {
            autoindex on;
            autoindex_exact_size on;
            autoindex_localtime on;
        }	
		
		location /status {
			stub_status on;
			access_log on;
			allow 127.0.0.1;
			deny all;
		}
    }
}

注意allow 127.0.0.1只允许本机访问,其他机器访问会返回403

测试

本地访问/status正常
accepts表示accept函数返回的socket个数,也就是累计创建tcp链接的个数
requests表示收到的http请求数

其他机器访问返回403

# curl -L -v "http://192.168.116.131/status"
*   Trying 192.168.116.131...
* TCP_NODELAY set
* Connected to 192.168.116.131 (192.168.116.131) port 80 (#0)
> GET /status HTTP/1.1
> Host: 192.168.116.131
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 403 Forbidden
< Server: nginx/1.18.0
< Date: Fri, 25 Sep 2020 08:55:33 GMT
< Content-Type: text/html
< Content-Length: 153
< Connection: keep-alive
<
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
* Connection #0 to host 192.168.116.131 left intact