For a project I'm working on, we require nginx and lua, and we start it like this:

MYVAR1=some_value MYVAR2=some_value /usr/local/openresty/nginx/sbin/nginx -p `pwd`/ -c nginx.conf

And everything works. If, instead, we do this:

export MYVAR1=some_value
export MYVAR2=some_value
/usr/local/openresty/nginx/sbin/nginx -p `pwd`/ -c nginx.conf

Then, a lua script running inside the worker process complains that MYVAR1 and MYVAR2 are not set. What is the difference between these 2, and why isn't nginx+lua picking them up in the 2nd version?

  • 4
    Note that if your shell is /bin/sh, that shell may not support the export var=val syntax. You may have to write var=val; export var. The main difference is that the first only export the variables for nginx and its children; the second will make the vars available for any process that shell spawns. Apr 2 '15 at 10:37

If running under bash, there should be no difference, other than that MYVAR1 and MYVAR2 will continue to be set to the same value once /usr/local/openresty/nginx/sbin/nginx is finished executing. The first syntax you showed will only have the variables exist for the lifetime of nginx's execution.

Now, if you're not using bash, (for example, /bin/sh, or another POSIX-compliant shell) you may need an alternate syntax, setting the environment variables before exporting them:

export MYVAR1

export MYVAR2

I'm not sure if you're doing this from a shell terminal or via a shell script, but you can determine this via:

ps -ef | grep $$ | grep -v grep

If it's /bin/sh, or some other shell, this is your problem. If you're doing this inside a shell script, you can change the shebang line (#!/... at the top of the file) to #!/bin/bash instead of #!/bin/sh, or use sh-compliant syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.