日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

nginx upstream配置_Prometheus快速监控Nginx

發布時間:2024/9/19 Nginx 74 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx upstream配置_Prometheus快速监控Nginx 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求:Prometheus 監控Nginx主要用到以下三個模塊:

  • nginx-module-vts:Nginx virtual host traffic status module,Nginx的監控模塊,能夠提供JSON格式的數據產出。
  • nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的監控數據,并給Prometheus提供監控接口,默認端口號9913。
  • Prometheus:監控Nginx-vts-exporter提供的Nginx數據,并存儲在時序數據庫中,可以使用PromQL對時序數據進行查詢和聚合。

一、初始化

yum install -y gcc gcc-c++ curl wget bzip2yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-develcd /optwget https://github.com/jemalloc/jemalloc/releases/download/5.1.0/jemalloc-5.1.0.tar.bz2tar -jxvf jemalloc-5.1.0.tar.bz2cd jemalloc-5.1.0./configure --prefix=/usr/local/jemallocmake -j 2 &>/dev/null && make install &>/dev/nullecho "/usr/local/jemalloc/lib" >/etc/ld.so.conf.d/jemalloc.confldconfigln -s /usr/local/jemalloc/lib/libjemalloc.so /usr/lib64/libjemalloc.soln -s /usr/local/jemalloc/lib/libjemalloc.so.2 /usr/lib64/libjemalloc.so.2

二、安裝nginx-module-vts

  • 1、安裝腳本
cd /opt/git clone https://github.com/vozlt/nginx-module-vtswget https://openresty.org/download/openresty-1.15.8.2.tar.gztar -zxvf openresty-1.15.8.2.tar.gzcd openresty-1.15.8.2./configure --prefix=/usr/local/openresty --with-stream --with-threads --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --user=www --group=www --build="LiveOps build at `date +%Y-%m-%d`" --with-ld-opt="-Ijemalloc" --add-module=/opt/nginx-module-vts/gmakegmake install
  • 2、配置nginx
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; }}
  • 3、打開vhost過濾:
vhost_traffic_status_filter_by_host on;開啟此功能,在Nginx配置有多個server_name的情況下,會根據不同的server_name進行流量的統計,否則默認會把流量全部計算到第一個server_name上。
  • 4、在不想統計流量的server區域禁用vhosttrafficstatus,配置示例:
server {...vhost_traffic_status off;...}假如nginx沒有規范配置server_name或者無需進行監控的server上,那么建議在此vhost上禁用統計監控功能。否則會出現“127.0.0.1”,hostname等的域名監控信息。
  • 5、安裝完vts模塊后,可以通過nginx status接口進行監控數據的查看

三、安裝nginx-vts-exporter

wget https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.10.3/nginx-vts-exporter-0.10.3.linux-amd64.tar.gztar zxvf nginx-vts-exporter-0.10.3.linux-amd64.tar.gzmv nginx-vts-exporter-0.10.3.linux-amd64 /usr/local/exporter/nginx-vts-exportercd /usr/local/exporter/nohup ./nginx-vts-exporter -nginx.scrape_timeout 10 -nginx.scrape_uri http://114.67.116.119/status/format/json
  • 啟動日志
[root@Prometheus nginx-vts-exporter]# tail -f nohup.out 2020/02/21 12:16:50 Starting nginx_vts_exporter (version=0.10.3, branch=HEAD, revision=8aa2881c7050d9b28f2312d7ce99d93458611d04)2020/02/21 12:16:50 Build context (go=go1.10, user=root@56ca8763ee48, date=20180328-05:47:47)2020/02/21 12:16:50 Starting Server at : :99132020/02/21 12:16:50 Metrics endpoint: /metrics2020/02/21 12:16:50 Metrics namespace: nginx2020/02/21 12:16:50 Scraping information from : http://114.67.116.119/status/format/json

備注:推薦exporter和nginx安裝在同一臺機器上,如果不在同一臺主機,把scrapeuri改為nginx主機的地址。 nginxvts_exporter的默認端口號:9913,對外暴露監控接口http://xxx:9913/metrics.

  • 展示:http://xxx:9913/metrics

四、nginx-vts-exporte 接入 promethueus.yml

修改prometheus.yml配置文件 vim /usr/local/prometheus/promethueus.yml

- job_name: nginx static_configs: - targets: ['114.67.116.119:9913'] labels: instance: nginx-test

nohup /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml &

圖形展示

五、granfana展示

在grafana數據源導入2949的模板 (https://grafana.com/dashboards/2949)

常用監控匯總表達式

DomainName對應nginx conf里的server_name,這里可以根據不同的server_name和upstream分別進行qps、2xx/3xx/4xx/5xx的狀態碼監控,另外也可以監控nginx每臺后端server的qps和后端接口響應時間。如果不需要區分server_name,可以把表達式里的$DomainName改為星號,“*****”代表所有;# 1. 求Nginx的QPS:sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))sum(irate(nginx_server_requests{instance=~"$Instance", code!="total"}[5m])) by (code)# 2. 求4xx萬分率(5xx類似,code=“5xx”):(sum(irate(nginx_server_requests{code="4xx",host=~"$DomainName"}[5m])) / sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))) * 10000# 3. 求upstream的QPS(示例求group1的qps):sum(irate(nginx_upstream_requests{code="total",upstream="group1"}[5m]))# 4. 求upstream后端server的響應時間(示例求group1的后端響應時間):nginx_upstream_responseMsec{upstream=“group1”}nginx_upstream_responseMsec{backend="192.168.x.xxx:8803",instance="nginx-web-1",job="nginx",upstream="UPSTREAM_NAME"}

總結

以上是生活随笔為你收集整理的nginx upstream配置_Prometheus快速监控Nginx的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。