nginx内部是由一些核心和非核心的第三方模块构成,模块化可以通过集成第三方模块实现扩展功能。
- 核心模块 ngx_http_core_module
- 标准模块 http模块
- 第三方模块
默认支持的模块
ngx_http_core_module 模块:包括 nginx.conf 中的 http段中的 server 配置 、 location 配置 、 error_page 配置等。
ngx_http_access_module 访问控制模块 location中配置 deny all;屏蔽所有用户的 访问 ,如果 访问会提示 403 ( allow all 就是允许所有人访问) ,deny address 可以实现 对某个客户address 访问的控制。
配置格式:
allow address | CIDR | unix: | all;
deny address | CIDR | unix: | all;
如何添加第三方模块
nginx不支持动态安装加载模块 所以必须重新编译。可能导致原来的配置被覆盖。为了避免原来的配置被覆盖 需要按照如下操作。(如果是第一次安装或者不用考虑第一次安装被覆盖的情况 则略过以下操作 正常make install安装即可)
1、重新安装时 第一次安装时 做的配置(比如编译时的 --prefix 参数等)必须都进行配置,通过nginx -V 可以查看安装时的配置。比如上次安装事 指定了安装目录 这次也必须指定。
2、不能直接 make install
三方模块安装方法
1、configure make 编译 (add-module 和 with两种方式)
./configure --prefix=/安装目录 --add-module = /第三方模块的目录
./configure --prefix=/data/program/nginx --with-http_stub_status_module --with-http_random_index_module
make
2、make 完成后复制并替换 cp objs/nginx $nginx_home/sbin/nginx
我这里重新安装 不考虑原版本 并且安装 两个模块 http_stub_status_module 和 http_random_index_module
1 ./configure --prefix=/usr/apps/nginx --with-http_stub_status_module --with-http_random_index_module
2、make && make install
3、到安装目录 运行 sbin/nginx 即可启动nginx
两个模块的功能介绍
http_stub_status_module 可以监控 nginx状态
http_random_index_module 可以随机返回多个主页中的一个。
1、http_stub_status_module 配置
vi conf/nginx.conf server配置 中 增加 如下location配置
location /status {
stub_status;
}
保存 退出 并重新加载配置
nginx -s reload
浏览器访问 http://192.168.68.137/status 可以看到nginx 状态信息如下:
Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数
2、http_random_index_module 配置
vi nginx.conf 默认location配置中增加 random_index on (默认为off)
location / {
root html;
random_index on;
index index.html index.htm;
}
保存 退出 并重新加载配置 nginx -s reload
同时 保证html目录中有多个html文件。
[root@zk03 html]# ll
总用量 12
-rw-r--r--. 1 root root 537 1月 23 17:42 50x.html
-rw-r--r--. 1 root root 11 1月 23 17:55 haha.html
-rw-r--r--. 1 root root 612 1月 23 17:42 index.html
访问nginx首页 有时候 出现 index.html 有时候出现haha.html 如下: