nginx 增加 lua模組
Nginx中的stub_status模組主要用於檢視Nginx的一些狀態資訊.
本模組預設是不會編譯進Nginx的,如果你要使用該模組,則要在編譯安裝Nginx時指定:
./configure –with-http_stub_status_module 這個模組如果需要也可以加入
######################### 下面是 lua模組
unknown directive "access_by_lua"
unknown directive "set_unescape_uri"
之所以報錯是缺少nginx的三方外掛,下面介紹安裝nginx的第三方外掛,外掛很多直介紹三個
方式一:
下載 ngx_openresty,該整合包中有:Nginx,Lua或Luajit,ngx_lua,以及一些有用的Nginx第三方模組。
安裝步驟:
- ./configure --with-luajit
- make
- make install
安裝完成,個人建議第一種安裝方便簡單,另外這個版本還提供了很多的元件,安裝不會出現錯誤。
方式二:
Ngx_lua手動編譯進Nginx。
首先,我的 Nginx 安裝路徑為:/usr/local/nginx。
我將嘗試編譯的兩個模組:echo,lua。
所需要的模組如下:
liujit http://luajit.org
lua http://www.lua.org
ngx_devel_kit https://github.com/simpl/ngx_devel_kit
echo-nginx-module https://github.com/agentzh/echo-nginx-module
lua-nginx-module https://github.com/chaoslawful/lua-nginx-module
安裝步驟:
1、Luajit2.0.2(推薦)
Java程式碼
- wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
- tar zxvf LuaJIT-2.0.2.tar.gz
- cd LuaJIT-2.0.2
- make
- sudo make install
安裝lua
tar-zxvf lua-5.2.0.tar.gz cd lua-5.2.0 make linux make install 完成安裝. 如果遇到 lua.c:67:31: fatal error: readline/readline.h: No such file or directory 說明缺少libreadline-dev依賴包 centos: yum install readline-devel debian: apt-get install libreadline-dev.
下面需要配置一下 luajit 或 lua 的環境變數(Nginx編譯時需要):
- -- luajit --
- # tell nginx's build system where to find LuaJIT:
- export LUAJIT_LIB=/path/to/luajit/lib
- export LUAJIT_INC=/path/to/luajit/include/luajit-2.0.2
- -- lua --
- # or tell where to find Lua if using Lua instead:
- export LUA_LIB=/path/to/lua/lib
- export LUA_INC=/path/to/lua/include
我的測試環境裡,配置如下:
- export LUAJIT_LIB=/usr/local/luajit/lib
- export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
2、安裝 ngx_devel_kit (NDK) 模組
- cd /usr/local
- git clone https://github.com/simpl/ngx_devel_kit.git
下載完成後,將在 /usr/local/ 目錄下生成子目錄 ngx_devel_kit。
3、安裝 lua-nginx-module 模組
Java程式碼
- cd /usr/local
- git clone https://github.com/chaoslawful/lua-nginx-module.git
下載完成後,將在 /usr/local/ 目錄下生成子目錄 lua-nginx-module。
4、重新編譯Nginx,需要注意編譯順序!
- ./configure --prefix=/usr/local/nginx \
- --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \
- --add-module=/usr/local/ngx_devel_kit \
- --add-module=/usr/local/echo-nginx-module \
- --add-module=/usr/local/lua-nginx-module
- make -j2
- make install
註釋:重新編譯 Nginx 二進位制,Nginx 需要 quit 再啟動。而普通配置更新則 reload 即可:
kill -HUP `cat /path/nginx/logs/nginx.pid`
/usr/local/nginx/sbin/nginx -s reload
模組編譯成功!
重啟Nginx伺服器!
在編譯安裝 Nginx 的第三方模組時,碰到一個錯誤:
/usr/local/nginx/sbin/ngxin -s reload
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
百事不得其解,後來Google之,發現瞭解決辦法。
在 Nginx 編譯時,需要指定 RPATH,加入下面選項即可:
./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"
或者
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
5、測試Lua
在Nginx.conf 配置檔案中,加入以下程式碼:
location /echo {
default_type 'text/plain';
echo 'hello echo';
}
location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
重啟Nginx伺服器:
/usr/local/nginx/sbin/nginx -s reload
使用curl測試:
[[email protected]] curl http://localhost/echo
hello echo
[[email protected]] curl http://localhost/lua
hello lua
測試結果表明,兩個模組都安裝成功!
簡潔版
Java程式碼
- wget http://nginx.org/download/nginx-1.2.7.tar.gz
- wget http://luajit.org/download/LuaJIT-2.0.1.tar.gz
- wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz
- wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.7.16.tar.gz
- tar xzf LuaJIT-2.0.1.tar.gz
- cd LuaJIT-2.0.1
- make
- make install PREFIX=/usr/local/LuaJIT/
- cd ..
- tar xzf nginx-1.2.7.tar.gz
- tar xzf v0.2.18.tar.gz
- tar xzf v0.7.16.tar.gz
- cd nginx-1.2.7
- export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
- export LUAJIT_LIB=/usr/local/LuaJIT/lib
- export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
- ./configure –user=www –group=www –prefix=/usr/local/webserver/nginx –with-http_stub_status_module –with-http_ssl_module –add-module=/root/software/ngx_devel_kit-0.2.18 –add-module=/root/software/lua-nginx-module-0.7.16/
- make
- make insatll
- ./configure --prefix=/usr/local/nginx/nginx-1.7.3 \
- --sbin-path=/usr/local/nginx/nginx-1.7.3 \
- --conf-path=/usr/local/nginx/nginx-1.7.3/conf/nginx.conf \
- --with-http_ssl_module \
- --with-openssl=/usr/local/nginx/openssl-1.0.1h \
- --with-pcre=/usr/local/nginx/pcre-8.33 \
- --with-zlib=/usr/local/nginx/zlib-1.2.8 \
- --with-http_stub_status_module \
- --add-module=/usr/local/nginx/ngx_devel_kit-0.2.18 \
- --add-module=/usr/local/nginx/lua-nginx-module-0.7.16 \
- --add-module=/usr/local/nginx/set-misc-nginx-module-0.27
另一篇文章:http://sunjun041640.blog.163.com/blog/static/2562683220134931235857/
misc-nginx-module和lua-nginx-module外掛具體文件可參考如下:
https://github.com/openresty/set-misc-nginx-module/tags
https://github.com/openresty/lua-nginx-module#installation
記錄日誌:
Java程式碼- user root;
- worker_processes 1;
- #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 {
- include mime.types;
- default_type application/octet-stream;
- log_format tick "$msec|||$u_t|||$remote_addr|||$u_domain|||$u_url|||$u_title|||$u_referrer|||$u_sh|||$u_sw|||$u_cd|||$u_lang|||$http_user_agent|||$u_utrace|||$u_account|||$u_time";
- #access_log logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #gzip on;
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- location /1.gif {
- #偽裝成gif檔案
- default_type image/gif;
- #本身關閉access_log,通過subrequest記錄log
- access_log off;
- access_by_lua "
- -- 使用者跟蹤cookie名為__utrace
- local uid = ngx.var.cookie___utrace
- if not uid then
- -- 如果沒有則生成一個跟蹤cookie,演算法為md5(時間戳+IP+客戶端資訊)
- uid = ngx.md5(ngx.now() .. ngx.var.remote_addr .. ngx.var.http_user_agent)
- end
- ngx.header['Set-Cookie'] = {'__utrace=' .. uid .. '; path=/'}
- if ngx.var.arg_domain then
- -- 通過subrequest到/i-log記錄日誌,將引數和使用者跟蹤cookie帶過去
- ngx.location.capture('/i-log?' .. ngx.var.args .. '&utrace=' .. uid .. '&time=' .. ngx.localtime())
- end
- ";
- #此請求不快取
- add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT";
- add_header Pragma "no-cache";
- add_header Cache-Control "no-cache, max-age=0, must-revalidate";
- #返回一個1×1的空gif圖片
- empty_gif;
- }
- location /i-log {
- #內部location,不允許外部直接訪問
- internal;
- #設定變數,注意需要unescape
- set_unescape_uri $u_domain $arg_domain;
- set_unescape_uri $u_t $arg_t;
- set_unescape_uri $u_url $arg_url;
- set_unescape_uri $u_title $arg_title;
- set_unescape_uri $u_referrer $arg_referrer;
- set_unescape_uri $u_sh $arg_sh;
- set_unescape_uri $u_sw $arg_sw;
- set_unescape_uri $u_cd $arg_cd;
- set_unescape_uri $u_lang $arg_lang;
- set_unescape_uri $u_utrace $arg_utrace;
- set_unescape_uri $u_account $arg_account;
- set_unescape_uri $u_time $arg_time;
- #開啟日誌
- log_subrequest on;
- #記錄日誌到ma.log,實際應用中最好加buffer,格式為tick
- access_log /usr/local/nginx/nginxlog/access.log tick;
- #輸出空字串
- echo '';
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- # remove the robots line if you want to use wordpress' virtual robots.txt
- location = /robots.txt { access_log off; log_not_found off; }
- location = /favicon.ico { access_log off; log_not_found off; }
- # this prevents hidden files (beginning with a period) from being served
- location ~ /\. { access_log off; log_not_found off; deny all; }
- }
- }