深入解析Nginx常见模块2
在Web服务器和反向代理服务器领域,Nginx凭借其高性能、稳定性和丰富的功能获得了广泛的应用。本文将介绍一些Nginx中常见的模块,帮助你更好地理解和使用它们。
点击这里跳转到上一篇文章https://blog.csdn.net/weixin_43663196/article/details/151015697?spm=1001.2014.3001.5501
Nginx常见模块
Nginx 状态页
基于nginx 模块 ngx_http_stub_status_module 实现,在编译安装nginx的时候需要添加编译参数 --with-http_stub_status_module,否则配置完成之后监测会是提示语法错误 注意:状态页显示的是整个服务器的状态,而非虚拟主机的状态
指令
http://nginx.org/en/docs/http/ngx_http_stub_status_module.htmlSyntax: stub_status;
Default: —
Context: server, location
示例
#配置示例:
location /nginx_status {stub_status;auth_basic "auth login";auth_basic_user_file /apps/nginx/conf/.htpasswd;allow 192.168.0.0/16;allow 127.0.0.1;deny all;access_log off;}#状态页用于输出nginx的基本状态信息:
#输出信息示例:
Active connections: 2
server accepts handled requests
5 5 22
#上面三个数字分别对应accepts,handled,requests三个值
Reading: 0 Writing: 1 Waiting: 1Active connections: #当前处于活动状态的客户端连接数,包括连接等待空闲连接数=reading+writing+waiting
accepts:#统计总值,Nginx自启动后已经接受的客户端请求连接的总数。
handled:#统计总值,Nginx自启动后已经处理完成的客户端请求连接总数,通常等于accepts,除非有因worker_connections限制等被拒绝的失败连接,即失败连接数=accepts-handled
requests:#统计总值,Nginx自启动后客户端发来的总的请求数。因为长连接的原因此值大于上面的accept数
Reading:#当前状态,正在读取客户端请求报文首部的连接的连接数,数值越大,说明排队现象严重,性能不足
Writing:#当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大
Waiting:#当前状态,正在等待客户端发出请求的空闲连接数,开启keep-alive时,Waiting+reading+writing=active connections
分析当前网站访问量
[root@ubuntu2404 ~]#curl -s http://www.caoge.com/nginx_status |awk -F: 'NR==1{print $NF}'
1
[root@ubuntu2404 ~]#curl -s http://www.caoge.com/nginx_status 2> /dev/null |awk '/Reading/{print $2,$4,$6}'
0 1 0
Nginx 第三方模块
第三模块是对nginx 的功能扩展,第三方模块需要在编译安装Nginx 的时候使用参数–add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx的第三方模块需要从源码重新编译进行支持。
nginx-module-vts 模块实现流量监控
https://github.com/vozlt/nginx-module-vts
示例
[root@rocky9 app]# cd /usr/local/src/
[root@rocky9 src]# git clone https://github.com/vozlt/nginx-module-vts.git
Cloning into 'nginx-module-vts'...
remote: Enumerating objects: 1058, done.
remote: Counting objects: 100% (258/258), done.
remote: Compressing objects: 100% (88/88), done.
remote: Total 1058 (delta 179), reused 196 (delta 162), pack-reused 800 (from 1)
Receiving objects: 100% (1058/1058), 1.16 MiB | 1.18 MiB/s, done.
Resolving deltas: 100% (702/702), done.
[root@rocky9 src]# ls
nginx-1.26.2 nginx-1.26.2.tar.gz nginx-module-vts
[root@rocky9 src]# cd nginx-1.26.2
[root@rocky9 nginx-1.26.2]# ./configure --prefix=/app/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/usr/local/src/nginx-module-vts
[root@rocky9 nginx-1.26.2]# make && make install
[root@rocky9 app]# vim nginx/conf/nginx.conf
http {vhost_traffic_status_zone;server {listen 80;server_name localhost;charset utf-8;location / {root /app/nginx/html;index index.html index.htm;}location /status {vhost_traffic_status_display;vhost_traffic_status_display_format html; }
[root@rocky9 app]# nginx
echo 模块实现信息显示
开源的echo模块可以用来打印信息,变量等
https://github.com/openresty/echo-nginx-module
示例
[root@rocky9 app]# systemctl stop nginx
[root@rocky9 app]# cd /usr/local/src/[root@rocky9 nginx-1.26.2]# git clone https://github.com/openresty/echo-nginx-module.git[root@rocky9 nginx-1.26.2]# cd nginx-1.26.2/[root@rocky9 nginx-1.26.2]# ./configure --prefix=/app/nginx --user=nginx --group=nginx \
--with-http_ssl_module --with-http_v2_module --with-http_realip_module
--with-http_stub_status_module --with-http_gzip_static_module --with-pcre
--with-stream --with-stream_ssl_module --with-stream_realip_module
--add-module=/usr/local/src/echo-nginx-module
--add-module=/usr/local/src/nginx-module-vts[root@rocky9 nginx-1.26.2]# make && make install [root@rocky9 nginx-1.26.2]# /app/nginx/sbin/nginx -t
nginx: the configuration file /app/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx/conf/nginx.conf test is successfull#编译新模块需要重启nginx才能访问测试,不支持reload
[root@rocky9 nginx-1.26.2]# systemctl restart nginx
[root@rocky9 app]# vim m.caoge.com.conf
server {server_name m.caoge.com;location / {root /data/nginx;index index.html;echo "hello word,main";echo $remote_addr;echo_reset_timer;echo "took $echo_timer_elapsed sec for total";}}
[root@rocky9 app]# nginx -t
nginx: the configuration file /app/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx/conf/nginx.conf test is successful
[root@rocky9 app]# nginx -s reload[root@centos7 ~]# curl m.caoge.com
hello word,main
192.168.101.11
took 0.000 sec for total
Nginx 变量使用
nginx的变量可以在配置文件中引用,作为功能判断或者日志等场景使用变量可以分为内置变量和自定义变量。内置变量是由nginx模块自带,通过变量可以获取到众多的与客户端访问相关的值
内置变量
官方文档
http://nginx.org/en/docs/varindex.html
常用内置变量
$remote_addr;
#存放了客户端的地址,注意是客户端的公网IP$proxy_add_x_forwarded_for
#此变量表示将客户端IP追加请求报文中X-Forwarded-For首部字段,多个IP之间用逗号分隔,如果请求中没有X-Forwarded-For,就使用$remote_addr
the “X-Forwarded-For” client request header field with the $remote_addr variable
appended to it, separated by a comma. If the “X-Forwarded-For” field is not
present in the client request header, the $proxy_add_x_forwarded_for variable is
equal to the $remote_addr variable.$args;
#变量中存放了URL中的所有参数,例如:http://www.wang.org/main/index.do?id=20190221&partner=search
#返回结果为: id=20190221&partner=search$is_args
#如果有参数为? 否则为空
“?” if a request line has arguments, or an empty string otherwise$document_root;
#保存了针对当前资源的请求的系统根目录,例如:/apps/nginx/html。$document_uri;
#保存了当前请求中不包含参数的URI,注意是不包含请求的指令,比如:http://www.caoge.org/main/index.do?id=20190221&partner=search会被定义为/main/index.do
#返回结果为:/main/index.do$host;
#存放了请求的host名称limit_rate 10240;
echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0$remote_port;
#客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口$remote_user;
#已经经过Auth Basic Module验证的用户名$request_body_file;
#做反向代理时发给后端服务器的本地资源的名称$request_method;
#请求资源的方式,GET/PUT/DELETE等$request_filename;
#当前请求的资源文件的磁盘路径,由root或alias指令与URI请求生成的文件绝对路径,如:/apps/nginx/html/main/index.html$request_uri;
#包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,例如:/main/index.do?id=20190221&partner=search $scheme;
#请求的协议,例如:http,https,ftp等$server_protocol;
#保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等$server_addr;
#保存了服务器的IP地址$server_name;
#请求的服务器的主机名$server_port;
#请求的服务器的端口号$http_user_agent;
#客户端浏览器的详细信息$http_cookie;
#客户端的所有cookie信息$cookie_<name>
#name为任意请求报文首部字部cookie的key名$http_<name>
#name为任意请求报文首部字段,表示记录请求报文的首部字段,ame的对应的首部字段名需要为小写,如果有横线需要替换为下划线
arbitrary request header field; the last part of a variable name is the field
name converted to lower case with dashes replaced by underscores #用下划线代替横线#示例:
echo $http_user_agent;
echo $http_host;$sent_http_<name>
#name为响应报文的首部字段,name的对应的首部字段名需要为小写,如果有横线需要替换为下划线,此变量有问题
echo $sent_http_server;$arg_<name>
#此变量存放了URL中的指定参数,name为请求url中指定的参数
echo $arg_id;$uri;
#和$document_uri相同
示例
[root@rocky9 app]# vim m.caoge.com.conf
server {server_name m.caoge.com;location / {root /data/nginx;index index.html;echo "hello word,main";echo $remote_addr;echo_reset_timer;echo $host;echo $remote_user;echo $remote_port;echo $request_uri;echo $scheme;echo $server_addr;echo $server_port;echo $http_cookie;echo $http_user_agent;echo $document_root;echo $server_name;echo "took $echo_timer_elapsed sec for total";}}
[root@centos7 ~]# curl m.caoge.com/dnaoewnoefnowenoangoangname=wang
hello word,main
192.168.101.11
m.caoge.com38414
/dnaoewnoefnowenoangoangname=wang
http
192.168.101.5
80curl/7.29.0
/data/nginx
m.caoge.com
took 0.000 sec for total[root@rocky9 app]# vim m.caoge.com.conf
server {server_name m.caoge.com;location / {root /data/nginx;index index.html;echo "hello word,main";echo $remote_addr;echo $proxy_add_x_forwarded_for;echo $args;echo $cookie_key1;echo $host;echo $remote_user;echo $remote_port;echo $request_uri;echo $scheme;echo $server_addr;echo $server_port;echo $http_cookie;echo $http_user_agent;echo $document_root;echo $server_name;echo "took $echo_timer_elapsed sec for total";}}[root@centos7 ~]# curl m.caoge.com/dnaoewnoefnowenoangoangname=wang
hello word,main
192.168.101.11
192.168.101.11m.caoge.com38420
/dnaoewnoefnowenoangoangname=wang
http
192.168.101.5
80curl/7.29.0
/data/nginx
m.caoge.com
took 0.000 sec for total
自定义变量
假如需要自定义变量名称和值,使用指令 set $variable value
语法格式
Syntax: set $variable value;
Default: —
Context: server, location, if
示例
[root@rocky9 app]# vim m.caoge.com.conf
server {server_name m.caoge.com;set $name caoge;set $my_port $server_port;location / {root /data/nginx;index index.html;echo "hello word,main";echo $remote_addr;echo $name;echo "$my_port:$server_port";echo $proxy_add_x_forwarded_for;echo $args;echo $cookie_key1;echo $host;echo "took $echo_timer_elapsed sec for total";}}
[root@centos7 ~]# curl m.caoge.com/
hello word,main
192.168.101.11
caoge
80:80
192.168.101.11m.caoge.com
took 0.000 sec for total
Nginx 自定义访问日志
访问日志是记录客户端即用户的具体请求内容信息,而在全局配置模块中的error_log是记录nginx服务器运行时的日志保存路径和记录日志的level,因此两者是不同的,而且Nginx的错误日志一般只有一个,但是访问日志可以在不同server中定义多个,定义一个日志需要使用access_log指定日志的保存路径,使用log_format指定日志的格式,格式中定义要保存的具体日志内容。
访问日志由 ngx_http_log_module模块实现。
官方帮助文档
http://nginx.org/en/docs/http/ngx_http_log_module.html
语法格式
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];access_log off; #关闭访问日志,比如在反向代理上可以关闭日志功能
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
日志格式的常见变量
$remote_addr # 记录客户端IP地址
$remote_user # 记录客户端用户名
$time_local # 记录通用的本地时间
$time_iso8601 # 记录ISO8601标准格式下的本地时间
$request # 记录请求的方法以及请求的http协议
$status # 记录请求状态码(用于定位错误信息)
$body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent # 发送给客户端的总字节数$msec # 日志写入时间。单位为秒,精度是毫秒。
$http_referer # 记录从哪个页面链接访问过来的
$http_user_agent # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端IP地址
$request_length # 请求的长度(包括请求行,请求头和请求正文)。
$request_time # 请求花费的时间,单位为秒,精度毫秒
# 注:如果Nginx位于负载均衡器,nginx反向代理之后,web服务器无法直接获取到客户端真实的IP地址。
# $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中,
# 增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址
默认日志格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;
自定义默认格式日志
如果是要保留日志的源格式,只是添加相应的日志内容,则配置如下
#注意:此指令只支持http块,不支持server块
log_format access_log_format '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"''$server_name:$server_port';#注意:此指令一定要在放在log_format命令后
access_log logs/access.log access_log_format;#重启nginx并访问测试日志格式[root@rocky9 app]# vim m.caoge.com.conf
log_format test_log '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_refere