Opened 9 years ago

Closed 9 years ago

#900 closed defect (fixed)

Nginx won't daemonise if "NGINX" environment variable is set

Reported by: davidjb Owned by:
Priority: minor Milestone:
Component: documentation Version: 1.9.x
Keywords: Cc:
uname -a: Linux localhost.localdomain 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
nginx -V: nginx version: nginx/1.9.11
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
configure arguments: --with-debug

Description

On non-Win32 platforms, I've found that Nginx won't daemonise if an environment variable with the name NGINX (any value) is present in the parent environment.

I had written the following:

export NGINX=1.9.11
wget -O - http://nginx.org/download/nginx-${NGINX}.tar.gz | tar -xzf -
cd nginx-${NGINX}
./configure --with-debug && make && make install
nginx -c ~/nginx.conf -p ~

and discovered that the final command wouldn't daemonise Nginx, even though daemon on was explicitly present in the configuration. After trial-and-error, I found the cause was the environment variable and I had to consult Nginx's source to discover why this was happening. Nginx believes it is working to inherit sockets (https://github.com/nginx/nginx/blob/master/src/core/nginx.c#L444), which, after the NGINX variable is read from the environment, flags ngx_inherited = 1. The invocation for daemonising is at https://github.com/nginx/nginx/blob/master/src/core/nginx.c#L332, which checks for ngx_inherited and then thus skips over daemonising.

I now know the errors logs *do* output a logging message about this:

2016/02/12 06:52:17 [notice] 8624#0: using inherited sockets from "1.9.11"

but as I had debugging messages enabled, this non-error wasn't obvious amongst the other logged messages.

So, to avoid this pitfall, can the environment variable be made more specific (such as __NGINX_INHERITED_SOCKETS)? Perhaps an error could also be raised/logged if value supplied to the environment variable are incorrectly formatted, and this behaviour be documented so others avoid this same pitfall.

Change History (3)

comment:1 by Maxim Dounin, 9 years ago

The NGINX environment variable is explicitly documented in nginx(8) manual page:

ENVIRONMENT
     The NGINX environment variable is used internally by nginx and should not
     be set directly by the user.

There is no error in your case as "1.9.11" doesn't contain ";" or ":", so nginx doesn't try to parse anything from the variable. The following patch should improve logging in such cases:

# HG changeset patch
# User Maxim Dounin <mdounin@mdounin.ru>
# Date 1455334235 -10800
#      Sat Feb 13 06:30:35 2016 +0300
# Node ID 317abf4680eb22d80f5127c55a966366e8ab522b
# Parent  643527b585ec08b45bd812d436e34c771650063e
Core: improved logging on invalid NGINX variable (ticket #900).

diff --git a/src/core/nginx.c b/src/core/nginx.c
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -481,6 +481,12 @@ ngx_add_inherited_sockets(ngx_cycle_t *c
         }
     }
 
+    if (v != p) {
+        ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
+                      "invalid socket number \"%s\" in " NGINX_VAR
+                      " environment variable, ignoring", v);
+    }
+
     ngx_inherited = 1;
 
     return ngx_set_inherited_sockets(cycle);

It can cause unexpected logging during upgrade from versions prior to 0.1.5, but it's probably not something we care about.

Note well that changing the variable name is not really an option, as it should be known to both old and new nginx versions for upgrade to work.

comment:2 by Maxim Dounin <mdounin@…>, 9 years ago

In 6399:50fb3fd79f76/nginx:

Core: improved logging on invalid NGINX variable (ticket #900).

comment:3 by Maxim Dounin, 9 years ago

Resolution: fixed
Status: newclosed

The patch to improve logging committed, thanks for the report.

Note: See TracTickets for help on using tickets.