Home  >  Article  >  Operation and Maintenance  >  Nginx monitors real-time status configuration and keeps abreast of website operation status

Nginx monitors real-time status configuration and keeps abreast of website operation status

WBOY
WBOYOriginal
2023-07-04 12:33:152178browse

Nginx monitors the real-time status configuration and understands the running status of the website in time

When the website is running, it is very important to understand the real-time status of the server. Nginx is a very popular web server software that provides many monitoring functions to help us understand the operation of the website. This article will introduce how to configure Nginx real-time monitoring and provide some code examples for reference.

1. Install Nginx
First, we need to install the Nginx server. You can compile and install it, or you can use a package manager to install it. Taking the Ubuntu system as an example, you can use the following command to install:

sudo apt-get update
sudo apt-get install nginx

After the installation is completed, you can use the following command to verify whether the installation is successful:

nginx -v

If the version number can be displayed, the installation is successful. .

2. Configure Nginx monitoring module
Nginx provides a very convenient module called ngx_http_stub_status_module, which can be used to monitor the status of Nginx in real time. The following are the steps to configure this module:

  1. Confirm the location of the Nginx configuration file. Normally, the Nginx configuration file is located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf. You can use the following command to confirm the location of the configuration file:

    nginx -t
  2. Edit the configuration file. Use a text editor to open the Nginx configuration file:

    sudo vi /etc/nginx/conf.d/default.conf
  3. Add the following content in the configuration file:

    location /nginx_status {
     stub_status on;
     access_log off;
     allow 127.0.0.1;
     deny all;
    }

    The above configuration will be in the path /nginx_statusEnable status monitoring under # and limit access to local access only.

  4. Save and exit the configuration file. Restart the Nginx service to make the configuration take effect:

    sudo service nginx restart

3. Access the Nginx status monitoring page
After the configuration is completed, you can access the Nginx status monitoring page through the browser. Enter http://your_domain/nginx_status in the browser address bar, where your_domain is your server domain name or IP address. If the configuration is correct, you will see a page similar to the following:

Active connections: 1
server accepts handled requests
 16630948 16630948 31070446
Reading: 0 Writing: 1 Waiting: 0

Among them, Active connections represents the current number of active connections; accepts, handled and requests indicate the total number of requests since Nginx started; Reading, Writing and Waiting indicate that Nginx is reading requests, writing Incoming responses and number of waiting connections.

4. Use monitoring API to obtain status data
If you want to obtain Nginx status data programmatically, you can use Nginx's monitoring API. The following is a sample code written in Python to obtain Nginx status information:

import requests

def get_nginx_status():
    url = 'http://your_domain/nginx_status'  # 替换成你的nginx状态监控页面
    response = requests.get(url)
    if response.status_code == 200:
        status = response.text
        return status
    else:
        return None

status = get_nginx_status()
if status:
    print(status)
else:
    print('Failed to get Nginx status')

Note that the url variable is replaced with the URL of your Nginx status monitoring page.

You can get Nginx status information by calling the get_nginx_status function and print it out. The status information returned by this function is a string, which is the same as what you see when accessing the status monitoring page through a browser.

Summary:
Nginx provides powerful monitoring functions that can help us understand the operation of the website in real time. By configuring the ngx_http_stub_status_module module, we can directly access the Nginx status monitoring page in the browser; at the same time, using the Nginx monitoring API, we can also programmatically obtain the Nginx status information for subsequent use. processing and analysis. The above is how Nginx monitors real-time status configuration. I hope it will be helpful to everyone.

The above is the detailed content of Nginx monitors real-time status configuration and keeps abreast of website operation status. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn