nginx 添加lua

  • 默認情況下Nginx不支持Lua模塊,需要安裝LuaJIT解釋器,並且重新編譯Nginx,或者可使用國人開發的openrestry

  • 需要的模塊:LuaJIT,Ngx_devellua-nginx-module
  • Luajit官網:https://luajit.org
  1. 環境準備

    yum install -y gcc gcc-c++ make pcre-devel zlib-devel openssl-devel環境準備
  2. 安裝LuaJIT

    wget https://luajit.org/download/LuaJIT-2.0.5.tar.gz
  3. 下載、解壓lua模塊

    wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
    wget https://github.com/openresty/lua-nginx-module/archive/v0.10.21rc2.tar.gz
  4. 保留原始模塊,用大寫V查看已經安裝的模塊

    nginx -V
    nginx version: nginx/1.23.1
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
    built with OpenSSL 1.0.2k-fips  26 Jan 2017
    TLS SNI support enabled
    configure arguments: --with-http_stub_status_module --with-http_ssl_module
  5. 路徑切換到nginx安裝包,這裏是安裝包路徑不是安裝路徑,重新加載配置

    ./configure --prefix=/安裝路徑/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-pcre --add-module=/模塊解壓位置/lua-nginx-module-0.10.21rc2 --add-module=/模塊解壓位置/ngx_devel_kit-0.3.0

     

  6. 執行make,千萬不要  install

  7. 我的錯誤,這個錯誤是配置文件錯誤,手動修改文件

    error: ‘ngx_http_headers_in_t’ has no member named ‘cookies’

    根據路徑指引,找到 cookies修改
    cookies   →   cookie

  8. 重複  步驟 6

  9. 備份

    cp /安裝目錄/sbin/nginx  nginx.xxx.bak
  10. 配置覆蓋

    cp  /安裝包目錄/objs/nginx  /安裝目錄/sbin/nginx
  11. 試試

    nginx -V
  12. 重啓

    nginx -s reload
  13. 可能出現的其他錯誤

    [root@localhost /]# nginx 
    nginx: [alert] detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
    nginx: [alert] failed to load the 'resty.core' module (https://github.com/openresty/lua-resty-core); ensure you are using an OpenResty release from https://openresty.org/en/download.html (reason: module 'resty.core' not found:
        no field package.preload['resty.core']
        no file './resty/core.lua'
        no file '/usr/local/share/luajit-2.0.5/resty/core.lua'
        no file '/usr/local/share/lua/5.1/resty/core.lua'
        no file '/usr/local/share/lua/5.1/resty/core/init.lua'
        no file './resty/core.so'
        no file '/usr/local/lib/lua/5.1/resty/core.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
        no file './resty.so'
        no file '/usr/local/lib/lua/5.1/resty.so'
        no file '/usr/local/lib/lua/5.1/loadall.so') in /usr/local/nginx/conf/nginx.conf:31

     

    原因是缺少 openresty 或者版本過低
    安裝或升級最新版本:https://openresty.org/en/download.html
    cd /usr/local/lib
    rm -rf ./resty
    ln -s /usr/local/openresty/lualib/resty /usr/local/lib
    nginx 或者/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章