title: How to pass to Nginx fastcgi_pass inside ngx_http_lua_module?
body: <p>I need to pass an Nginx variable to my PHP 7.0 backend using the excellent library <a href="https://github.com/openresty/lua-nginx-module" rel="nofollow">https://github.com/openresty/lua-nginx-module</a></p>
  
  <p>I prefer to use <code>content_by_lua_block</code> instead of <code>set_by_lua_block</code>, because the documentation for the 'set' function states "This directive is designed to execute short, fast running code blocks as the Nginx event loop is blocked during code execution. Time consuming code sequences should therefore be avoided.".
  <a href="https://github.com/openresty/lua-nginx-module#set_by_lua" rel="nofollow">https://github.com/openresty/lua-nginx-module#set_by_lua</a></p>
  
  <p>However, since the 'content_...' function is non-blocking, the following code does not return in time, and $hello is unset when passed to PHP:</p>
  
  <pre><code>location ~ \.php{
      set $hello '';
  
      content_by_lua_block {
          ngx.var.hello = "hello there.";
      }
  
      fastcgi_param HELLO $hello;
      include fastcgi_params;
      ...
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  }
  </code></pre>
  
  <p>The problem is, my Lua code has the potential to be "time consuming code sequences" if certain code paths are taken, using crypto for example.</p>
  
  <p>The following Nginx location works just fine, but that's because set_by_lua_block() is a blocking function call:</p>
  
  <pre><code>location ~ \.php {
      set $hello '';
  
      set_by_lua_block $hello {
          return "hello there.";
      }
  
      fastcgi_param HELLO $hello;
      include fastcgi_params;
      ...
      fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  }
  </code></pre>
  
  <p>My question is, what is the best approach here?  Is there a way to call the Nginx directive <code>fastcgi_pass</code> and related directives from within a content_by_lua_block() only after my variables have been set?</p>