nginx 模块

nginx的模块有官方模块和第三方模块之分

通过 rpm 包安装nginx加载的模块有,具体可以通过 nginx -V 进行查看

  • --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

http_stub_status_module 模块:

该ngx_http_stub_status_module模块提供对基本状态信息的访问。

此模块不是默认生成的,应该使用--with-http_stub_status_module 配置参数启用 。

配置

编译选项:

  • --with-http_stub_status_module

作用:

  • Nginx的客户端状态

主要用于展示当前处理链接的状态,用于监控链接信息

 配置语法:

  • Syntax:stub_status;
  • Default:——
  • Context:server,location

 例子:
我们编辑默认配置文件,添加自己的配置  /etc/nginx/conf.d/default.conf

    location /mystatus {
        stub_status;
    }

验证刚刚配置的语法是否正确

nginx -tc /etc/nginx/nginx.conf

重载 nginx 服务

nginx -s reload -c /etc/nginx/nginx.conf

  

效果查看访问  

第一行展示的是当前活跃连接数
第二行第一个数字表示nginx接受的握手的总次数,第二个表示nginx所处理的连接数,最后一个表示请求数。正常情况连接数和握手数是相等的,表示没有丢失。
最后一行分别表示读写等待的数量,最后一个等待表示开启长连接时,客户端服务端既没有读也没有写仅仅建立连接的数量。

--with-http_random_index_module 模块

该ngx_http_random_index_module模块处理以斜线字符(' /')结尾的请求,并选取一个目录中的随机文件作为索引文件。
该模块在ngx_http_index_module模块之前进行处理 。
 
此模块不是默认生成的,应该使用--with-http_random_index_module 配置参数启用 。

编译选项:

  • --with-http_random_index_module

作用:

  • 主目录中选一个随机主页

 配置语法:

  • Syntax:random_index on | off;
  • Default:random_index off;
  • Context:location

 例子:随机访问页面

编辑默认配置文件 /etc/nginx/nginx.conf

    location / {
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
        root    /opt/app/code;
        random_index on;
    }

 重新设置了主目录文件,并且在主目录下新建了html文件  

 

检查重启:

nginx -tc /etc/nginx/nginx.conf
systemctl reload nginx

 

连续刷新会看到不同的页面

注意:虽然nginx会将主目录下的文件作为随机主页,但是不会将隐藏文件包括在内,Linux的隐藏文件是指以点 . 开始的文件

--with-http_sub_module 模块

编译选项:

  • --with-http_sub_module

作用:

  • HTTP内容替换

该模块是用于Nginx服务端在给客户端response内容的时候,进行HTTP内容更换。

 语法:

  • Syntax: sub_filter string replacement; (string表示要替换的内容,replacement表示替换后的对象)
  • Default: — Context:
  • http, server, location

语法:

  • sub_filter_last_modified on | off;
  • 默认: sub_filter_last_modified off;
  • 语境: http,server,location```

该模块用于判断每次请求的服务端内容是否发生变化,当发生变化的时候返回给客户端,当没有发生变化的时候,不再返回内容。重要用于缓存。 

语法: sub_filter_once on | off;

默认值: sub_filter_once on;

配置段: http, server, location

字符串替换一次还是多次替换,默认替换一次,例如你要替换响应内容中的ttlsa为运维生存时间,如果有多个ttlsa出现,那么只会替换第一个,如果off,那么所有的ttlsa都会被替换

 首先我们编写了一个html文件

/opt/app/code/submodule.html

<head>
    <meta charset="utf-8">
    <title>submodules</title>
</head>
<body>
    <a>jeson</a>
    <a>at</a>
    <a>imooc</a>
    <a>jeson</a>
    <a>imooc</a>
</body>
</html>

  

default.conf

    location / {
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
        root   /opt/app/code;
        index submodule.html;
        sub_filter '<a>imooc' '<a>IMOOC';

    }

  

 重启 nginx

systemctl reload nginx

  

访问主页

可以发现默认替换的是第一次匹配的

这是因为sub_filter_once默认开启,因此只替换了一个。

    location / {
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
        root   /opt/app/code;
        index submodule.html;
        sub_filter '<a>imooc' '<a>IMOOC';
     sub_filter_once off;
    
    }

  

重启 nginx 再次属性页面  

 

猜你喜欢

转载自www.cnblogs.com/crazymagic/p/11012978.html