Nginx动态增加Echo模块
查看Nginx编译模块:
/usr/local/nginx/sbin/nginx -V
--prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_random_index_module --with-http_sub_module
安装Echo模块:
#下载地址
https://github.com/openresty/echo-nginx-module
unzip echo-nginx-module-master.zip
给Nginx动态增加Echo模块:(add 增加echo模块)
cd /mnt/nginx-1.17.5
# 执行以下命令在原有模块下动态增加echo模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_random_index_module --with-http_sub_module --add-module=/mnt/echo-nginx-module-master
把Nginx重新编译:(make生成主程序,不需要make install)
cd /mnt/nginx-1.17.5
make
查看编译模块是否添加成功
/mnt/nginx-1.17.5/objs/nginx -V
让新Nginx程序生效:
pkill nginx
cp /mnt/nginx-1.17.5/objs/nginx /usr/local/nginx/sbin/
重新启动Nginx:
/usr/local/nginx/sbin/nginx
Echo模块使用
1、Echo模块测试:
location /test {
echo "user123";
}
#验证
curl 192.168.56.10/test
2、Echo输出内置变量:
location /test {
echo $remote_addr;
}
#验证
curl 192.168.56.10/test
3、变量定义与输出:
location /test {
set $name user123;
echo $name;
}
#验证
curl 192.168.56.10/test
4、异步执行多location:
location /echo1 {
echo_sleep 1;
echo '111';
}
location /echo2 {
echo_sleep 1;
echo '222';
}
#异步执行echo1、echo2
location /main {
echo_reset_timer;
echo_location_async /echo1;
echo_location_async /echo2;
echo "took $echo_timer_elapsed sec for total.";
}
#验证
curl 192.168.56.10/main
5、同步执行多location:(当echo1未执行完成,echo2只能等待)
location /echo1 {
echo_sleep 1;
echo '111';
}
location /echo2 {
echo_sleep 2;
echo '222';
}
location /main {
echo_reset_timer;
echo_location /echo1;
echo_location /echo2;
echo "took $echo_timer_elapsed sec for total.";
}
#验证
curl 192.168.56.10/main
6、echo_duplicate重复多次输出:
location /echo3 {
echo_duplicate 10 "hello ";
}
#验证
curl 192.168.56.10/echo3