安裝nginx_lua_module以及echo-nginx-module收集日誌

unknown directive "access_by_lua" 

unknown directive "set_unescape_uri"javascript

 

之因此報錯是缺乏nginx的三方插件,下面介紹安裝nginx的第三方插件,插件不少直介紹三個html

 

方式一:java

 

下載 ngx_openresty,該集成包中有:NginxLuaLuajitngx_lua,以及一些有用的Nginx第三方模塊。linux

安裝步驟:nginx

 

  1. ./configure --with-luajit   
  2. make   
  3. make install  

 

安裝完成,我的建議第一種安裝方便簡單,另外這個版本還提供了不少的組件,安裝不會出現錯誤。git

 

方式二:github

 

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  

安裝步驟:

一、Luajit2.0.2(推薦)web

 

Java代碼 算法

 收藏代碼

  1. wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz  
  2. tar zxvf LuaJIT-2.0.2.tar.gz  
  3. cd LuaJIT-2.0.2   
  4. make    
  5. sudo make install  

 

安裝luacentos

 

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編譯時須要):
 

Java代碼 

 收藏代碼

  1. -- luajit --   
  2. # tell nginx's build system where to find LuaJIT:   
  3. export LUAJIT_LIB=/path/to/luajit/lib   
  4. export LUAJIT_INC=/path/to/luajit/include/luajit-2.0.2  
  5. -- lua --   
  6. # or tell where to find Lua if using Lua instead:   
  7. export LUA_LIB=/path/to/lua/lib   
  8. export LUA_INC=/path/to/lua/include   

 

個人測試環境裏,配置以下:
 

Java代碼 

 收藏代碼

  1. export LUAJIT_LIB=/usr/local/luajit/lib   
  2. export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0  

   


二、安裝 ngx_devel_kit (NDK) 模塊

Java代碼 

 收藏代碼

  1. cd /usr/local   
  2. git clone https://github.com/simpl/ngx_devel_kit.git    

 

下載完成後,將在 /usr/local/ 目錄下生成子目錄 ngx_devel_kit。


三、安裝 lua-nginx-module 模塊

 

Java代碼 

 收藏代碼

  1. cd /usr/local   
  2. git clone https://github.com/chaoslawful/lua-nginx-module.git  

 

下載完成後,將在 /usr/local/ 目錄下生成子目錄 lua-nginx-module。


四、從新編譯Nginx,須要注意編譯順序!

Java代碼 

 收藏代碼

  1. ./configure --prefix=/usr/local/nginx \   
  2.                 --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \   
  3.                 --add-module=/usr/local/ngx_devel_kit \   
  4.                 --add-module=/usr/local/echo-nginx-module \   
  5.                 --add-module=/usr/local/lua-nginx-module   
  6. make -j2   
  7. 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  
五、測試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測試:
    [root@localhost] curl http://localhost/echo  
    hello echo  
    [root@localhost] curl http://localhost/lua  
    hello lua  

測試結果代表,兩個模塊都安裝成功!

 

簡潔版

 

