Skip to content

Instantly share code, notes, and snippets.

@DinoChiesa
Created July 4, 2025 03:54
Show Gist options
  • Save DinoChiesa/68549a060716fb605de1dd0cdb7e6910 to your computer and use it in GitHub Desktop.
Save DinoChiesa/68549a060716fb605de1dd0cdb7e6910 to your computer and use it in GitHub Desktop.
Building nginx with lua support

build an nginx container with lua

Following these instructions https://github.com/nginx/docker-nginx/tree/4bf0763f4977fff7e9648add59e0540088f3ca9f/modules

...which are linked from here: https://github.com/nginx/docker-nginx/tree/master/modules

...entitled "Adding third-party modules to nginx official image"

The build relies on source available here: https://hg.nginx.org/pkg-oss

The hg repository doesn't have latest nginx. It seems to stop at 1.26. So you cannot build 1.27 or later this way. If you want that, maybe follow the instructions for "Docker BuildKit", here: https://github.com/nginx/docker-nginx/tree/master/modules

cd make-containers

curl -O https://raw.githubusercontent.com/nginx/docker-nginx/4bf0763f4977fff7e9648add59e0540088f3ca9f/modules/Dockerfile.alpine

docker build --build-arg ENABLED_MODULES="ndk lua"
-f Dockerfile.alpine
--build-arg NGINX_FROM_IMAGE=nginx:1.26.2-alpine
-t my-nginx-1.26-with-lua .

docker build --build-arg ENABLED_MODULES="ndk lua"
-f Dockerfile.alpine
--build-arg NGINX_FROM_IMAGE=nginx:1.20.2-alpine
-t my-nginx-1.20-with-lua .

This worked. At the end of each build there is a notice

The 3rd-party Lua dynamic modules for nginx have been installed.
To enable these modules, add the following to /etc/nginx/nginx.conf
and reload nginx:

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

Note that ndk_http_module.so must be placed first.

These modules are compiled with LuaJIT 2.1 library.
The following binary should be used for testing and bytecode generation:

    /usr/bin/nginx-luajit

Please refer to the modules documentation for further details:
https://github.com/openresty/lua-nginx-module
https://github.com/openresty/stream-lua-nginx-module

To run:

podman run --rm --name nginx-1.20
-p 8081:8080
-v "$(pwd)/nginx-3.conf:/etc/nginx/nginx.conf"
my-nginx-1.20-with-lua

See the other file for my nginx.conf

worker_processes auto;
pid /var/run/nginx.pid;
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
# load_module modules/ngx_stream_lua_module.so;
events {
worker_connections 1024;
}
http {
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log debug;
# error_log with debug works only with the nginx-debug binary
# See https://stackoverflow.com/a/71764652/48082
log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time req_header:"$req_header" resp_header:"$resp_header"';
server {
listen 8080;
location / {
# This block will store inbound headers for logging
set $req_header "";
set $resp_header "";
header_filter_by_lua '
local h = ngx.req.get_headers()
for k, v in pairs(h) do
ngx.var.req_header = ngx.var.req_header .. k.."="..v.." "
end
local rh = ngx.resp.get_headers()
for k, v in pairs(rh) do
ngx.var.resp_header = ngx.var.resp_header .. k.."="..v.." "
end
';
# Proxy to a test upstream service
proxy_pass https://echo.dchiesa.demo.altostrat.com;
proxy_set_header Host echo.dchiesa.demo.altostrat.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_ssl_server_name on;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment