开发者学堂课程【Nginx 企业级 Web 服务实战:AIO、第三方模块及自定义变量】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/649/detail/10759
AIO、第三方模块及自定义变量(二)
三、Nginx 高级配置
Nginx 状态页
状态页是我们当前了解 Nginx 运行状态的方法之一,例如现在想要查看当前有多少人正在访问我的博客,在网站上输入
blogs.studylinux.net/nginx_status
可以看到如图访问量为6个
在编译安装 nginx 的时候需要添加编译参数
--with-http_stub_status_module
,否则配置完成之后监测会是提示语法错误。
配置示例:
location /nginx_status {
stub_status;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
}
状态页用于输出 nginx 的基本状态信息:
输出信息示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应 accepts, handled , requests 三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections:当前处于活动状态的客户端连接数,包括连接等待空闲连接数。
accepts:统计总值,Nginx 自启动后已经接受的客户端请求的总数。
handled:统计总值,Nginx 自启动后已经处理完成的客户端请求的总数,通常等于accepts,除非有因
worker_connections 限制等被拒绝的连接。
requests:统计总值,Nginx 自启动后客户端发来的总的请求数。
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数。
Writing:当前状态,正在向客户端发送响应报文过程中的连接数。
waiting:当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive 的情况下,这个值等于 active -( reading+writing)
Nginx 第三方模块
第三模块是对 nginx 的功能扩展,第三方模块需要在编译安装 Nginx 的时候使用参数 --add-module=PATH 指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到 github 进行开源的模块,nginx 支持第三方模块需要从源码重新编译支持,比如开源的 echo 模块
https://github.com/openresty/echo-nginx-module:
演示,输入
./configure --help | grep add
结果显示有 --add-module=PATH 功能,可以开启一些扩展模块 enable external module
nginx 官网上有 Alphabetical index of variables 可以查看所有变量
要想使用 echo 模块,要先在服务器上将该地址 Clone 下来,进入网址github.com/openresty/echo-nginx-module,之后复制下来
也可以在 nginx 服务器上直接 Clone,输入
cd..
pwd
Gitclone
https://github.com/openresty/echo-nginx-module.git
Clone 完后需要对 Nginx 进行重新编译,先将 Nginx 停止,输入
/apps/nginx/sbin/nginx -s stop
在进入到 nginx 的源码目录,输入
cd nginx-1.16.1/
/apps/nginx/sbin/nginx -s reload^C
然后找到编译的源码命令,输入
./configure --help | grep add
/apps/nginx/sbin/nginx -V
.configure --prefix=/apps/nginx --with-http_ssl_module --wth-http_v2_module --with-http_realip_ module --with-http_addition_module --with-http_image_filter_module --with-http_geoip_module --with-http_gunzip_module --with-http_stub_status_ module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --with-select_module --with-file-aio --add-module
=/usr/local/src/echo-nginx-module
再来输入 make
再输入 make install
安装完后就可以配置 Nginx 装 echo 模块
输入vim /apps/nginx/conf/conf.d/linux39-pc.conf
进入后先注释掉
index index.html index.htm;
try_files $uri $uri/index.html %uri.html /about/index.html;
try_files $uri/index.html $uri.html =666;
然后添加 default_type text/html;和 echo hello world;使用 default_type text/html进行文本处理下载
代码即
#index index.html index.htm;
default_type text/html;
#try_files $uri $uri/index.html %uri.html /about/index.html;
#try_files $uri/index.html $uri.html =666;
echo hello world;
再来输入/apps/nginx/sbin/nginx -s reload
之后在网址 magedu.net 上刷新,页面就改变为 hello world
再来在配置中增加一个文件,
输入vim /apps/nginx/conf/conf.d/linux39-pc.conf
进入后在末尾增加
location /main {
index index.html;
default_type text/html;
echo "hello world,main-->";
echo_reset_timer;
echo_ location /sub1;
echo location / sub2;
echo "took $echo_timer_elapsed sec for total. " ;
}
loction /sub1 {
echo_sleep 1;
echo sub1;
}
location /sub2{
echo_sleep 1;
echo sub2;
}
保存后然后 reload,输入
/apps/nginx/sbin/nginx -s reload
vim /apps/nginx/conf/conf.d/linux39-pc.conf
此时再访问时访问 main 就可以
输入网址 magedu.net/main
页面就显示 hello world,main-->sub1 sub2 took 2.003 sec for total.
如果将 echo sleep 注释掉,#echo_sleep 1;
则会瞬间完成,hello world,main-->sub1 sub2 took 0.000 sec for total.