others-How to monitor nginx in command line using stub status module in nginx?

1. Purpose

In this post, I would demonstrate how to use stub status module in nginx, you can use it to monitor your nginx connections and requests’s status in command line.



2. The solution

2.1 Add the module to nginx

Check if you have stub_status module enabled in nginx:

nginx -V 2>&1|grep --color status

If you find this in the result, then the module is enabled in your nginx:

--with-http_stub_status_module

Otherwise , you need to enable it from compiling nginx source:

First get all the compile options in nginx:

nginx -V
configure arguments: xxxxxxxxxxxx

Copy and paste or the configure arguments and compile the nginx from source again with additional option:

./configure xxxxxxxxxxxx --with-http_stub_status_module

replace the xxxxxxxxxxxx with your nginx options.

Then

make && make install



2.2 Config file in nginx

Add this line in your location config:

stub_status;

For example:

    location / {
       stub_status;
    }


Then we can test it:

 ⚡ root@launch-advisor-20191120  /etc/nginx  curl http://localhost:81
Active connections: 1
server accepts handled requests
 2 2 2
Reading: 0 Writing: 1 Waiting: 0

Active connections: Number of active connections made to the backend.

Server accepts handled requests: Nginx processed a total of xxx connections, successfully created yyy handshakes (proving that there was no failure in the middle), and processed a total of zzz requests.

Reading: Nginx The number of header information read to the client.

Writing: Nginx The number of header information returned to the client.

Waiting: When keep-alive is enabled, this value is equal to active - (reading + writing), which means that Nginx has completed processing and is waiting for the resident connection of the next request command.

Therefore, when the access efficiency is high and the request is processed quickly, it is normal for the number of Waiting to be large. If the number of reading + writing is large, it means that the concurrent access volume is very large and is being processed.


2.3 Add security control on status module

    location / {
       stub_status;
       allow 127.0.0.1;	#only allow requests from localhost
 	     deny all;		#deny all other hosts
    }

Then only you can visit the status module , others are all denied.

3. Summary

In this post, I demonstrated how to enable and use the stub_status module in nginx . That’s it, thanks for your reading.