step by step configuration of open-resty-* modules and a simple lua text response

lua-nginx-module is a nginx module that makes it possible to handle http request directly
in nginx using Lua.
It’s possible to install lua-nginx-module by compiling it into nginx. However, I suggest you install OpenResty; the parent project of lua-nginx-module. This is nginx bundled with lua-nginx-module as well as other popular nginx/lua-nginx modules. Installing OpenResty You can run. /configure –help to see available options.

 ./configure \
 --prefix=/opt/resty \
 --conf-path=/opt/resty/nginx.conf \
 --with-cc-opt="-I/usr/local/include" \
 --with-ld-opt="-L/usr/local/lib" \
 --with-pcre-jit \
 --with-ipv6 \
 --with-http_postgres_module \
 --with-http_gunzip_module \
 --with-http_secure_link_module \
 --with-http_gzip_static_module \
 --without-http_redis_module \
 --without-http_redis2_module \
 --without-http_xss_module \
 --without-http_memc_module \
 --without-http_rds_json_module \
 --without-http_rds_csv_module \
 --without-lua_resty_memcached \
 --without-lua_resty_mysql \
 --without-http_ssi_module \
 --without-http_autoindex_module \
 --without-http_fastcgi_module \
 --without-http_uwsgi_module \
 --without-http_scgi_module \
 --without-http_memcached_module \

Change directory to /opt/resty/ and confirm if you can locate find nginx.conf and mime.types. Also confirm if the nginx binary is located at nginx/sbin/nginx. Run nginx with the –V verbose flag to see if there have been any errors in your configuration. We can start nginx with a -p and -c flags. The first overwrites the prefix path nginx was configured with. The second tells nginx to use a customized config file (instead of the standard configuration file). Below is a standard nginx config file:

worker_processes 1;
 daemon off;
 error_log /dev/stdout warn;
 worker_connections 32;
 http {
 default_type text/html;
 access_log off;
 server {
 listen 3000;
 include 'src/proj1.conf';
 There's nothing special here, so let's look at src/proj1.conf:
 location / {
 content_by_lua_block {

you can executes Lua code located in a “handler” module/file. Here’s below is a handler.lua:

 local function process()
 ngx.say('the spice must flow')
 return process

Now you can start nginx manually using the next command;

 # /opt/resty/nginx/sbin/nginx -c ~/code/proj1/develop.conf

You will notice that if you first run http://localhost:3000, a 500 error message is
encountered. If you troubleshoot with the command line we see the error.

 # the handler.lua, which we required, couldn't be found.

To resolve this error the lua_package_path directive must be altered in develop.conf which will add a directory to our search path.
Change your file as follows:

 lua_package_path '${prefix}../../src/?.lua;;';
 server {
 listen 3000;
 include 'src/proj1.conf';

we can change that with the -p flag for Lua to run while overlapping then nginx’s prefixsearch path First though, you’ll need to create some directories:

mkdir -p ~/code/proj1/test/nginx/logs

We can now launch nginx via:

 /opt/resty/nginx/sbin/nginx -c ~/code/proj1/develop.conf -p

And reload our page to get a proper result. You can use any prefix prefix path you want and adjust your lua_package_path accordingly.


Comments are closed.

%d bloggers like this: