LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

高性能WEB服务器Nginx常用功能(三):状态页、第三方模块、变量、自定义访问日志、压缩功能

admin
2025年6月29日 18:38 本文热度 59

3 Nginx 常用功能

3.10 Nginx 状态页
https://nginx.org/en/docs/http/ngx_http_stub_status_module.html
stub_status; #添加此指令后可开启 Nginx 状态页,作用域 server, location
server {    listen 80;    server_name www.m99-josedu.com;    root /var/www/html/www.m99-josedu.com;    location /status {        stub_status;    }
#在浏览器中访问 http://www.m99-josedu.com
Active connections: 2server accepts handled requests8 8 55Reading: 0 Writing: 1 Waiting: 1
# Active connections 当前处于活动状态的客户端连接数,=reading+writing+waiting# accepts 总数,自 Nginx 启动到当前己接收的客户端请求总数# handled 总数,自 Nginx 启动到当前己处理完成的连接数,通常等于 accepts,如果有失败连接,则要去掉失败连接# requests 总数,自 Nginx 启动到当前客户端己发送的请求总数# Reading 状态,当前正在读取请求报文的连接数,值越大,说明排队现象严重,性能不足# Writing 状态,正在向客户端发送响应报文过程中的连接数,值越大,说明访问量越大# Waiting 状态,己建立连接,但没有数据传输的空闲连接数,开启 keep-alive,Reading+Writing+Waiting=Active connections

3.11 Nginx 第三方模块的使用

第三方模块是对 Nginx 的功能扩展,需要在编译的时候用 --add-module=PATH 指定路径,所以在使用前要先获得第三方模块的源码,当然,我们也可以自行编写第三方模块

相同的功能,如果 Nginx 有官方模块实现,则尽量使用官方模块实现,如果官方没有该功能,我们才考虑使用第三方模块,一般可以去 github 上搜索我们需要扩展模块

https://github.com/vozlt/nginx-module-vts # 第三方流量监控模块https://github.com/openresty/echo-nginx-module # echo 模块,可以直接输出
#安装编译工具链[root@ubuntu ~]# apt update[root@ubuntu ~]# apt install -y make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
#创建运行用户[root@ubuntu ~]# useradd -r -s /usr/sbin/nologin nginx
#下载最新版源码并解压,在物理机中下载第三方模块源码,上传到Linux[root@ubuntu ~]# wget https://nginx.org/download/nginx-1.22.1.tar.gz
#查看[root@ubuntu ~]# ls -lhtotal 1.4M-rw-r--r-- 1 root root 76K Jan 30 15:35 echo-nginx-module-master.zip-rw-r--r-- 1 root root 1.1M Oct 19 2024 nginx-1.22.1.tar.gz-rw-r--r-- 1 root root 206K Jan 30 15:40 nginx-module-vts-master.zip
#分别解压[root@ubuntu ~]# tar xf nginx-1.22.1.tar.gz[root@ubuntu ~]# unzip echo-nginx-module-master.zip[root@ubuntu ~]# unzip nginx-module-vts-master.zip
#编译安装[root@ubuntu ~]# cd nginx-1.22.1/[root@ubuntu nginx-1.22.1]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --withhttp_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=/root/echo-nginx-module-master/ --add-module=/root/nginx-module-vtsmaster/[root@ubuntu nginx-1.22.1]# make && make install
#修改目录属主属组并查看[root@ubuntu nginx-1.22.1]# chown -R nginx.nginx /apps/nginx/[root@ubuntu nginx-1.22.1]# ls -l /apps/nginx/total 16drwxr-xr-x 2 nginx nginx 4096 Jan 20 15:35 conf #配置文件目录drwxr-xr-x 2 nginx nginx 4096 Jan 20 15:35 html #网站文件根目录drwxr-xr-x 2 nginx nginx 4096 Jan 20 15:35 logs #日志文件目录drwxr-xr-x 2 nginx nginx 4096 Jan 20 15:35 sbin #二进制程序目录
#创建软链接[root@ubuntu nginx-1.22.1]# ln -sv /apps/nginx/sbin/nginx /usr/sbin/nginx'/usr/sbin/nginx' -> '/apps/nginx/sbin/nginx'
#查看版本[root@ubuntu nginx-1.22.1]# nginx -vnginx version: nginx/1.22.1[root@ubuntu nginx-1.22.1]# nginx -Vnginx version: nginx/1.22.1built by gcc 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)built with OpenSSL 3.0.2 15 Mar 2022TLS SNI support enabledconfigure arguments: --prefix=/apps/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=/root/echo-nginx-module-master/ --add-module=/root/nginx-module-vts-master/
#测试[root@ubuntu nginx-1.22.1]# cd /apps/nginx/conf/[root@ubuntu conf]# cat nginx.conf......  vhost_traffic_status_zone; #写在 http 中  server {    listen 80;    server_name www.m99-josedu.net;    root /apps/nginx/html/www.m99-josedu.net;    location /status { #自带 status        stub_status;    }
    location /vts { #第三方status        vhost_traffic_status_display;        vhost_traffic_status_display_format html; #可以写成 josn ,方便数据采集    }
    location /echo { #第三方 echo 模块        default_type text/html;        echo "hello world<br />";        echo "$request_uri<br />";        echo "$uri<br />";        echo "$remote_addr<br />";    }}
[root@ubuntu ~]# mkdir /apps/nginx/html/www.m99-josedu.net/[root@ubuntu ~]# echo "index" > /apps/nginx/html/www.m99-josedu.net/index.html
#启动nginx[root@ubuntu ~]# nginx
#在物理机中加 hosts 域名解析,然后在浏览器中查看效果,对比第三方status 和官方status 区别

3.12 Nginx 中的变量

Nginx 变量可以在配置文件中使用,用作判断或定义日志格式等场景,Nginx 变量可以分为内置变量和自定义变量两种

Nginx 内置变量

Nginx 内置变量是 Nginx 自行定义的,可以直接调用

https://nginx.org/en/docs/varindex.html
常用内置变量
$remote_addr # 客户端公网IP,如果经多层Nginx代理,那最后的Nginx 通过此变量无法获取客户端IP$proxy_add_x_forwarded_for # 代理IP和真实客户端IP$args # URL中的所有参数$is_args # 是否有参数,有参数该变量值为 ?,没有参数值为空$document_root # 当前资源的文件系统路径$document_uri # 当前资源不包含参数的URI$host # 请求头中的host值$remote_port # 客户端随机问口$remote_user # 经过 auth basic 验证过的用户名$request_body_file # 作为反向代理发给后端服务器的本地资源名称$request_method # 当前资源的请求方式 GET/PUT/DELETE 等$request_filename # 当前资源在文件系统上的绝对路径$request_uri # 不包含的主机名的URI,包含请求的资源和参数$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 # 客户端UA$http_cookie # 客户端所有COOKIE$cookie_<name> # 获取指定COOKIE$http_<name> # 获取指定的请求头中的字段,如果字段有中划线要替换成下划线,大写转成小写$sent_http_<name> # 获取指定的响应头中的字段,如果字段有中划线要替换成下划线,大写转成小写$arg_<name> # 获取指定参数
server {    listen 80;    server_name www.m99-josedu.net;    root /apps/nginx/html/www.m99-josedu.net;    location /vars {        default_type text/html;        echo "request: " $request;        echo "proxy_add_x_forwarded_for: " $proxy_add_x_forwarded_for;        echo "args: " $args;        echo "document_uri: " $document_uri;        echo "request_uri: " $request_uri;        echo "document_root: " $document_root;        echo "host: " $host;        echo "request_method: " $request_method;        echo "request_filename: " $request_filename;        echo "scheme: " $scheme;        echo "UA: " $http_User_Agent;        echo "all_cookies: " $http_cookie;        echo "cookie_uname: " $cookie_uname;    }
#测试[root@ubuntu ~]# curl --cookie "age=10,uname=jerry" "http://www.m99-josedu.net/vars?id=123&name=tom"request: GET /vars?id=123&name=tom HTTP/1.1proxy_add_x_forwarded_for: 10.0.0.206args: id=123&name=tomdocument_uri: /varsrequest_uri: /vars?id=123&name=tomdocument_root: /apps/nginx/html/www.m99-josedu.nethost: www.m99-josedu.netrequest_method: GETrequest_filename: /apps/nginx/html/www.m99-josedu.net/varsscheme: httpUA: curl/7.81.0all_cookies: age=10,uname=jerrycookie_uname: jerry

用户自定义变量

在 Nginx 中,除了内置变量外,我们还可以使用 set 指令来自定义变量

set $variable value; # 自定义变量,变量名以$开头,变量值可以从其它变量中获取,也可以直接指定                     # 作用域 server, location, if
server {    listen 80;    server_name www.m99-josedu.net;    root /apps/nginx/html/www.m99-josedu.net;    set $var1 1234; #直接赋值,数字    set $var2 "hello world"#直接赋值,字符串,中间有空格    set $var3 $host#间接赋值,从内置变量中获得值    location /set {        set $var4 $var1#间接赋值,从内置变量中获得值        echo $var1;        echo $var2;        echo $var3;        echo $var4;    }
#测试[root@ubuntu ~]# curl www.m99-josedu.net/set1234hello worldwww.m99-josedu.net1234

3.13 自定义访问日志

访问日志是记录客户端访问服务器资源的记录,我们可以通过分析访问日志来统计当前网站的日请求量,热点资源,热点时间段,平时响应时长,错误状态码占比,客户端IP分布,客户端所使用的浏览器等信息,用好访问日志可以帮助网站所有者了解网站的具体运营情况,此日志作用重大

https://nginx.org/en/docs/http/ngx_http_log_module.html
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];access_log off;# path 日志路径# format 日志格式# buffer=size 定义 buffer 表示启用缓冲区,会异步落盘# gzip[=level] 启用gzip 压缩,此参数会自动启用buffer,默认缓冲区大小为64k,默认压缩级别为1# flush=time 强制落盘时间频率,在缓冲区满了,或者此参数规定的时间到了都会定磁盘# [if=condition] 条件判断,返回true 才会记日志# off 表示不启用日志# 默认值 access_log logs/access.log combined;# 作用域 http, server, location, if in location, limit_exceptlog_format name [escape=default|json|none] string ...;# name 日志格式名称,供 access_log 指令调用# [escape=default|json|none] 设置变量的字符转义,默认default# string ... 当前定义的日志格式要记录的具体内容# 默认值 log_format combined "...";# 作用域 http
#二进制包安装的nginx 默认 access_log 配置[root@ubuntu ~]# cat /etc/nginx/nginx.conf | grep access_logaccess_log /var/log/nginx/access.log;
#默认 log_format 内容log_format combined '$remote_addr - $remote_user [$time_local] '                    '"$request" $status $body_bytes_sent '                    '"$http_referer" "$http_user_agent"';
#测试,并查看日志内容[root@ubuntu ~]# curl http://www.m99-josedu.comindex[root@ubuntu ~]# cat /var/log/nginx/access.log127.0.0.1 - - [31/Jan/2025:23:20:01 +0800"GET / HTTP/1.1" 200 6 "-" "curl/7.81.0"
#定义日志格式常用的变量$remote_addr #客户端的IP地址$remote_user #使用 HTTP 基本身份验证时的远程用户$time_local #服务器本地时间$request #客户端请求的 HTTP 方法、URI 和协议$status #服务器响应的状态码$body_bytes_sent #发送给客户端的字节数,不包括响应头的大小$http_referer #客户端跳转前的来源页面$http_user_agent #客户端的用户代理字符串$http_x_forwarded_for #通过代理服务器传递的客户端真实 IP 地址$host #请求的主机头字段$server_name #服务器名称$request_time #请求处理时间$upstream_response_time #从上游服务器接收响应的时间$upstream_status #上游服务器的响应状态码$time_iso8601 #ISO 8601 格式的本地时间$request_id #用于唯一标识请求的 ID$ssl_protocol #使用的 SSL 协议$ssl_cipher #使用的 SSL 加密算法
#自定义访问日志格式 - 字符串log_format basic '$remote_addr - $remote_user [$time_local"$request" '                 '$status $body_bytes_sent "$http_referer" '                 '"$http_user_agent" "$http_x_forwarded_for"';
#自定义访问日志格式 - json 字符串log_format json_basic '{"remote_addr""$remote_addr", '                      '"remote_user""$remote_user", '                      '"time_local""$time_local", '                      '"request""$request", '                      '"status""$status", '                      '"body_bytes_sent""$body_bytes_sent", '                      '"http_referer""$http_referer", '                      '"http_user_agent""$http_user_agent", '                      '"http_x_forwarded_for""$http_x_forwarded_for"}';
server {    listen 80;    server_name www.m99-josedu.com;    root /var/www/html/www.m99-josedu.com;    access_log /var/log/nginx/${host}_access.log basic;    location /json {        access_log /var/log/nginx/${host}_json_access.log json_basic; #记录 json 格式        return 200 "json";    }    location /test {        access_log off; #不记录access log        return 200 "test";    }}
#测试[root@ubuntu ~]# curl http://www.m99-josedu.comindex[root@ubuntu ~]# curl http://www.m99-josedu.com/index.htmlindex[root@ubuntu ~]# curl "http://www.m99-josedu.com/index.html?age=123&name=tom"index[root@ubuntu ~]# curl http://www.m99-josedu.com/jsonjson[root@ubuntu ~]# curl http://www.m99-josedu.com/test.htmltest
[root@ubuntu ~]# cat /var/log/nginx/www.m99-josedu.com_access.log127.0.0.1 - - [01/Feb/2025:00:24:52 +0800"GET / HTTP/1.1" 200 6 "-" "curl/7.81.0" "-"127.0.0.1 - - [01/Feb/2025:00:24:57 +0800"GET /index.html HTTP/1.1" 200 6 "-" "curl/7.81.0" "-"127.0.0.1 - - [01/Feb/2025:00:25:06 +0800"GET /index.html?age=123&name=tom HTTP/1.1" 200 6 "-" "curl/7.81.0" "-"[root@ubuntu ~]# cat /var/log/nginx/www.m99-josedu.com_json_access.log{"remote_addr""127.0.0.1""remote_user""-""time_local":"01/Feb/2025:00:25:13 +0800""request""GET /json HTTP/1.1""status""200","body_bytes_sent""4""http_referer""-""http_user_agent""curl/7.81.0","http_x_forwarded_for""-"}

3.14 Nginx 压缩功能

Nginx 中可以启用压缩功能,在服务端将要传输的的资源进行压缩后再发送给客户端,此设置可以有效减小传送资源的大小,从而节约网络资源,但压缩会占用服务端的CPU资源

https://nginx.org/en/docs/http/ngx_http_gzip_module.htmlhttps://nginx.org/en/docs/http/ngx_http_gzip_static_module.html
gzip on|off#启用或禁用压缩功能,默认off,作用域 http, server, location, if in locationgzip_buffers number size; #Nginx在压缩时要向服务器申请的缓存空间个数和每个缓存大小,默认 32 4k 或 16 8k                          #作用域 http, server, locationgzip_comp_level level; #压缩比,默认 1,作用域 http, server, locationgzip_disable regex ...; #根据客户端请求头中的UA字段内容来确定在某些情况下禁用压缩,可以支持正则表达式                        #gzip_disable "MSIE [1-6]\."; 这种写法就表示 IE6 浏览器禁用压缩,默认为空                        #作用域 http, server, locationgzip_http_version 1.0|1.1#启用gzip 压缩的最小版本,默认 1.1,即http/1.1 及更高版本才使用压缩                          #作用域 http, server, locationgzip_min_length length; #资源体积多大才启用压缩,默认20,作用域 http, server, locationgzip_proxied off|expired|no-cache|nostore|private|no_last_modified|no_etag|auth|any ...;                #nginx作为反向代理时是否压缩后端返回数据,根据请求头中的 Via 字段来判断                #默认值 off,不压缩                #expired 如果请求头中包含 Expires,则压缩                #no-cache 如果请求头中包含 Cache-Control:no-cache 则压缩                #no-store 如果请求头中包含 Cache-Control:no-store 则压缩                #private 如果请求头中包含 Cache-Control:private 则压缩                #no_last_modified 如果请求头中不包含 Last-Modified 则压缩                #no_etag 如果请求头中不包含 ETag 则压缩                #auth 如果请求头中包含 Authorization 则压缩                #any 任何情况都压缩                #作用域 http, server, locationgzip_types mime-type ...; #指定要压缩的资源类型,默认 text/html,text/html 类型,只要开启gzip 都会被压缩                          # * 表示所有类型,作用域 http, server, locationgzip_vary on|off#是否在响应头中添加 Vary: Accept-Encoding,默认 off                  #作用域 http, server, location
#命令行工具默认不会发送压缩请求头,响应头中显示 Content-Length: 277[root@ubuntu ~]# curl -I https://www.baidu.comHTTP/1.1 200 OKAccept-Ranges: bytesCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformConnection: keep-aliveContent-Length: 277Content-Type: text/htmlDate: Thu, 01 Feb 2025 02:48:58 GMTEtag: "575e1f60-115"Last-Modified: Mon, 13 Jun 2021 02:50:08 GMTPragma: no-cacheServer: bfe/1.0.8.18
#显式指定压缩,响应头中能看到 Content-Encoding: gzip 内容,但看不到传输内容长度#这是因为启用压缩后,传输的内容要进行压缩,大小会改变,但 header 又先于 body 传输#所以 header 在传输前并不知道 body 会被压成多大,所以无法给出body 大小#如果又要压缩,又要返因 content-length,则需要 ngx_http_gzip_static_module 模块支持#但可以在客户端统计,在浏览器中打开页面,通过开发者工具可以查看大小
[root@ubuntu ~]# curl -H "Accept-Encoding: gzip" -I https://www.baidu.comHTTP/1.1 200 OKCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformConnection: keep-aliveContent-Encoding: gzipContent-Type: text/htmlDate: Thu, 01 Feb 2025 02:48:43 GMTLast-Modified: Mon, 13 Jun 2021 02:50:08 GMTPragma: no-cacheServer: bfe/1.0.8.18
[root@ubuntu ~]# curl --compressed -I https://www.baidu.comHTTP/1.1 200 OKCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformConnection: keep-aliveContent-Encoding: gzipContent-Type: text/htmlDate: Thu, 01 Feb 2025 02:48:38 GMTLast-Modified: Mon, 13 Jun 2021 02:50:08 GMTPragma: no-cacheServer: bfe/1.0.8.18
server {    listen 80;    server_name www.m99-josedu.com;    root /var/www/html/www.m99-josedu.com;    gzip on;    gzip_types text/html text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php ijose/gif ijose/png;    gzip_vary on;    location =/b.html {        gzip_comp_level 5;    }    location =/c.html {        gzip off;    }}
[root@ubuntu ~]# cd /var/www/html/www.m99-josedu.com/[root@ubuntu www.m99-josedu.com]# cp /var/log/syslog ./a.html[root@ubuntu www.m99-josedu.com]# cp /var/log/syslog ./b.html[root@ubuntu www.m99-josedu.com]# cp /var/log/syslog ./c.html[root@ubuntu www.m99-josedu.com]# chmod +r {a..c}.html[root@ubuntu www.m99-josedu.com]# ls -lh {a..c}.html-rw-r--r-- 1 root root 681K Feb 1 11:06 a.html-rw-r--r-- 1 root root 681K Feb 1 11:06 b.html-rw-r--r-- 1 root root 681K Feb 1 11:06 c.html
#在浏览器中分别访问 a.html,b.html,c.html,打开开发者工具查看并对比

— END —


阅读原文:原文链接


该文章在 2025/7/1 23:01:49 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved