9

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.

4 Answers 4

17

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

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

  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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I was following these steps when I had a run at it the first time, I was able to gather the relevant files and start to compile but I was having issues with the command on "building nginx" previously. I will give this another try.
@ScipioAfricanus Yes, I totally understand building "Lua-nginx-module" is bit tricky and It does not provide expected results sometimes. I too faced many issues and drafted this after several attempts. It worked for me, give it a try and Do let us know, how it goes for you.
@Akshaybarahate, I faced error for make install /home/nginx-build/lua-nginx-module-0.10.8/src/ngx_http_lua_headers.c:227:15: error: incompatible types when assigning to type ‘ngx_buf_t * {aka struct ngx_buf_s *}’ from type ‘ngx_chain_t {aka struct ngx_chain_s}’ b = hc->busy[i]; ^ objs/Makefile:1446: recipe for target 'objs/addon/src/ngx_http_lua_headers.o' failed make[1]: *** [objs/addon/src/ngx_http_lua_headers.o] Error 1 make[1]: Leaving directory '/home/nginx-build/nginx-1.19.3' Makefile:11: recipe for target 'install' failed make: *** [install] Error 2
10

Now in most Operating Systems (e.g. Ubuntu, etc) Lua support for Nginx can be enabled by installing the libnginx-mod-http-lua package. e.g:

sudo apt install libnginx-mod-http-lua

This package includes the dynamic library and the corresponding load_module directive in /usr/share/nginx/modules-available/mod-http-lua.conf which is usually enabled when the package is installed (by softlinking it into the /etc/nginx/modules-enabled/ directory).

Note: Sometimes it seems that an error (e.g. nginx: [emerg] dlopen() "/usr/share/nginx/modules/ngx_http_lua_module.so" failed (/usr/share/nginx/modules/ngx_http_lua_module.so: undefined symbol: ndk_set_var_value) in /etc/nginx/modules-enabled/mod-http-lua.conf:1 ) can occur when things are apparently set up ok. The ordering of the module imports can be an issue and this can be fixed by explicitly including them in the /etc/nginx/nginx.conf config file before this line (or by commenting it out):
include /etc/nginx/modules-enabled/*.conf;

load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
load_module modules/ngx_stream_module.so;

3 Comments

This saved me from installing resty on raspberry 32bit with debian. Thanks.
unfortunately that's not enough unless your nginx is compiled to include this module. As of Dec 2024 nginx is no longer including lua module, so you have to use resty or compile nginx
It is still available for the current latest Debian release (trixie) - latest version on 8feb25: packages.debian.org/trixie/libnginx-mod-http-lua
5

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;

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.