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 ~]# 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 变量的创建只能发生在 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: 1
3、Stub Status 参数说明

connection #连接数,tcp连接 request #http请求,GET/POST/DELETE/UPLOAD

nginx总共处理了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 - 该组当前带宽。
转自:知乎千锋云计算学院