Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding LUA module to nginx

Tags:

nginx

lua

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.

like image 872
ScipioAfricanus Avatar asked May 15 '18 19:05

ScipioAfricanus


People also ask

How does Lua work with nginx?

Nginx+Lua is a self-contained web server embedding the scripting language Lua. Powerful applications can be written directly inside Nginx without using cgi, fastcgi, or uwsgi. By adding a little Lua code to an existing Nginx configuration file, it is easy to add small features.

How do I install nginx modules?

Download the Nginx source code. Run the Nginx configure script with --add-module parameters, like this: ./configure --prefix=/somewhere --add-module=/path-to-your-module. Run make && sudo make install to compile and install Nginx.

What is NJS in nginx?

njs is a subset of the JavaScript language that allows extending nginx functionality. njs is created in compliance with ECMAScript 5.1 (strict mode) with some ECMAScript 6 and later extensions. The compliance is still evolving. Download and install. Changes.

What are modules in nginx?

NGINX, at its core, is a collection of modules. Whether you are using core modules, like the http and stream (TCP/UDP) modules, or third‑party modules like GeoIP or RTMP, the module framework is the same. With the addition of dynamic module support, modules are an even better way to add functionality to NGINX.


2 Answers

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

        rpm -i rpm-package-name
      

    Install-rpm-file-on-linux

- Tarball installation for prerequisites.

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

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

like image 71
Akshay barahate Avatar answered Oct 16 '22 22:10

Akshay barahate


In case you are running nginx with docker you can just follow the instructions on the docker-nginx github repository.

In your nginx-conf make sure to also load the module by adding

load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
load_module modules/ngx_stream_lua_module.so;
like image 22
nflaig Avatar answered Oct 17 '22 00:10

nflaig