二进制安装nginx 下载地址:https://nginx.org/download/nginx-1.22.1.tar.gz

# 解压进入nginx目录

# 安装编译环境
yum install gcc make libtool zlib-devel pcre-devel openssl-devel -y

# 检测系统环境和配置选项,以生成适合当前系统的构建配置文件。
./configure
# 编译
make
# 安装
make install

使用nginx-module-vts模块监控nginx

安装nginx-module-vts 版本:nginx-module-vts-0.2.1 下载地址:https://github.com/vozlt/nginx-module-vts/releases安装步骤

# 进入nginx安装目录
cd /usr/local/nginx
# 上传nginx-module-vts安装包
# 解压nginx-module-vts
tar -zxvf nginx-module-vts-0.2.1.tar.gz
# 预编译
./configure --add-module=nginx-module-vts-0.2.1
# 编译(这里只make,不要make install,不然会覆盖。如果是新装nginx,可以继续make install)
make

调整nginx启动脚本

# 进入nginx的sbin目录
cd /usr/local/nginx/sbin/
# 停止nginx
./nginx -s stop
# 备份原启动脚本
mv nginx nginx.old
# 替换新的启动脚本
cp /usr/local/nginx/nginx-1.20.1/objs/nginx /usr/local/nginx/sbin/
# 启动nginx
cd /usr/local/nginx/sbin/
./nginx

# 查看插件是否安装成功
./nginx -V
configure arguments: 最后是否有 --add-module=nginx-module-vts-0.2.1
# 进入nginx的conf目录
cd /usr/local/nginx/conf
# 备份原配置文件
cp nginx.conf nginx.conf.bak

# 修改nginx.conf配置,增加以下内容
http {
    ...
    vhost_traffic_status_zone;  
    vhost_traffic_status_filter_by_host on;
    ...
    server {
        ...   
        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
        }
}

# 重启nginx
/usr/local/nginx/sbin/nginx -s reload

配置解析:

1、打开vhost过滤:
vhost_traffic_status_filter_by_host on;
开启此功能,在Nginx配置有多个server_name的情况下,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个server_name上。

2、在不想统计流量的server区域禁用vhost_traffic_status,配置示例:
server {
        ...
        vhost_traffic_status off;
        ...
}

安装nginx-vts-exporter 版本:nginx-vts-exporter-0.10.3 下载地址:https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gz

nohup /usr/local/prometheus_exporter/nginx_exporter/nginx-vts-exporter-0.10.3.linux-amd64/nginx-vts-exporter -nginx.scrape_uri http://127.0.0.1:9200/status/format/json &> /dev/null &

添加prometheus监控配置并重启

# 进入prometheus目录
cd /usr/local/prometheus
# 修改 prometheus.yml
vim prometheus.yml
 
# 添加如下内容
- job_name: 'nginx'
    scrape_interval: 30s
    static_configs:
      - targets: ['39.101.198.57:9913']
        labels:
          instance: '监控(39.101.198.57:9913)'
          
# 重启prometheus
# systemctl restart prometheus

# 杀掉prometheus进程
kill -SIGTERM `pgrep -f prometheus`
# 启动prometheues
/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus --web.enable-lifecycle --web.listen-address=:39090 &> /dev/null &

prometheus+grafana监控nginx_重启

dashboard选择 2949

生产环境nginx配置文件示例

server {
           listen   9200;
           location /status {
                vhost_traffic_status_display;
                vhost_traffic_status_display_format html;
                }
           }
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: nginx