For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sign inTry it free
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
DocsGuidesSDKsIntegrationsAPI docsTutorialsFlagship blog
  • Guides
    • Cheatsheets
    • Feature flags
    • AgentControl
    • Experimentation
    • Statistical methodology
    • Metrics
    • Infrastructure
    • Account management
    • Teams and custom roles
    • SDKs
      • Unit testing with Jest
      • Use cases for SDK wrappers
      • Using LaunchDarkly without a supported SDK
      • Using mobile SDKs
      • Using the Lua SDK with HAProxy
      • Using the Lua SDK with NGINX
      • Using the JavaScript SDK in Salesforce Lightning Web Components
      • Resilient architecture patterns for LaunchDarkly's SDKs
    • Integrations
    • REST API
    • Additional resources
Sign inTry it free
LogoLogo
On this page
  • Prerequisites
  • Preparing the Lua server-side SDK
  • Preparing the C++ server-side SDK
  • Ensuring correct initialization
  • Example: Feature flagged reverse proxy
GuidesSDKs

Using the Lua SDK with NGINX

Was this page helpful?
Previous

Using the JavaScript SDK in Salesforce Lightning Web Components

Next
Built with

This guide explains how to use our Lua server-side SDK with the NGINX OpenResty framework.

OpenResty extends NGINX with LuaJIT, enabling complex control of NGINX functionality. The OpenResty framework has substantial commercial adoption.

Find a basic Dockerized app in the GitHub repository at hello-nginx.

Prerequisites

To complete this guide, you must have the following prerequisites:

  • Basic working knowledge of the LaunchDarkly Lua server-side SDK
  • Basic working knowledge of the LaunchDarkly C++ server-side SDK

The C++ server-side SDK is required because the Lua server-side SDK is implemented as a wrapper around the C++ server-side SDK.

Preparing the Lua server-side SDK

Make the source of the Lua SDK accessible to OpenRESTY. You can control imports with the lua_package_path directive.

For example:

NGINX
1http {
2 lua_package_path ";;/usr/local/openresty/nginx/scripts/?.lua;";
3 ...
4}

Preparing the C++ server-side SDK

Make the source of the C++ server-side SDK accessible to the dynamic linker. The most convenient way to do this is to install the binary system-wide at /usr/lib/libldserverapi.so.

Configuring OpenSSL with OpenResty

OpenResty handles OpenSSL in specific ways. You may need to build the SDK from scratch instead of using our release artifacts in order to make OpenSSL work correctly.

Ensuring correct initialization

The most important part of effective SDK usage is managing the lifetime of clients correctly. Because NGINX utilizes process-based concurrency, multiple clients initiate. If you accidentally initiate a client per request the application will be substantially slower, as the SDK has to download a fresh ruleset from LaunchDarkly.

Initialize each NGINX worker process exactly once for ideal operations. You can do this with the init_worker_by_lua_file directive. This directive executes a script when a process is freshly spawned. Client initialization should reside in this script. In the example below, this file is called shared.lua.

Here is an example of initialization logic:

1local ld = require("launchdarkly_server_sdk")
2local os = require("os")
3
4return ld.clientInit(os.getenv(sdk_key), 1000, config)

Later we can use the result of this initialization process in other directives.

1local ld = require("launchdarkly_server_sdk")
2local client = require("shared")
3local os = require("os")
4
5local context = ld.makeContext({
6 user = {
7 key = "abc"
8 }
9})
10
11if client:boolVariation(context, "example-flag-key", false) then
12 ngx.say("<p>feature launched</p>")
13else
14 ngx.say("<p>feature not launched</p>")
15end

Example: Feature flagged reverse proxy

This reverse proxy example demonstrates more complex interaction between multiple NGINX directives. You can use a reverse proxy to route traffic to multiple applications.

The example follows:

1location / {
2 set $proxy_host "";
3
4 rewrite_by_lua_block {
5 local ld = require("launchdarkly_server_sdk")
6 local client = require("shared")
7
8 local context = ld.makeContext({
9 user = {
10 key = "abc"
11 }
12 })
13
14 ngx.var.proxy_host = client:stringVariation(context, "example-flag-key", "10.0.0.0")
15 }
16
17 proxy_pass https://$proxy_host$uri;
18 proxy_set_header Host $proxy_host;
19 proxy_set_header X-Forwarded-For $remote_addr;
20}