# NGINX logs enrichment whether Dynatrace is enabled or not

**URL:** <https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128>\
**Category:** Logs\
**Tags:** nginx, log-monitoring-class\
**Created:** [June 8, 2023, 3:29pm UTC](https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128 "2023-06-08T15:29:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad](https://avatars.discourse-cdn.com/v4/letter/a/a4c791/32.png) [@ahmad](https://community.dynatrace.com/u/ahmad)\
**Post date:** [June 8, 2023, 3:29pm UTC](https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128/1 "2023-06-08T15:29:50Z")

</div>

Hello all,

I’ve been following the [article](https://www.dynatrace.com/support/help/observe-and-explore/logs/log-monitoring/log-monitoring-configuration/log-enrichment#retrieve-span-and-trace-ids) to enable Nginx logs enrichment and to include the DT in the&nbsp;Nginx logs however, I have Dynatrace implemented in one environment and it works fine but when I try to run&nbsp;Nginx on my local machine I get the following error:

```markup
[emerg] 1#1: unknown "dt_trace_id" variable
nginx: [emerg] unknown "dt_trace_id" variable

```

My config looks like this:

```c
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /tmp/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format custom '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '[!dt dt.trace_id=$dt_trace_id,dt.span_id=$dt_span_id,dt.trace_sampled=$dt_trace_sampled] '
                        '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log custom;

    sendfile on;
    #tcp_nopush on;

    keepalive_timeout 65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;
}

```

Anyone can advise on how I can do Nginx logs enrichment that can work whether Dynatrace is enabled or not?

Thank you.

---

<div class="post-metadata">

**Author:** ![ChadTurner](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/chadturner/32/161_2.png) [@ChadTurner](https://community.dynatrace.com/u/ChadTurner)\
**Post date:** [August 3, 2023, 7:41pm UTC](https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128/2 "2023-08-03T19:41:57Z")

</div>

[@ahmad](https://community.dynatrace.com/user/63603)&nbsp;were you able to solve this issue or are you still experiencing it? If it was solved, what was the underlying problem?

---

<div class="post-metadata">

**Author:** ![danmichael](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/danmichael/32/32395_2.png) [@danmichael](https://community.dynatrace.com/u/danmichael)\
**Post date:** [December 14, 2023, 2:44pm UTC](https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128/3 "2023-12-14T14:44:18Z")

</div>

Is nginx instrumented locally? What version of nginx is being run? Do you have the oneagent installed?

Nginx logs enrichment that can work whether Dynatrace is enabled or not? -- using this method depends on the nginx process being instrumented by the OneAgent

---

<div class="post-metadata">

**Author:** ![jasonbutler22](https://avatars.discourse-cdn.com/v4/letter/j/b3f665/32.png) [@jasonbutler22](https://community.dynatrace.com/u/jasonbutler22)\
**Post date:** [June 25, 2025, 12:07am UTC](https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128/4 "2025-06-25T00:07:35Z")

</div>

We also have dev and test environments without Dynatrace. My strategy is to run a startup script to detect if Dynatrace is instrumenting the Docker container my nginx server is running in. The script tests if the /etc/ld.so.preload is set to load the dynatrace one agent library. If it is detected then I update the logging config with the Dynatrace specific logging patterns. e.g.

```auto
# Check if the Dynatrace OneAgent is installed by checking for the presence of
# liboneagentproc in the /etc/ld.so.preload file.
if grep -q "liboneagentproc" "/etc/ld.so.preload" ; then
echo "Enabling Dynatrace logging instrumentation"
# Update or replace nginx.conf to insert Dynatrace pattern [!dt dt.trace_id=$dt_trace_id]
# into the logging config here.
fi

```

---

<div class="post-metadata">

**Author:** ![radek\_jasinski](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/radek_jasinski/32/10432_2.png) [@radek\_jasinski](https://community.dynatrace.com/u/radek_jasinski)\
**Post date:** [June 25, 2025, 7:49am UTC](https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128/5 "2025-06-25T07:49:15Z")

</div>

Hi,

You can try solving this by defining fallback values for the Dynatrace variables using the map directive in NGINX. This is useful when Dynatrace OneAgent is not installed (e.g., in local environments), and NGINX would otherwise throw an error due to unknown variables like $dt\_trace\_id.

map “” $dt\_trace\_id\_safe {  
default “-”;  
}

map “” $dt\_span\_id\_safe {  
default “-”;  
}

map “” $dt\_trace\_sampled\_safe {  
default “-”;  
}

then update your log\_format.

Radek

---

<div class="post-metadata">

**Author:** ![avdveen](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@avdveen](https://community.dynatrace.com/u/avdveen)\
**Post date:** [June 26, 2025, 1:12pm UTC](https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128/6 "2025-06-26T13:12:39Z")

</div>

Hello&nbsp;[@radek\_jasinski](https://community.dynatrace.com/user/16832),&nbsp;do you have a working example of this that you can share? I’d like to see where you placed that and in which file, the nginx.conf or another? I also have a need for [Nginx log enrichment](https://docs.dynatrace.com/docs/shortlink/log-monitoring-log-enrichment#example-of-manually-enriching-nginx-log-data), but I get the same error as&nbsp;[@ahmad](https://community.dynatrace.com/user/63603)&nbsp;did.

I have the agent monitoring the nginx process in full stack, the OneAgent module **dt\_one\_agent** &nbsp;shows up in the modules list.

---

<div class="post-metadata">

**Author:** ![radek\_jasinski](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/radek_jasinski/32/10432_2.png) [@radek\_jasinski](https://community.dynatrace.com/u/radek_jasinski)\
**Post date:** [June 27, 2025, 11:48am UTC](https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128/7 "2025-06-27T11:48:31Z")

</div>

Yes, i will share later some example

---

<div class="post-metadata">

**Author:** ![Mo\_Azuz](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/mo_azuz/32/5921_2.png) [@Mo\_Azuz](https://community.dynatrace.com/u/Mo_Azuz)\
**Post date:** [August 19, 2025, 9:12pm UTC](https://community.dynatrace.com/t/nginx-logs-enrichment-whether-dynatrace-is-enabled-or-not/40128/8 "2025-08-19T21:12:06Z")

</div>

Hi&nbsp;[@radek\_jasinski](https://community.dynatrace.com/user/16832), are you able to share any examples? Thank you!