Java代碼 

 收藏代碼

  1. wget http://nginx.org/download/nginx-1.2.7.tar.gz  
  2. wget http://luajit.org/download/LuaJIT-2.0.1.tar.gz  
  3. wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz  
  4. wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.7.16.tar.gz  
  5.   
  6. tar xzf LuaJIT-2.0.1.tar.gz  
  7. cd LuaJIT-2.0.1  
  8. make  
  9. make install PREFIX=/usr/local/LuaJIT/  
  10.   
  11. cd ..  
  12. tar xzf nginx-1.2.7.tar.gz  
  13. tar xzf v0.2.18.tar.gz  
  14. tar xzf v0.7.16.tar.gz  
  15. cd nginx-1.2.7  
  16.   
  17. export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0  
  18. export LUAJIT_LIB=/usr/local/LuaJIT/lib  
  19. export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH  
  20.   
  21. ./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/  
  22. make  
  23. make insatll  
  24.   
  25.   
  26.   
  27.   
  28. ./configure --prefix=/usr/local/nginx/nginx-1.7.3 \  
  29. --sbin-path=/usr/local/nginx/nginx-1.7.3 \  
  30. --conf-path=/usr/local/nginx/nginx-1.7.3/conf/nginx.conf \  
  31. --with-http_ssl_module \  
  32. --with-openssl=/usr/local/nginx/openssl-1.0.1h \  
  33. --with-pcre=/usr/local/nginx/pcre-8.33 \  
  34. --with-zlib=/usr/local/nginx/zlib-1.2.8 \  
  35. --with-http_stub_status_module \  
  36. --add-module=/usr/local/nginx/ngx_devel_kit-0.2.18 \  
  37. --add-module=/usr/local/nginx/lua-nginx-module-0.7.16 \  
  38. --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代碼 

 收藏代碼

  1. user  root;    
  2. worker_processes  1;    
  3.     
  4. #error_log  logs/error.log;    
  5. #error_log  logs/error.log  notice;    
  6. #error_log  logs/error.log  info;    
  7.     
  8. pid        logs/nginx.pid;    
  9.     
  10.     
  11. events {    
  12.     worker_connections  1024;    
  13. }    
  14.     
  15.     
  16. http {    
  17.     include       mime.types;    
  18.     default_type  application/octet-stream;    
  19.     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";    
  20.         
  21.     
  22.     #access_log  logs/access.log  main;    
  23.     sendfile        on;    
  24.     #tcp_nopush     on;    
  25.     
  26.     #keepalive_timeout  0;    
  27.     keepalive_timeout  65;    
  28.     
  29.     #gzip  on;    
  30.     
  31.     server {    
  32.         listen       80;    
  33.         server_name  localhost;    
  34.     
  35.         #charset koi8-r;    
  36.     
  37.     location /1.gif {    
  38.         #假裝成gif文件    
  39.         default_type image/gif;    
  40.         #自己關閉access_log,經過subrequest記錄log    
  41.         access_log off;    
  42.         access_by_lua "    
  43.         -- 用戶跟蹤cookie名爲__utrace    
  44.         local uid = ngx.var.cookie___utrace    
  45.         if not uid then    
  46.         -- 若是沒有則生成一個跟蹤cookie,算法爲md5(時間戳+IP+客戶端信息)    
  47.         uid = ngx.md5(ngx.now() .. ngx.var.remote_addr .. ngx.var.http_user_agent)    
  48.         end    
  49.         ngx.header['Set-Cookie'] = {'__utrace=' .. uid .. '; path=/'}    
  50.         if ngx.var.arg_domain then    
  51.         -- 經過subrequest到/i-log記錄日誌,將參數和用戶跟蹤cookie帶過去    
  52.         ngx.location.capture('/i-log?' .. ngx.var.args .. '&utrace=' .. uid .. '&time=' .. ngx.localtime())    
  53.         end    
  54.         ";    
  55.         #此請求不緩存    
  56.         add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT";    
  57.         add_header Pragma "no-cache";    
  58.         add_header Cache-Control "no-cache, max-age=0, must-revalidate";    
  59.         #返回一個1×1的空gif圖片    
  60.         empty_gif;    
  61.         }    
  62.         location /i-log {    
  63.         #內部location,不容許外部直接訪問    
  64.         internal;    
  65.      #設置變量,注意須要unescape    
  66.         set_unescape_uri $u_domain $arg_domain;    
  67.     set_unescape_uri $u_t $arg_t;    
  68.         set_unescape_uri $u_url $arg_url;    
  69.         set_unescape_uri $u_title $arg_title;    
  70.         set_unescape_uri $u_referrer $arg_referrer;    
  71.         set_unescape_uri $u_sh $arg_sh;    
  72.         set_unescape_uri $u_sw $arg_sw;    
  73.         set_unescape_uri $u_cd $arg_cd;    
  74.         set_unescape_uri $u_lang $arg_lang;    
  75.         set_unescape_uri $u_utrace $arg_utrace;    
  76.         set_unescape_uri $u_account $arg_account;    
  77.     set_unescape_uri $u_time $arg_time;    
  78.         #打開日誌    
  79.         log_subrequest on;    
  80.         #記錄日誌到ma.log,實際應用中最好加buffer,格式爲tick    
  81.         access_log /usr/local/nginx/nginxlog/access.log tick;    
  82.         #輸出空字符串    
  83.         echo '';    
  84.         }    
  85.     
  86.         #error_page  404              /404.html;    
  87.     
  88.         # redirect server error pages to the static page /50x.html    
  89.         #    
  90.         error_page   500 502 503 504  /50x.html;    
  91.         location = /50x.html {    
  92.             root   html;    
  93.         }    
  94.         
  95.     # remove the robots line if you want to use wordpress' virtual robots.txt    
  96.         location = /robots.txt  { access_log off; log_not_found off; }    
  97.         location = /favicon.ico { access_log off; log_not_found off; }    
  98.         # this prevents hidden files (beginning with a period) from being served    
  99.         location ~ /\.          { access_log off; log_not_found off; deny all; }    
  100.     
  101.     }    
  102.     
  103.     
  104.     
  105. }   
相關文章
相關標籤/搜索