Adding LUA module to nginx

2020-07-19 03:37发布

问题:

I rpm installed nginx 1.12 on a redhat 7.5 server and It also has LUA 5.1.4 I downloaded lua-nginx-module-0.10.13 tar ball and put it under /etc/nginx/modules, but I am not able to run nginx with LUA auth file.

I also have openresty under /opt/openresty/ ..

http://openresty.org/en/installation.html I followed the "make" method here.

Unfortunately this server doesnt have access to the internet so I cant install stuff from git which slows this down considerably. I am not sure how to add the module here. Any comments would be helpful.

This is what my nginx config looks like ..

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;
    }
}

The lua_access_file is causing an error

nginx: [emerg] unknown directive "access_by_lua_file" Is there some "include" I need to define in the config to get rid of this ?

Thanks.

回答1:

I am breaking down your problem into the small task, as per question and my understanding.

1) The error clearly says that you have not install Lua-nginx-module properly.

Lua-nginx-module documentation

2) The server does not have access to the internet so cannot download from git. *

  • Assuming you are doing ssh into your machine from windows. So, please check below link to copy the files from windows to Linux.

    Installing/Accessing via WinSCP

  • how-to-copy-files-from-one-machine-to-another-using-ssh

  • this step will get all the necessary files on your server.

3) Steps to install nginx with lua-nginx-module.

  • 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

      • 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".
    3. Install with the following command.

        rpm -i rpm-package-name
      

      Install-rpm-file-on-linux

    - Tarball installation for prerequisites.

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

    $ 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.
    

    how-to-reload-nginx-systemctl-or-nginx-s



回答2:

Akshay barahate's answer is long and comprehensive. However to answer your question

Is there some "include" I need to define in the config to get rid of this ?

Try adding

load_module "ngx_http_lua_module.so"

The package name may vary, the above file name is derived from an ubuntu 18.04 installation from package. (Usually you find your modules in /usr/share/nginx/modules)

Note: If you compiled nginx from source (which seems to be the recommended way to get lua running), pathes and filenames can vary.

Happy codin'.



标签: nginx lua