1

How do I set the port via environment variable with lua?

Currently my setup sets $port through lua:

server {

  set_by_lua_block $port {
    return os.getenv("PORT")

  listen       $port;

  }

However, doing so gives me this error:

nginx: [emerg] host not found in "$port" of the "listen" directive in /usr/local/openresty/nginx/conf/nginx.conf:93

Am I doing something wrong? How do I go about doing this?

3
  • Speculation: don't you need a closing curly brace after the return to balance the opening brace? Commented Aug 5, 2017 at 3:21
  • 1
    Listen doesn't accept variables. You have to generate valid config before starting nginx. Use any template tool (bash script, ansible, whatever else) Commented Aug 5, 2017 at 6:00
  • Thanks @AlexeyTen if you want to add that as an answer, I will accept it. Commented Aug 6, 2017 at 0:48

1 Answer 1

1

The answer as posted in a comment, but wasn't posted, so I'll do it to close this out.

Listen doesn't accept variables. You have to generate valid config before starting nginx. Use any template tool (bash script, ansible, whatever else)

Alexey Ten

If you're looking for a templating engine, I recommend ERB. See my answer to a different question here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.