将LUA模块添加到nginx

7

我在Redhat 7.5服务器上通过rpm安装了nginx 1.12,并且还有LUA 5.1.4。我下载了lua-nginx-module-0.10.13 tar ball并放置在/etc/nginx/modules目录下,但是我却无法使用LUA auth文件运行nginx。

我还在/opt/openresty/下安装了openresty。

http://openresty.org/en/installation.html中,我遵循了此处的“make”方法。

不幸的是,这台服务器无法访问互联网,因此我无法从git中安装软件,这使得速度变得缓慢。我不知道如何在此处添加模块。任何意见都将是有帮助的。

这是我的nginx配置示例:

server
{
    listen 80;

    access_log  /opt/elk/logs/nginx/access.log  main;

    #auth_basic "admin";
    #auth_basic_user_file "/etc/nginx/passwd";

    client_max_body_size 100M;

    location /
    {
        proxy_pass http://127.0.0.1:9200;

        keepalive_timeout 300s;

        #auth_basic on;
        auth_basic "admin";
        auth_basic_user_file "/etc/nginx/passwd";

        access_by_lua_file '/etc/nginx/authorized.lua';
    }

    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   /usr/share/nginx/html;
    }
}

lua_access_file引起了一个错误。

nginx: [emerg] 未知指令“access_by_lua_file”,我需要在配置文件中定义某些“include”来解决这个问题吗?

谢谢。

4个回答

17
我将根据问题和我的理解,将您的问题分解成小任务。 1) 错误明确表示您未正确安装 Lua-nginx-module。 Lua-nginx-module文档 2) 服务器无法访问互联网,因此无法从git下载。 如何使用ssh在两台机器之间复制文件
  • 这一步将在您的服务器上获取所有必要的文件。
3) 安装带有lua-nginx-module的nginx的步骤。
  • lua nginx module compatibility check.

     Nginx Compatibility
         The latest version of this module is compatible with the following versions of Nginx:
    
         1.13.x (last tested: 1.13.6)
         1.12.x
         1.11.x (last tested: 1.11.2)
         1.10.x
         1.9.x (last tested: 1.9.15)
         1.8.x
         1.7.x (last tested: 1.7.10)
         1.6.x
    
         Nginx cores older than 1.6.0 (exclusive) are not supported.
    

    referance document for nginx compatibility

  • Prerequisites

    **- Centos/RHEL**[**In case if internet is working in your server**].
    
     yum install -y wget unzip gcc make openssl-devel pcre-devel zlib-devel 
    

    - Downloading .rpm package manually and installing.

  1. Search the prerequisites from the RPM resource site

  2. Copy the file in your Linux box

    • Please refer above point (2)"The server does not have access to the internet so cannot download from git".
    1. Install with the following command.

        rpm -i rpm-package-name
      

    Install-rpm-file-on-linux

