Nginx monitors the real-time status configuration and views the website operation in real time
Introduction:
Nginx is a very popular reverse proxy server. Its high performance and high concurrency capabilities make it the preferred choice for many websites. First choice. In order to ensure the stable operation of the website, we need to monitor the running status of Nginx at all times. This article will introduce how to configure Nginx real-time status monitoring, and use sample code to give readers a better understanding.
1. Install the Nginx status monitoring module
To achieve real-time status monitoring of Nginx, you need to install the ngx_http_stub_status module on Nginx. First, make sure Nginx has been installed, then go to the Nginx source code directory and execute the following command:
./configure --prefix=/usr/local/nginx --add-module=../ngx_http_stub_status_module make make install
After the installation is complete, add the following configuration items to the Nginx configuration file:
location /status { stub_status; access_log off; allow 127.0.0.1; deny all; }
Restart Nginx Make the configuration take effect:
/usr/local/nginx/sbin/nginx -s reload
You can now view the real-time status information of Nginx by accessing "http://yourdomain/status".
2. Nginx status monitoring data format description
Nginx status monitoring data is a simple text format, including the current number of connections, number of requests, number of bytes read and written, and other information. The following is an example:
Active connections: 10 server accepts handled requests 10000 10000 10000 Reading: 0 Writing: 1 Waiting: 9
Among them, "Active connections" indicates the current number of active connections, "Reading" indicates that the number of connections requested by the client is being read, and "Writing" indicates that the response is being written to the client. The number of connections, "Waiting" indicates the number of idle connections waiting for client requests.
3. Get Nginx status in real time through code
The following is a code example written in Python, which can obtain Nginx status information in real time and display it.
import requests response = requests.get('http://yourdomain/status') status = response.text.split(' ') active_connections = status[0].split(':')[1].strip() reading_connections = status[3].split(':')[1].strip() writing_connections = status[4].split(':')[1].strip() waiting_connections = status[5].split(':')[1].strip() print('活动连接数:', active_connections) print('正在读取连接数:', reading_connections) print('正在写入连接数:', writing_connections) print('等待连接数:', waiting_connections)
Through the above code, we can obtain key information such as the number of Nginx connections in real time and print it out. You can also further process the obtained status information according to your needs, such as saving it to the database for subsequent analysis.
Conclusion:
Nginx’s real-time status monitoring is very important to ensure the stable operation of the website. By installing and configuring the ngx_http_stub_status module, we can easily obtain real-time status data of Nginx. Through code examples, we can obtain and display Nginx status information in real time, allowing us to better understand and maintain our website. I hope this article will be helpful to everyone in Nginx status monitoring!
The above is the detailed content of Nginx monitors real-time status configuration and views website operation in real time. For more information, please follow other related articles on the PHP Chinese website!