Skip to content

NGINX OpenTelemetry Tracing Deployment Guide (ngx_otel_module)

Deploy the ngx_otel_module bundle and configure NGINX tracing.

7 min read Updated SS

This guide covers deploying OpenTelemetry (OTel) tracing for NGINX using the ngx_otel_module bundle — the packaged bundle that ships with an install script, a manifest, and a validation script — and configuring the exporter endpoint and related tracing settings in nginx.conf and/or nginx/conf.d/*.conf.

Work through the sections in order: confirm the prerequisites first, review the configuration reference, then carry out the deployment steps.

Confirm which module bundle you have before you start. There are two different OpenTelemetry modules for NGINX, and their directives are not interchangeable — a configuration written for one will not load under the other. This guide applies only to ngx_otel_module.so, whose directives all begin with otel_ and which exports over OTLP/gRPC. If your bundle instead contains otel_ngx_module.so, is installed by hand by exporting NGINX_OTEL_HOME, and is configured with opentelemetry_* directives over OTLP/HTTP, see the NGINX OpenTelemetry Web Tracing Guide instead. Running Apache, IBM HTTP Server (IHS), or Oracle HTTP Server (OHS)? See the Apache & IBM HTTP Server OpenTelemetry Web Tracing Guide.

Prerequisite checklist#

Confirm every item below before starting the deployment. Skipping these checks is the most common cause of a failed or partially working deployment.

Check Details
Bundle received You have the tar bundle provided to you — the module file plus a manifest describing the exact NGINX version, OS, and architecture it was built for.
NGINX version match Run nginx -v and confirm the version exactly matches the version stated in the bundle's manifest.
OS / architecture match The target host's operating system and CPU architecture match what the bundle was built for. A mismatched bundle will fail to load even if the NGINX version matches.
Administrative access You have root or sudo access on the target host, needed to install the module and modify the NGINX configuration.
Configuration layout known You know the path to the main nginx.conf, and whether the server already uses a conf.d directory — that is, an include conf.d/*.conf; style line inside the http block.
Backup possible You are able to take a backup copy of the current nginx.conf before making any changes.
Collector endpoint reachable You have the OpenTelemetry Collector endpoint (host and port) that traces should be sent to, and this host can reach it over the network.
Deployment and validation scripts available The install script and the validation script provided alongside the module bundle are present and executable on this host.
Maintenance window A maintenance window or change approval is in place, since NGINX has to be reloaded to pick up the new configuration.

Configuration reference#

The directives below control tracing behaviour. They are added to nginx.conf and/or a file under conf.d/, depending on the placement rules that follow the table.

Directive Where it goes Purpose Example
load_module Main context only — the very top of nginx.conf Loads the OpenTelemetry module binary into NGINX. load_module modules/ngx_otel_module.so;
otel_exporter { endpoint ... } http block Sets the OTLP/gRPC address of the OpenTelemetry Collector that traces are sent to. otel_exporter { endpoint <COLLECTOR_IP>:4317; }
otel_service_name http or server block The service name shown for this NGINX instance in the tracing backend. otel_service_name "<YOUR_SERVICE_NAME>";
otel_trace http, server, or location block Turns tracing on or off for the enclosing block. Use off on locations you want to exclude, such as health checks or static assets. otel_trace on;
otel_trace_context location block — typically where requests are passed to a backend Propagates the W3C trace context header to the backend application, so the backend can be linked into the same trace. otel_trace_context propagate;
otel_span_name location block Sets a meaningful, request-specific name for the span. Without this, all requests matching the same location pattern share one generic span name. otel_span_name $uri;

Placement rules#

  • load_module must go at the very top of nginx.conf, before the events and http blocks. It cannot go in a file under conf.d/, because conf.d files are only included from inside the http block — by the time NGINX reads them, the main context has already been parsed.
  • otel_exporter, otel_service_name, otel_trace, otel_trace_context, and otel_span_name may go either directly inside nginx.conf or inside a file under conf.d/ — but only if nginx.conf already includes conf.d files from within its http block. Check for a line such as include /etc/nginx/conf.d/*.conf;. If that include is not already present, add these directives directly inside nginx.conf instead.
  • Keep every edit inside the correct block: otel_exporter and otel_service_name belong at the http level; otel_trace_context and otel_span_name belong inside the specific location block or blocks that should be traced.

Deployment steps#

Step 1: Extract the bundle#

Copy the tar bundle to the target host, then extract it:

tar xzf <bundle-file-name>.tar.gz

Confirm the extracted folder contains the module file and a manifest file, along with the install and validation scripts.

Step 2: Run the installation script#

Run the provided installation script, pointing it at the extracted bundle. It installs the module file into the correct NGINX modules directory, adds the load_module line to the top of nginx.conf, and creates a timestamped backup of the original nginx.conf before making any change.

sudo ./install_on_host.sh /path/to/<bundle-file-name>.tar.gz

If the manifest includes a checksum value, pass it in so the script can verify the module file has not been corrupted or tampered with in transit:

export EXPECTED_SHA256="<sha256 value from the manifest>"
sudo ./install_on_host.sh /path/to/<bundle-file-name>.tar.gz

Note the backup filename the script prints — you need it for the rollback procedure.

Note: The script stops short of editing the http, server, and location blocks of nginx.conf. Step 3 covers that, using the configuration reference above.

Step 3: Add the OpenTelemetry configuration#

Using the directive reference and the placement rules above, add the tracing configuration to nginx.conf — or to a conf.d file, where that applies. A typical http-level and location-level configuration looks like this:

http {
    otel_exporter {
        endpoint <COLLECTOR_IP>:4317;
    }

    otel_service_name "<YOUR_SERVICE_NAME>";
    otel_resource_attr "deployment.environment" "production";

    # ... existing http-level configuration stays as-is ...
}       # End of http block

server {
    otel_trace on;

    location ~ \.php$ {
        otel_trace_context propagate;
        otel_span_name $uri;
        # ... existing fastcgi_pass / proxy_pass configuration stays as-is ...
    }
}       # End of server block

Replace <COLLECTOR_IP> and <YOUR_SERVICE_NAME> with the values you confirmed in the prerequisite checklist. Add otel_trace_context and otel_span_name only to the location blocks you actually want traced — typically the ones that pass requests to an application backend, rather than static-file locations.

Note: A single-file NGINX configuration may not lay out separate http and server blocks the way the example does. Place each directive in the enclosing block it belongs to, following the placement rules above.

Step 4: Test the configuration#

Before reloading NGINX, validate the syntax of the updated configuration:

sudo nginx -t

If this reports an error naming the module — a version mismatch, for example — do not proceed. The module bundle does not match this host's NGINX build, and you need to request a new bundle built for this host's exact NGINX version.

Step 5: Reload NGINX#

Once the test in Step 4 passes, apply the change:

sudo systemctl reload nginx

Step 6: Run the validation script#

Run the provided validation script to confirm the module loaded correctly and the tracing directives are active:

./validate_tracing.sh

The script checks that nginx -t still passes, confirms the otel_* directives are present in the running configuration, and sends a test request. Then confirm separately, in your tracing backend, that a new trace appears for the service name you configured.

Step 7 (optional): Confirm trace context at a PHP-FPM backend#

If NGINX passes requests to a PHP-FPM backend, use the provided PHP check file to confirm the trace context is reaching the application:

  1. Copy the check file into the application's web directory, under a name that will not collide with any existing application file.
  2. Request it directly:

    curl http://localhost/<check-file-name>.php
    
  3. Confirm the response reports that the trace context was received.

  4. Remove the check file once validation is complete. It is for temporary use only and must not stay reachable afterwards.

Rollback procedure#

If an issue turns up after deployment, restore the configuration backup the installation script created automatically in Step 2.

  1. Remove the otel_* directives you added to the http, server, and location sections of the NGINX configuration.
  2. Restore the backup taken before the module was installed:

    sudo cp /etc/nginx/nginx.conf.pre-otel.<timestamp>.bak /etc/nginx/nginx.conf
    
  3. Validate the restored configuration and reload:

    sudo nginx -t && sudo systemctl reload nginx
    

Note: The exact backup filename, including its timestamp, is printed by the installation script when it runs. Always confirm with sudo nginx -t that the restored configuration is valid before reloading.

Next