title: Dynamically listen on an nginx port
body: <p>I'd like to pass a port to Nginx to listen on dynamically. So I can write something like:</p>
  
  <pre><code>PORT=4567 nginx -c $PWD/nginx.conf
  </code></pre>
  
  <p>and then have an nginx config that looks something like:</p>
  
  <pre><code>http {
      server {
          listen $PORT;
      }
  }
  </code></pre>
  
  <p>and have nginx listen on the specified port. I tried compiling nginx with lua support, and writing:</p>
  
  <pre><code>events {
      worker_connections 200;
  }
  
  env SERVER_PORT;
  
  http {
      server {
          set_by_lua_block $sp {
              return os.getenv("SERVER_PORT");
          }
          listen $sp;
          root /Users/kevin/code/nginx-testing;
      }
  }
  </code></pre>
  
  <p>But this doesn't work, either; <code>$sp</code> doesn't get defined until the <code>rewrite</code> phase. </p>
  
  <p>Are there any options here or am I resigned to rewriting the config file via <code>sed</code> or similar before starting nginx?</p>
  
  <p>Kevin</p>