nginx启动不了_nginx 变量与监控
Nginx的配置文件使用語法的就是一門微型的編程語言。既然是編程語言,一般也就少不了“變量”這種東西。
1、nginx變量簡介
- 所有的 Nginx變量在 Nginx 配置文件中引用時都須帶上 $ 前綴
- 在 Nginx 配置中,變量只能存放一種類型的值,而且也只存在一種類型,那就是字符串類型
- 所有的變量值都可以通過這種方式引用:
$變量名
2、nginx 變量的定義和使用
nginx中的變量分為兩種,自定義變量與內置預定義變量1、自定義變量1、聲明變量 可以在sever,http,location等標簽中使用set命令聲明變量,語法如下
set $變量名 變量值注意:- nginx 中的變量必須都以$開頭
- nginx 的配置文件中所有使用的變量都必須是聲明過的,否則 nginx 會無法啟動并打印相關異常日志
nginx安裝echo模塊
查看已經安裝的nginx的版本
[root@192 ~]# nginx -V
上傳或者下載一個相同版本的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/
添加上原來已經有的參數和新添加的模塊:
[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 #啟動
[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 conf.d]# nginx -s reload [root@192 conf.d]# curl localhost/test foo: helloNginx 變量的創建只能發生在 Nginx 配置加載的時候,或者說 Nginx 啟動的時候。而賦值操作則只會發生在請求實際處理的時候。這意味著不創建而直接使用變量會導致啟動失敗。
2、內置預定義變量
內置預定義變量即無需聲明就可以使用的變量,通常包括一個http請求或響應中一部分內容的值,以下為一些常用的內置預定義變量
e data-draft-node="block" data-draft-type="table" data-size="normal" data-row-style="normal">
20、nginx 監控
1、nginx的基礎監控
- 進程監控
- 端口監控
注意: 這兩個是必須要加在zabbix監控,加觸發器有問題及時告警。
nginx 提供了 ngx_http_stub_status_module.這個模塊提供了基本的監控功能
2、監控的指標
1、基本活躍指標
Accepts(接受)、Handled(已處理)、Requests(請求數)是一直在增加的計數器。Active(活躍)、Waiting(等待)、Reading(讀)、Writing(寫)隨著請求量而增減。
2、每秒請求數 -- QPS
通過持續的 QPS 監控,可以立刻發現是否被惡意攻擊或對服務的可用性進行評估。雖然當問題發生時,通過 QPS 不能定位到確切問題的位置,但是他可以在第一時間提醒你環境可能出問題了
3、服務器錯誤率
通過監控固定時間間隔內的錯誤代碼(4XX代碼表示客戶端錯誤,5XX代碼表示服務器端錯誤)。
4、請求處理時間
請求處理時間也可以被記錄在 access log 中,通過分析 access log,統計請求的平均響應時間。 ----$request_time 變量
3、指標的收集
通過在編譯時加入 nginx 的 ngx_http_stub_status_module 模塊我們可以實時監控以下基本的指標:
1、nginx Stub Status 監控模塊安裝
先使用命令查看是否已經安裝這個模塊:
# -V大寫會顯示版本號和模塊等信息、v小寫僅顯示版本信息 [root@localhost ~]# nginx -V注意:是如果沒有此模塊,需要重新安裝,編譯命令如下:
./configure –with-http_stub_status_module具體的使用方法是在執行 ./configure 時,指定 --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 狀態查看
配置完成后在瀏覽器中輸入http://10.0.105.207/nginx-status 查看顯示信息如下:
Active connections: 2 server accepts handled requests 26 26 48 Reading: 0 Writing: 1 Waiting: 13、Stub Status 參數說明
connection #連接數,tcp連接 request #http請求,GET/POST/DELETE/UPLOADnginx總共處理了26個連接,成功創建26次握手,也就是成功的連接數connection. 總共處理了48個請求
失敗連接=(總連接數(accepts)-成功連接數(handled))(相等表示中間沒有失敗的), Reading : nginx讀取到客戶端的Header信息數。請求頭 -----速度快。
Writing :nginx返回給客戶端的Header信息數。響應頭
Waiting :開啟keep-alive的情況下,意思就是Nginx說已經處理完正在等候下一次請求指令的駐留連接。
也可以通過ngx_req_status_module能夠統計Nginx中請求的狀態信息。需要安裝第三方模塊
安裝模塊:
tengine官方說req-status模塊默認安裝。但是并沒有。從github引入第三方模塊解決該問題 yum與編譯安裝的nginx擴展模塊安裝: [root@nginx-server ~]# yum install -y unzip 1. 安裝,先查看一下當前編譯安裝nginx的版本 [root@localhost nginx-1.16.0]# nginx -V 下載或者上傳一個和當前的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 添加上原來的參數 --add-module=/usr/local/ngx_req_status-master [root@localhost nginx-1.16.0]# make -j2 由于原先已有nginx,所以不能執行make install,否則會覆蓋掉以前的配置文件及內容 [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 如果發現編譯的配置文件有變化就成功了! ========================= 指令介紹 req_status_zone 語法: req_status_zone name string size 默認值: None 配置塊: http # 定義請求狀態ZONE,請求按照string分組來排列,例如: req_status_zone server_url $server_name$uri 256k; #域名+uri將會形成一條數據,可以看到所有url的帶寬,流量,訪問數 #在location中啟用請求狀態,你可以指定更多zones。 req_status 語法: req_status zone1[ zone2] 默認值: None 配置塊: http, server, location ? #在當前位置啟用請求狀態處理程序 req_status_show 語法: req_status_show on 默認值: 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; ? 創建配置文件: [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請求狀態信息包括以下字段:
- zone_name - 利用req_status_zone定義的分組標準。例如,按照服務器名稱對請求進行分組后;
- key - 請求按分組標準分組后的分組標識(即組名)。例如按服務器名稱分組時,組名可能是localhost;
- max_active - 該組的最大并發連接數;
- max_bw - 該組的最大帶寬;
- traffic - 該組的總流量;
- requests - 該組的總請求數;
- active - 該組當前的并發連接數;
- bandwidth - 該組當前帶寬。
轉自:知乎千鋒云計算學院
總結
以上是生活随笔為你收集整理的nginx启动不了_nginx 变量与监控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中删除字典中的某个元素_py
- 下一篇: python json有什么用_为什么要