- 先决条件的Tarball安装。

 - [Installing gcc from source code ][6]         Similarly,you can look for
          other prerequistes.
  • Downloading the source

     $ rm -fr /tmp/nginx-build  
     $ mkdir /tmp/nginx-build
     $ cd /tmp/nginx-build
    
     $ wget http://nginx.org/download/nginx-1.13.0.tar.gz
    
     $ wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
    
     $ wget -O nginx_devel_kit.tar.gz https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
    
     $ wget -O nginx_lua_module.tar.gz https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz
    
  • Extracting

      $ tar xvf LuaJIT-2.0.4.tar.gz
    
      $ tar xvf nginx-1.11.10.tar.gz
    
      $ tar xvf nginx_devel_kit.tar.gz
    
      $ tar xvf nginx_lua_module.tar.gz
    
  • Building LuaJIT

    To build Nginx with LuaJIT, we need to build LuaJIT first. This is as simple as a make command

        $ cd /tmp/nginx-build/LuaJIT-2.0.4
        $ make install
         ==== Building LuaJIT 2.0.4 ====
         make -C src
         make[1]: Entering directory `/tmp/nginx/LuaJIT-2.0.4/src'
         ...
         ...
         ln -sf luajit-2.0.4 /usr/local/bin/luajit
         ==== Successfully installed LuaJIT 2.0.4 to /usr/local ====
    
  • Building Nginx

         $ cd /tmp/nginx-build/nginx-1.11.10
         $ LUAJIT_LIB=/usr/local/lib LUAJIT_INC=/usr/local/include/luajit-2.0 \
         ./configure \
         --user=nobody                          \
         --group=nobody                         \
         --prefix=/etc/nginx                   \
         --sbin-path=/usr/sbin/nginx           \
         --conf-path=/etc/nginx/nginx.conf     \
         --pid-path=/var/run/nginx.pid         \
         --lock-path=/var/run/nginx.lock       \
         --error-log-path=/var/log/nginx/error.log \
         --http-log-path=/var/log/nginx/access.log \
         --with-http_gzip_static_module        \
         --with-http_stub_status_module        \
         --with-http_ssl_module                \
         --with-pcre                           \
         --with-file-aio                       \
         --with-http_realip_module             \
         --without-http_scgi_module            \
         --without-http_uwsgi_module           \
         --without-http_fastcgi_module ${NGINX_DEBUG:+--debug} \
         --with-cc-opt=-O2 --with-ld-opt='-Wl,-rpath,/usr/local/lib' \
         --add-module=/tmp/nginx/ngx_devel_kit-0.3.0 \
         --add-module=/tmp/nginx/lua-nginx-module-0.10.8
         $ make install
    
  • Syntanx Check

      $ nginx -t
         nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
         nginx: configuration file /etc/nginx/nginx.conf test is successful
    
  • Nginx Lua Testing

    *As per you nginx file.

      location /
     {
     proxy_pass http://127.0.0.1:9200;
    
     keepalive_timeout 300s;
    
     #auth_basic on;
     auth_basic "admin";
     auth_basic_user_file "/etc/nginx/passwd";
    
     access_by_lua_file '/etc/nginx/authorized.lua'; }
    
  • Reload / restart nginx

         systemctl nginx restart
         systemctl nginx reload.
    

如何重载Nginx(使用systemctl或nginx -s)?


谢谢,我第一次尝试时正在按照这些步骤操作,我能够收集相关文件并开始编译,但之前在“构建Nginx”的命令上出现了问题。我会再试一次。 - ScipioAfricanus
2
@ScipioAfricanus 是的,我完全理解构建“Lua-nginx-module”有点棘手,有时它并不能提供预期的结果。我也遇到了许多问题,并在几次尝试后起草了这篇文章。它对我有效,你可以试试看,然后告诉我们它的效果如何。 - Akshay barahate
2
@Akshaybarahate,我在执行make install时遇到了错误: /home/nginx-build/lua-nginx-module-0.10.8/src/ngx_http_lua_headers.c:227:15: 错误:将类型“ngx_chain_t {aka struct ngx_chain_s}”分配给类型“ngx_buf_t * {aka struct ngx_buf_s *}”时不兼容 b = hc->busy[i]; ^ objs/Makefile:1446: 目标“objs/addon/src/ngx_http_lua_headers.o”的规则失败 make[1]: *** [objs/addon/src/ngx_http_lua_headers.o] 错误 1 make[1]: 离开目录“/home/nginx-build/nginx-1.19.3” Makefile:11: 目标“install”的规则失败 make: *** [install] 错误 2 - Techiescorner

5

现在,在大多数操作系统中(如Ubuntu等),可以通过安装libnginx-mod-http-lua软件包来启用Nginx对Lua的支持。例如:

sudo apt install libnginx-mod-http-lua

该软件包包含动态库以及相应的load_module指令,位于/usr/share/nginx/modules-available/mod-http-lua.conf中,通常在软件包安装时启用(通过将其软链接到/etc/nginx/modules-enabled/目录中)。


这让我在树莓派32位debian上安装resty时省了不少事。谢谢。 - philippe lhardy

4
如果您正在使用Docker运行nginx,您只需按照说明docker-nginx的github存储库上操作即可。
在您的nginx-conf中,请确保通过添加以下内容来加载模块。
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
load_module modules/ngx_stream_lua_module.so;

1
Akshay Barahate的回答长而全面。 但是为了回答你的问题,

我需要在配置中定义一些“include”来消除这个问题吗?

尝试添加

load_module "ngx_http_lua_module.so"

软件包名称可能会有所不同,上述文件名来自于Ubuntu 18.04安装包。(通常您可以在/usr/share/nginx/modules中找到模块)

注意:如果您从源代码编译nginx(似乎是运行lua的推荐方式),路径和文件名可能会有所不同。

祝编程愉快。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接