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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

nginx启动不了_nginx 变量与监控

發(fā)布時(shí)間:2025/3/20 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx启动不了_nginx 变量与监控 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Nginx的配置文件使用語法的就是一門微型的編程語言。既然是編程語言,一般也就少不了“變量”這種東西。

1、nginx變量簡介

  • 所有的 Nginx變量在 Nginx 配置文件中引用時(shí)都須帶上 $ 前綴
  • 在 Nginx 配置中,變量只能存放一種類型的值,而且也只存在一種類型,那就是字符串類型
  • 所有的變量值都可以通過這種方式引用:
    $變量名
    2、nginx 變量的定義和使用
    nginx中的變量分為兩種,自定義變量與內(nèi)置預(yù)定義變量1、自定義變量1、聲明變量 可以在sever,http,location等標(biāo)簽中使用set命令聲明變量,語法如下
    set $變量名 變量值注意:
    • nginx 中的變量必須都以$開頭
    • nginx 的配置文件中所有使用的變量都必須是聲明過的,否則 nginx 會無法啟動(dòng)并打印相關(guān)異常日志

nginx安裝echo模塊
查看已經(jīng)安裝的nginx的版本
[root@192 ~]# nginx -V
上傳或者下載一個(gè)相同版本的nginx包
[root@192 ~]# ls
anaconda-ks.cfg nginx-1.16.0.tar.gz
下載echo模塊的安裝包
[root@192 ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
[root@192 ~]# ls
anaconda-ks.cfg nginx-1.16.0.tar.gz v0.61.tar.gz
解壓到相同路徑下:
[root@192 ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/
[root@192 ~]# tar xzf v0.61.tar.gz -C /usr/local/
安裝編譯工具
[root@192 ~]# cd /usr/local/
[root@192 local]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ zlib zlib-devel gd-devel
添加模塊:
[root@192 local]# cd nginx-1.16.0/
添加上原來已經(jīng)有的參數(shù)和新添加的模塊:
[root@192 nginx-1.16.0]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/echo-nginx-module-0.61
[root@192 nginx-1.16.0]# make #編譯,不要make install 否則會覆蓋原來的文件
[root@192 nginx-1.16.0]# mv /usr/sbin/nginx /usr/sbin/nginx_bak #將原來的nignx備份
[root@192 nginx-1.16.0]# cp objs/nginx /usr/sbin/ 拷貝nignx
[root@192 nginx-1.16.0]# systemctl restart nginx #啟動(dòng)
[root@192 nginx-1.16.0]# nginx -V 查看模塊是否添加成功
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/echo-nginx-module-0.612、配置 $foo=hello

[root@192 ~]# cd /etc/nginx/conf.d/ [root@192 conf.d]# vim echo.conf server { listen 80; server_name localhost; location /test { set $foo hello; echo "foo: $foo"; } }

輸出

[root@192 conf.d]# nginx -s reload [root@192 conf.d]# curl localhost/test foo: hello

Nginx 變量的創(chuàng)建只能發(fā)生在 Nginx 配置加載的時(shí)候,或者說 Nginx 啟動(dòng)的時(shí)候。而賦值操作則只會發(fā)生在請求實(shí)際處理的時(shí)候。這意味著不創(chuàng)建而直接使用變量會導(dǎo)致啟動(dòng)失敗。

2、內(nèi)置預(yù)定義變量

內(nèi)置預(yù)定義變量即無需聲明就可以使用的變量,通常包括一個(gè)http請求或響應(yīng)中一部分內(nèi)容的值,以下為一些常用的內(nèi)置預(yù)定義變量

e data-draft-node="block" data-draft-type="table" data-size="normal" data-row-style="normal">

20、nginx 監(jiān)控

1、nginx的基礎(chǔ)監(jiān)控

  • 進(jìn)程監(jiān)控
  • 端口監(jiān)控

注意: 這兩個(gè)是必須要加在zabbix監(jiān)控,加觸發(fā)器有問題及時(shí)告警。

nginx 提供了 ngx_http_stub_status_module.這個(gè)模塊提供了基本的監(jiān)控功能

2、監(jiān)控的指標(biāo)

1、基本活躍指標(biāo)

Accepts(接受)、Handled(已處理)、Requests(請求數(shù))是一直在增加的計(jì)數(shù)器。Active(活躍)、Waiting(等待)、Reading(讀)、Writing(寫)隨著請求量而增減。

2、每秒請求數(shù) -- QPS

通過持續(xù)的 QPS 監(jiān)控,可以立刻發(fā)現(xiàn)是否被惡意攻擊或?qū)Ψ?wù)的可用性進(jìn)行評估。雖然當(dāng)問題發(fā)生時(shí),通過 QPS 不能定位到確切問題的位置,但是他可以在第一時(shí)間提醒你環(huán)境可能出問題了

3、服務(wù)器錯(cuò)誤率

通過監(jiān)控固定時(shí)間間隔內(nèi)的錯(cuò)誤代碼(4XX代碼表示客戶端錯(cuò)誤,5XX代碼表示服務(wù)器端錯(cuò)誤)。

4、請求處理時(shí)間

請求處理時(shí)間也可以被記錄在 access log 中,通過分析 access log,統(tǒng)計(jì)請求的平均響應(yīng)時(shí)間。 ----$request_time 變量

3、指標(biāo)的收集

通過在編譯時(shí)加入 nginx 的 ngx_http_stub_status_module 模塊我們可以實(shí)時(shí)監(jiān)控以下基本的指標(biāo):

1、nginx Stub Status 監(jiān)控模塊安裝

 先使用命令查看是否已經(jīng)安裝這個(gè)模塊:

# -V大寫會顯示版本號和模塊等信息、v小寫僅顯示版本信息 [root@localhost ~]# nginx -V

