echo-nginx-module 模块可以在 Nginx 中用来输出一些信息, 可以用来实现简单接口或者排错.
项目地址: https://github.com/openresty/echo-nginx-module
获取 Nginx 源码
因为需要编译模块, 需要有 Nginx 源码.
如果已安装 Nginx, 需要查看当前安装版本的编译参数:
- $ /usr/local/nginx/sbin/nginx -V
- nginx version: nginx/1.12.2
- built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
- built with OpenSSL 1.0.1e-fips 11 Feb 2013
- TLS SNI support enabled
- configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module
其中 configure arguments 这个参数是非常重要的, 我们在后面安装 Lua 模块的时候, 需要以这个为基础, 增加新的参数.
如果还没有安装 Nginx, 上面可以忽略.
Nginx 下载页面: http://nginx.org/en/download.html
这里我们以 nginx/1.12.2 为例. 需要获取源码:
- $ cd /opt/
- $ wget http://nginx.org/download/nginx-1.12.2.tar.gz
- $ tar -zxvf nginx-1.12.2.tar.gz
安装取 echo-nginx-module
获取 echo-nginx-module
我们下载最新稳定版 (截止到 2018-12-23), 并解压, 不用安装:
- $ cd /opt
- $ wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
- $ tar zxvf v0.61.tar.gz
编译 Nginx
Nginx 编译参数配置:
- $ cd /opt/nginx-1.12.2/
- $ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --add-module=/opt/echo-nginx-module-0.61
这里因为已经安装了 Nginx, 所以这里的参数是从 Nginx -V 的输出里获取的, 并追加了新的参数:
--add-module=/opt/echo-nginx-module-0.61
运行上面的./configure 后进行编译安装:
- $ make -j2
- $ make install
make install 后, 会覆盖之前安装的 Nginx.
测试 echo 模块
在 / usr/local/nginx/conf/nginx.conf 中 server 代码块里加入如下代码:
- location /hello {
- default_type 'text/plain';
- return 200 'hello!';
- }
- location /hello_echo {
- default_type 'text/plain';
- echo "hello, echo!";
- }
注意: 重新编译 Nginx 二进制, Nginx 需要停止重启. 而普通配置更新则 reload 即可:
$ kill -QUIT `cat /usr/local/nginx/logs/nginx.pid` && /usr/local/nginx/sbin/nginx
如果支持 service nginx restart, 则可以这样重新启动:
$ service nginx restart && /usr/local/nginx/sbin/nginx -s reload
然后 curl 测试:
- $ curl http://127.0.0.1/hello
- hello!
- $ curl http://127.0.0.1/hello_echo
- hello, echo!
当然, echo-nginx-module 模块不仅仅是提供 echo 这么简单的指令, 还有其它的指令, 详见:
编译动态模块
echo-nginx-module 支持以动态模块方式加载, 详见: .Nginx 版本需要 >=1.9.11 .
- $ cd /opt/nginx-1.12.2/
- $ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --add-dynamic-module=/opt/echo-nginx-module-0.61
- $ make -j2
- $ make install
相比静态编译, 参数 --add-module 改成了 --add-dynamic-module.
编译成功后, 会把模块安装在 nginx/modules / 目录. 查看:
- $ ls /usr/local/nginx/modules/
- ngx_http_echo_module.so
接下来我们需要在 nginx.conf 配置中加入以下两行, 实现动态调用模块:
load_module /usr/local/nginx/modules/ngx_http_echo_module.so;
注意: load_module 指令不能放在 http{} 里面:
- worker_processes 1;
- load_module xxx;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- }
接下来可以按上面的 测试 echo 模块 小节测试. 唯一不同的是无需使用 kill -QUIT 退出 Nginx, 直接使用 nginx -s reload 热重启就行了.
来源: http://www.bubuko.com/infodetail-2895470.html