Marian
Marian

Reputation: 15358

How do I add the Lua module for nginx on Alpine linux?

I'd like to have a lean Docker image for nginx with the Lua module enabled. How can I create this based on Alpine linux?

Upvotes: 28

Views: 41675

Answers (6)

GammaGames
GammaGames

Reputation: 1829

Building on Luc Chauvin's answer, here's a full walkthrough using the information from the guide on GitHub as well as the changes necessary to get it working with docker compose. This guide assumes that you previously used the provided alpine nginx image with the service set up like:

services:
  nginx:
    image: nginx:alpine
  1. Download the base builder dockerfile: https://github.com/nginxinc/docker-nginx/blob/master/modules/Dockerfile.alpine (there is also a debian based version available).

  2. Update the service in your docker-compose.yaml file to build the modules. Use the folder containing the dockerfile for the context, for this example I've placed it alongside the compose file:

    services:
      nginx:
        build:
          context: ./
          args:
            ENABLED_MODULES: ndk lua
    
  3. Add the following to the top of your nginx.conf to load the modules:

    load_module modules/ndk_http_module.so;
    load_module modules/ngx_http_lua_module.so;
    
  4. Build the container with docker compose build. This will take a while, but at the end you should be able to docker compose up without issue.

Upvotes: 1

Luc Chauvin
Luc Chauvin

Reputation: 41

See: "Adding third-party modules to nginx official image" At: https://github.com/nginxinc/docker-nginx/tree/master/modules

"It's possible to extend a mainline image with third-party modules either from your own instuctions following a simple filesystem layout/syntax using build_module.sh helper script, or failing back to package sources from pkg-oss."

$ docker build --build-arg ENABLED_MODULES="ndk lua" -t my-nginx-with-lua .

Upvotes: 4

Piero
Piero

Reputation: 1853

Dockerfile:

FROM nginx:1.15-alpine
RUN  mkdir -p /run/nginx
RUN  apk add --no-cache nginx-mod-http-lua
COPY nginx_conf/ /etc/nginx/ # Your nginx conf
COPY lua/ /etc/lua/          # Your lua files 

First line of nginx conf:

load_module /usr/lib/nginx/modules/ndk_http_module.so;
load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
pcre_jit on;

Upvotes: 7

dirtyqwerty
dirtyqwerty

Reputation: 305

We use Openresty, a platform that integrates nginx and Lua.

In the default nginx file, you could call Lua like so:

server {
    listen 80;
    listen 443 ssl; # 'ssl' parameter tells NGINX to decrypt the traffic

    # 1
    location ~ /api/(.*) {
        resolver xxx.x.x.xx;

    rewrite_by_lua_block {
        ngx.req.set_header("x-header", "12345678901234567")

    }
}

The alpine image here: https://github.com/openresty/docker-openresty/tree/master/

There is also an alpine-fat that had make, git and other libraries that can help you build within your Docker image.

Upvotes: 3

user2915097
user2915097

Reputation: 32176

You look on the Docker Hub

https://hub.docker.com/

and you find an Nginx image, based on Alpine Linux, with Lua support

Some examples

https://hub.docker.com/r/ilagnev/alpine-nginx-lua/

or

https://hub.docker.com/r/firesh/nginx-lua/

Have a look at the Dockerfile for more details

Upvotes: -4

Marian
Marian

Reputation: 15358

Here is a Dockerfile:

FROM alpine:3.6

RUN apk add --no-cache nginx-mod-http-lua

# Delete default config
RUN rm -r /etc/nginx/conf.d && rm /etc/nginx/nginx.conf

# Create folder for PID file
RUN mkdir -p /run/nginx

# Add our nginx conf
COPY ./nginx.conf /etc/nginx/nginx.conf

CMD ["nginx"]

Installing the nginx-mod-http-lua package will also install nginx and luajit, among others.

The nginx.conf should contain at least this:

load_module /usr/lib/nginx/modules/ndk_http_module.so;
load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;

pcre_jit on;

events {
  worker_connections 1024;
}

daemon off;

Upvotes: 25

Related Questions