注意:是如果沒有此模塊,需要重新安裝,編譯命令如下:

./configure –with-http_stub_status_module

具體的使用方法是在執(zhí)行 ./configure 時(shí),指定 --with-http_stub_status_module,然后通過配置:

[root@localhost ~]# vim /etc/nginx/conf.d/status.conf server { listen 80; server_name localhost; location /nginx-status { stub_status on; access_log on; } }

2、nginx 狀態(tài)查看

配置完成后在瀏覽器中輸入http://10.0.105.207/nginx-status 查看顯示信息如下:

Active connections: 2 server accepts handled requests 26 26 48 Reading: 0 Writing: 1 Waiting: 1

3、Stub Status 參數(shù)說明

connection #連接數(shù),tcp連接 request #http請求,GET/POST/DELETE/UPLOAD

nginx總共處理了26個(gè)連接,成功創(chuàng)建26次握手,也就是成功的連接數(shù)connection. 總共處理了48個(gè)請求

失敗連接=(總連接數(shù)(accepts)-成功連接數(shù)(handled))(相等表示中間沒有失敗的), Reading : nginx讀取到客戶端的Header信息數(shù)。請求頭 -----速度快。

Writing :nginx返回給客戶端的Header信息數(shù)。響應(yīng)頭

Waiting :開啟keep-alive的情況下,意思就是Nginx說已經(jīng)處理完正在等候下一次請求指令的駐留連接。

也可以通過ngx_req_status_module能夠統(tǒng)計(jì)Nginx中請求的狀態(tài)信息。需要安裝第三方模塊

安裝模塊:

tengine官方說req-status模塊默認(rèn)安裝。但是并沒有。從github引入第三方模塊解決該問題 yum與編譯安裝的nginx擴(kuò)展模塊安裝: [root@nginx-server ~]# yum install -y unzip 1. 安裝,先查看一下當(dāng)前編譯安裝nginx的版本 [root@localhost nginx-1.16.0]# nginx -V 下載或者上傳一個(gè)和當(dāng)前的nginx版本一樣的nginx的tar包。 [root@nginx-server ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/ 2.下載ngx_req_status_module 模塊, 這是第三方模塊需要添加 [root@nginx-server ~]# wget https://github.com/zls0424/ngx_req_status/archive/master.zip -O ngx_req_status.zip [root@nginx-server ~]# unzip ngx_req_status.zip [root@nginx-server ~]# cp -r ngx_req_status-master/ /usr/local/ #與解壓的nginx在同一級目錄下 [root@nginx-server ~]# cd /usr/local/nginx-1.16.0/ [root@nginx-server nginx-1.16.0]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ zlib zlib-devel [root@nginx-server nginx-1.16.0]# yum -y install patch.x86_64 [root@nginx-server nginx-1.16.0]# patch -p1 < ../ngx_req_status-master/write_filter-1.7.11.patch [root@localhost nginx-1.16.0]# ./configure 添加上原來的參數(shù) --add-module=/usr/local/ngx_req_status-master [root@localhost nginx-1.16.0]# make -j2 由于原先已有nginx,所以不能執(zhí)行make install,否則會覆蓋掉以前的配置文件及內(nèi)容 [root@localhost nginx-1.16.0]# mv /usr/sbin/nginx /usr/sbin/nginx_bak [root@localhost nginx-1.16.0]# cp objs/nginx /usr/sbin/ [root@localhost nginx-1.16.0]# systemctl restart nginx [root@localhost nginx-1.16.0]# nginx -V 如果發(fā)現(xiàn)編譯的配置文件有變化就成功了! ========================= 指令介紹 req_status_zone 語法: req_status_zone name string size 默認(rèn)值: None 配置塊: http # 定義請求狀態(tài)ZONE,請求按照string分組來排列,例如: req_status_zone server_url $server_name$uri 256k; #域名+uri將會形成一條數(shù)據(jù),可以看到所有url的帶寬,流量,訪問數(shù) #在location中啟用請求狀態(tài),你可以指定更多zones。 req_status 語法: req_status zone1[ zone2] 默認(rèn)值: None 配置塊: http, server, location ? #在當(dāng)前位置啟用請求狀態(tài)處理程序 req_status_show 語法: req_status_show on 默認(rèn)值: None 配置塊: location ? ============================================= 配置如下: 需要在http里面配置。 [root@localhost ~]# vim /etc/nginx/nginx.conf req_status_zone server_name $server_name 256k; req_status_zone server_addr $server_addr 256k; req_status_zone server_url $server_name$uri 256k; ? 創(chuàng)建配置文件: [root@localhost ~]# vim /etc/nginx/conf.d/rep_stat.conf server { listen 80; server_name localhost; req_status server_name server_addr server_url; ? location /req-status { req_status_show on; } } ? [root@localhost ~]# nginx -t [root@localhost ~]# nginx -s reload

請求狀態(tài)信息包括以下字段:

  • zone_name - 利用req_status_zone定義的分組標(biāo)準(zhǔn)。例如,按照服務(wù)器名稱對請求進(jìn)行分組后;
  • key - 請求按分組標(biāo)準(zhǔn)分組后的分組標(biāo)識(即組名)。例如按服務(wù)器名稱分組時(shí),組名可能是localhost;
  • max_active - 該組的最大并發(fā)連接數(shù);
  • max_bw - 該組的最大帶寬;
  • traffic - 該組的總流量;
  • requests - 該組的總請求數(shù);
  • active - 該組當(dāng)前的并發(fā)連接數(shù);
  • bandwidth - 該組當(dāng)前帶寬。

轉(zhuǎn)自:知乎千鋒云計(jì)算學(xué)院

總結(jié)

以上是生活随笔為你收集整理的nginx启动不了_nginx 变量与监控的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。