自学内容网 自学内容网

n7.Nginx 第三方模块

Nginx 第三方模块

第三模块是对nginx 的功能扩展,第三方模块需要在编译安装Nginx 的时候使用参数–add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开 源爱好者开发好之后上传到github进行开源的模块,nginx的第三方模块需要从源码重新编译进行支持

1. nginx-module-vts 模块实现流量监控

[root@Ubuntu2204 ~]#cd /usr/local/src/
[root@Ubuntu2204 ~]#git clone https://github.com/vozlt/nginx-module-vts.git
[root@Ubuntu2204 src]#ls
nginx-1.22.1  nginx-1.22.1.tar.gz  nginx-module-vts
[root@Ubuntu2204 src]#cd nginx-1.22.1/
[root@Ubuntu2204 nginx-1.22.1]#nginx -V
nginx version: nginx/1.22.1
built by gcc 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure 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

[root@Ubuntu2204 nginx-1.22.1]#./configure --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=/usr/local/src/nginx-module-vts

[root@Ubuntu2204 nginx-1.22.1]#make && make install

[root@Ubuntu2204 nginx-1.22.1]#vim /apps/nginx/conf/conf.d/mirrors.conf

vhost_traffic_status_zone;

server {
    listen 80;
    server_name mirrors.anan.org;
    root /data/nginx/html/mirrors;
    charset utf-8;
    server_tokens off;
    access_log /data/nginx/logs/mirrors-access.log; 
    error_log /data/nginx/logs/mirrors-error.log;
    
    location /status {
    vhost_traffic_status_display;
    vhost_traffic_status_display_format html;
    }
} 

[root@Ubuntu2204 nginx-1.22.1]#systemctl restart nginx.service 

#浏览器输入 mirrors.anan.org/status

在这里插入图片描述

2. echo 模块实现信息显示

开源的echo模块可以用来打印信息,变量等

[root@Ubuntu2204 ~]#cd /usr/local/src
[root@Ubuntu2204 src]#git clone https://github.com/openresty/echo-nginx-module.git
[root@Ubuntu2204 src]#ls
echo-nginx-module  nginx-1.22.1  nginx-1.22.1.tar.gz  nginx-module-vts
[root@Ubuntu2204 src]#cd nginx-1.22.1/
[root@Ubuntu2204 nginx-1.22.1]#./configure --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=/usr/local/src/nginx-module-vts --add-module=/usr/local/src/echo-nginx-module
[root@Ubuntu2204 nginx-1.22.1]#make && make install
[root@Ubuntu2204 nginx-1.22.1]#systemctl restart nginx
[root@Ubuntu2204 nginx-1.22.1]#vim /apps/nginx/conf/conf.d/mirrors.conf
vhost_traffic_status_zone;

server {
    listen 80;
    server_name mirrors.anan.org;
    root /data/nginx/html/mirrors;
    charset utf-8;
    server_tokens off;
    access_log /data/nginx/logs/mirrors-access.log; 
    error_log /data/nginx/logs/mirrors-error.log;

    location /main {
     index index.html;
     default_type text/html;
     echo "hello world,main-->";
     echo $remote_addr ;
     echo_reset_timer;
     echo_location /sub1;
     echo_location /sub2;
     echo "took $echo_timer_elapsed sec for total.";
 }
 location /sub1 {
     echo_sleep 1;
     echo sub1;
 }
 location /sub2 {
     echo_sleep 1;
     echo sub2;
 }
    
} 

[root@Ubuntu2204 nginx-1.22.1]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@Ubuntu2204 nginx-1.22.1]#nginx -s reload

# 测试查看结果(另一台客户端rocky查看)
[root@Rocky8 ~]#curl mirrors.anan.org/main
hello world,main-->
10.0.0.8
sub1
sub2
took 2.004 sec for total.

# 测试查看结果(浏览器查看)

在这里插入图片描述


原文地址:https://blog.csdn.net/weixin_56408536/article/details/140582861

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!