Nginx安装echo模块
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 installmake 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這么簡單的指令,還有其它的指令,詳見:https://github.com/openresty/echo-nginx-module#content-handler-directives
編譯動態模塊
echo-nginx-module 支持以動態模塊方式加載,詳見:https://github.com/openresty/echo-nginx-module#installation 。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熱重啟就行了。
參考
1、Nginx安裝Nginx-echo模塊 - chen2013 - 博客園
http://www.cnblogs.com/chenjianxiang/p/8489055.html
2、openresty/echo-nginx-module
https://github.com/openresty/echo-nginx-module#installation
3、Nginx編譯安裝Lua - 飛鴻影~ - 博客園
https://www.cnblogs.com/52fhy/p/10164553.html
總結
以上是生活随笔為你收集整理的Nginx安装echo模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 序列化和反序列化的对单例破坏的防止及其原
- 下一篇: 模拟赛 yjqb