search close

Working with Multiple Lua Scripts in Nginx

access_time Updated Jun 20, 2021

Currently, Nginx only supports one init_by_lua or init_by_lua_file, which is used by the Signal Sciences Nginx module. If you have your own Lua scripts embedded within Nginx, you will need to splice the Signal Sciences module into your custom Lua code.

Note: By not using the sigsci.conf configuration file, you will not receive configuration file updates when the module is upgraded. You should take care and review your Lua module when a Signal Sciences module release is updated.

Removing the Signal Sciences Nginx Lua Module

Before you add our module into your existing Lua code, you’ll need to remove any references to the sigsci include file: Look for and remove any lines that look like:

include /opt/sigsci/nginx/sigsci.conf;

Next, the following should be added to your Nginx configuration:

lua_shared_dict sigsci_conf 12k;
lua_use_default_type off;

Within your init_by_lua or the file specified by init_by_lua_file, include the following snippet:

package.path = "/opt/sigsci/nginx/?.lua;" .. package.path
sigsci = require("SignalSciences")

Lastly, you will need to add an access_by_lua and log_by_lua into your Nginx configuration. If you already have these directives defined, copy the sigsci.prerequest() and sigsci.postrequest() statements to their respective Lua callers.

access_by_lua 'sigsci.prerequest()';
log_by_lua    'sigsci.postrequest()';

After adding those lines to your custom Lua scripts, restart Nginx.