Compiling OpenResty (Nginx) with SSL extensions for LUA

The Lua SSL extensions for OpenResty enables you to use LUA to script SSL operations. For example, you can use this code to switch the SSL certificate depending on the servername.

Download and extract OpenResty:

$ cd /usr/local/src/
$ wget
$ tar vxf ngx_openresty-

Download and extract OpenSSL:

$ wget
$ tar vxf openssl-1.0.2a.tar.gz

Install the prerequisites:

$ apt-get install -y libpcre3-dev

Replace ngx_lua-0.9.15 with the latest one from Github and checkout the ssl-cert-by-lua branch. The SSL support hasn't been included in HEAD yet.

$ cd /usr/local/src/ngx_openresty-
$ rm -rf ngx_lua-0.9.15
$ git clone ngx_lua-0.9.15
$ cd ngx_lua-0.9.15
$ git checkout ssl-cert-by-lua

Patch the Nginx code with support for the SSL context.

$ cd /usr/local/src/ngx_openresty-
$ patch < ../../../ngx_lua-0.9.15/patches/nginx-ssl-cert.patch

Now we have prepared everything and we can build OpenResty:

$ cd /usr/local/src/ngx_openresty-
$ ./configure --with-http_ssl_module --with-luajit --with-openssl=/usr/local/src/openssl-1.0.2a/ --add-module=bundle/lua-nginx-module && make && make install

Copy the SSL lua extension:

$ mkdir -p /usr/local/openresty/lualib/ngx
$ cp build/lua-nginx-module/lua/ngx/ssl.lua /usr/local/openresty/lualib/ngx/ssl.lua

If the build has been succesfull, we can check the configuration:

$ /usr/local/openresty/nginx/sbin/nginx -t

Now you can use both functions ssl_certificate_by_lua and ssl_certificate_by_lua_file.

Currently these functions are supported:

  • ssl_set_der_certificate
  • ssl_clear_certs
  • ssl_set_der_private_key
  • ssl_raw_server_addr
  • ssl_server_name
  • cert_pem_to_der
  • ssl_get_ocsp_responder_from_der_chain
  • ssl_create_ocsp_request
  • ssl_validate_ocsp_response
  • ssl_set_ocsp_status_resp
  • ssl_get_tls1_version

I've added some functions to create dynamic certificates on-the-fly depending on the request, which where used in a content filtering proxy. This solution has been tested under high load and outscales Squid (although I don't have actual numbers). The code will be opensourced soon, still have to clean it up.

Although you shouldn't do this, but if you are wondering how to prevent Nginx to verify proxied certificates, you can disable certificate verification by compiling with: