Nginx模块举例说明

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模块讲解:

编译选项:--with-http_stub_status_module 
作用:Nginx的客户端状态

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

配置语法:

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

例子:
我们编辑默认配置文件,添加配置

编辑完成检查配置是否正确

[root]# nginx -tc /etc/nginx/nginx.conf 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

然后重载服务

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

浏览器输入地址http://localhost/mystatus,以下为浏览器相应图片

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

--with-http_random_index_module模块讲解

编译选项:--with-http_random_index_module
作用:主目录中选一个随机主页

配置语法:

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

例子:生成随机页面

编辑默认配置文件:

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

[root]# pwd
/opt/app/code
[root]# ls
1.html  2.html 3.html

配置完毕重载,请求主页,发现几个html页面1、2、3随机切换

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

--with-http_sub_module模块讲解

编译选项:--with-http_sub_module
作用:HTTP内容替换
该模块是用于Nginx服务端在给客户端response内容的时候,进行HTTP内容更换。

语法:

语法:     sub_filter string replacement  (string表示要替换的内容,replacement表示替换后的对象)
默认值: sub_filter_last_modified off;
配置段:     http, server, location

这个指令在nginx 1.5.1中添加,我这个版本没有,可以忽略掉.
语法: sub_filter_once on | off;
默认值: sub_filter_once on;
配置段: http, server, location

字符串替换一次还是多次替换,默认替换一次,例如你要替换响应内容中的ttlsa为运维生存时间,如果有多个ttlsa出现,那么只会替换第一个,如果off,那么所有的ttlsa都会 被替换
语法: sub_filter_types mime-type ...;
默认值: sub_filter_types text/html;
配置段: http, server, location
指定需要被替换的MIME类型,默认为“text/html”,如果制定为*,那么所有的

测试HTML

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

配置编辑(一)

location / {
   root   /opt/app/code;
   index  index.html index.htm;
   sub_filter '<a>imooc' '<a>IMOOC_JOY';  #sub_filter 后面添加需要替换内容,已经替换后的内容
  ########## sub_filter_once off;        #在原有基础加上此模块,off 关闭
}

通过配置语法替换"imooc"的内容为“IMOOC_JOY”

 

编辑配置(二)

location / {
   root   /opt/app/code;
   index  index.html index.htm;
   sub_filter '<a>imooc' '<a>IMOOC_JOY';  #sub_filter 后面添加需要替换内容,已经替换后的内容
    }

 

posted @ 2018-03-04 00:55  温柔的风  阅读(160)  评论(0编辑  收藏  举报