https://nginx.org/en/docs/http/ngx_http_stub_status_module.html
server {    listen 80;    server_name www.m99-josedu.com;    root /var/www/html/www.m99-josedu.com;    location /status {        stub_status;    }} 
Active connections: 2server accepts handled requests8 8 55Reading: 0 Writing: 1 Waiting: 1
3.11 Nginx 第三方模块的使用
第三方模块是对 Nginx 的功能扩展,需要在编译的时候用 --add-module=PATH 指定路径,所以在使用前要先获得第三方模块的源码,当然,我们也可以自行编写第三方模块
相同的功能,如果 Nginx 有官方模块实现,则尽量使用官方模块实现,如果官方没有该功能,我们才考虑使用第三方模块,一般可以去 github 上搜索我们需要扩展模块
https://github.com/vozlt/nginx-module-vts https://github.com/openresty/echo-nginx-module 
#安装编译工具链[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 $proxy_add_x_forwarded_for $args $is_args $document_root $document_uri $host $remote_port $remote_user $request_body_file $request_method $request_filename $request_uri $scheme $server_protocol $server_addr $server_name $server_port $http_user_agent $http_cookie $cookie_<name> $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 ~]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 指令来自定义变量
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 ~]1234hello 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;log_format name [escape=default|json|none] string ...;
#二进制包安装的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:index[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 $remote_user $time_local $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for $host $server_name $request_time $upstream_response_time $upstream_status $time_iso8601 $request_id $ssl_protocol $ssl_cipher 
#自定义访问日志格式 - 字符串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:index[root@ubuntu ~]# curl http:index[root@ubuntu ~]# curl "http://www.m99-josedu.com/index.html?age=123&name=tom"index[root@ubuntu ~]# curl http:json[root@ubuntu ~]# curl http:test
[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资源
gzip on|off; gzip_buffers number size;                           gzip_comp_level level; gzip_disable regex ...;                                                 gzip_http_version 1.0|1.1;                           gzip_min_length length; gzip_proxied off|expired|no-cache|nostore|private|no_last_modified|no_etag|auth|any ...;                                                                                                                                                                                gzip_types mime-type ...;                           gzip_vary on|off;                   
[root@ubuntu ~]HTTP/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
[root@ubuntu ~]HTTP/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 ~]HTTP/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 编辑过