Nginx 內嵌lua腳本,結合Redis使用

WBOY
發布: 2016-08-08 09:23:16
原創
1174 人瀏覽過

0x00 Nginx 內嵌Lua腳本有以下特點:

  • 20k個並發連接

  • Lua腳本可以在Nignx 11個擴展的不同層次速度,​​層次的不同層次速度暫存器指令)

  • 0x01 應用場景

在web server端做請求過濾處理(如:WAF、Anti CC等)

  • 0x02 簡單配置過程LTS

幾個需要下載的模組(注意安裝順序和export路徑問題)

  1. Nginx 1.7.4
. for Lua)

  • ngx_devel_kit( Nginx Development Kit)

  • echo-nginx-module( more shell-style goodies to Nginx config file)

  • lua-nginx-module(Embed the Power of Lua into Nginx)

  • 0x03 可能存在的問題,找不到lua.h 等,是因為luaJIT的lib和inc沒有配置在環境變量中

  •     需要這樣配置(你實際的本地路徑) :
  •     export LUAJIT_LIB=/usr/lib/lua

  •     export LUAJIT_INC=/usr/local/include/luajit-2.0🜆in/ihao/ihao/ii​​iiiiiii>iiiiiiiiiiiiCr/i> /local/include /

    

    如果有無法啟動的情況,service 可以查看tail /var/log/syslog  查看錯誤

   如果已經產生nginx bin檔案可以用nginx -V 來查看設定檔是否正確

    


    如果缺少模組:

🠟 fat    3-dev

    zlib


    sudo apt-get install zlib1g-dev

    

    openssl


    sudo apt-get install libssl-dev

    
ps:特別說明的是,請注意下Nginx的版本不要下載最新的,可能不支持上面那些模塊接口,我用的是Nginx 1.7.4

    

    其中0x02的安裝步驟都有安裝說明,這裡就不細說了

0x04 安裝完後

    修改nginx.conf文件(默認路徑/etc/nginx/nginx .conf):

新增lua程式碼

    


    

    效果:

    


    2.  添加lua 檔案:
  1.     新增兩個lua_package_path,lua_code_cache(為了不保留lua cache,方便調試,實際專案需要開啟)

        的):

    /etc/nginx/lua/hello.lua

    /etc/nginx/lua/hello_redis.lua
/

    

    nginx .conf = "text/plain";

     ngx.say("say hello from hello.lua");

    所有新增的location代碼:

    

    

    3.使用redis(第三條新加的redis):

    

   get install redis-server

    

    hello_redis.lua 內容物:

    

    本地快取= redis.new()

    


    本地ok, err = cache.connect(快取, '127.0.0.1', '6379')

    


    cache:set_timeout(60000)

🠎          ngx.say("連結失敗:", err)

            return

結束

    


    res, err = cache:set("hello", "redis in nginx_inline_lua")

   ello 失敗:",錯誤)

return

    結束

    


    ngx.say("設定結果:", res)

    

            ngx.say ("未能收到hello: ",錯誤)

            return

    結束

        ngx.say("hello 找不到。")

           返回

    結束


   

    ngx.say("hello: ", res)

     ngx.say("hello: ", res)

    ("關閉失敗: “,錯誤)

        返回

    結束

    


    效果:

    

gin385在過程中放棄,堅持下去,相信你一定會成功。

0xFF 附加資料:

    http://wiki.nginx.org/HttpLuaModule

    http://openresty.org/ (最早完成Nginx內)蠎的中文.org/

休閒請婚紗出處(個人論壇):http://www.byteway.net/thread-index-fid-4-tid-316 .htm

以上就介紹了Nginx內嵌lua腳本,結合Redis的使用,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!