溫馨提示×

centos nginx如何監控系統資源

小樊
57
2025-07-22 19:46:38
欄目: 智能運維

在CentOS系統中,使用Nginx監控系統資源可以通過多種方式實現。以下是一些常用的方法:

1. 使用Nginx自帶的模塊

Nginx有一些內置的模塊可以幫助你監控系統資源,例如ngx_http_stub_status_module。

安裝和配置

  1. 安裝Nginx(如果尚未安裝):

    sudo yum install nginx
    
  2. 啟用stub_status模塊: 編輯Nginx配置文件(通常是/etc/nginx/nginx.conf),添加以下內容:

    server {
        listen 80;
        server_name localhost;
    
        location /nginx_status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;  # 只允許本地訪問
            deny all;         # 拒絕其他所有IP
        }
    }
    
  3. 重啟Nginx

    sudo systemctl restart nginx
    
  4. 訪問監控頁面: 打開瀏覽器,訪問http://your_server_ip/nginx_status,你應該能看到Nginx的實時狀態信息。

2. 使用第三方監控工具

你可以使用一些第三方監控工具來更全面地監控系統資源,例如Prometheus和Grafana。

安裝和配置Prometheus和Grafana

  1. 安裝Prometheus: 下載并解壓Prometheus:

    wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz
    tar xvfz prometheus-2.30.3.linux-amd64.tar.gz
    cd prometheus-2.30.3.linux-amd64
    
  2. 配置Prometheus: 編輯prometheus.yml文件,添加Nginx的監控目標:

    scrape_configs:
      - job_name: 'nginx'
        static_configs:
          - targets: ['localhost:9113']
    
  3. 啟動Prometheus

    ./prometheus --config.file=prometheus.yml
    
  4. 安裝Grafana: 下載并解壓Grafana:

    wget https://dl.grafana.com/oss/release/grafana-8.2.0.linux-amd64.tar.gz
    tar xvfz grafana-8.2.0.linux-amd64.tar.gz
    cd grafana-8.2.0
    
  5. 啟動Grafana

    ./bin/grafana-server
    
  6. 配置Grafana: 打開瀏覽器,訪問http://your_server_ip:3000,使用默認用戶名和密碼(admin/admin)登錄。然后添加Prometheus作為數據源,并創建儀表盤來監控Nginx和其他系統資源。

3. 使用系統自帶的監控工具

CentOS自帶了一些監控工具,例如top、htop、vmstat等。

使用top和htop

  • top

    top
    

    這個命令會實時顯示系統的CPU和內存使用情況。

  • htop

    sudo yum install htop
    htop
    

    htop提供了更友好的界面和更多的功能。

使用vmstat

vmstat 1

這個命令會每秒刷新一次系統的虛擬內存統計信息。

通過這些方法,你可以有效地監控CentOS系統中Nginx的性能和系統資源的使用情況。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女