title: proxy_pass request base on the result of another internal request via lua-nginx
body: <p>I want to proxy_pass a request or return a response base on the result of an internal service.
  to accomplish this purpose I'm using <a href="https://hub.docker.com/r/openresty/openresty/" rel="nofollow noreferrer">openresty</a> docker image</p>
  <p>Here is my <code>nginx.conf</code>:</p>
  <pre><code># nginx.vh.default.conf  --  docker-openresty
  #
  # This file is installed to:
  #   `/etc/nginx/conf.d/default.conf`
  #
  # It tracks the `server` section of the upstream OpenResty's `nginx.conf`.
  #
  # This config (and any other configs in `etc/nginx/conf.d/`) is loaded by
  # default by the `include` directive in `/usr/local/openresty/nginx/conf/nginx.conf`.
  #
  # See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
  #
  # load_module /usr/lib/nginx/modules/ndk_http_module.so;
  # load_module /usr/lib/nginx/modules/ngx_http_lua_module.so;
  lua_package_path '/usr/local/nginx/includes.d/?.lua;/home/user/lua/?.lua;/home/lua/?.lua;;';
  
  error_log  logs/error.log  info;
  error_log  logs/error.log  debug;
  
  upstream flask_webservice {
      server challenge:5000;
  }
  
  server {
      listen 80;
      server_name  localhost;
      lua_need_request_body on;
      #charset koi8-r;
      #access_log  /var/log/nginx/host.access.log  main;
  
      location / {
          # default_type text/plain;
          # proxy_pass http://challenge:5000/;
          content_by_lua_file /home/user/lua/proxy_middleware.lua;
          # content_by_lua_block {}
      }
  
      #error_page  404              /404.html;
  
      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/local/openresty/nginx/html;
      }
  
      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #    proxy_pass   http://127.0.0.1;
      #}
  
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #    root           /usr/local/openresty/nginx/html;
      #    fastcgi_pass   127.0.0.1:9000;
      #    fastcgi_index  index.php;
      #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
      #    include        fastcgi_params;
      #}
  
      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    deny  all;
      #}
  }
  </code></pre>
  <p>Here is <code>proxy_middleware.lua</code>:</p>
  <pre class="lang-lua prettyprint-override"><code>local http = require &quot;resty.http&quot;
  local cjson = require &quot;cjson&quot;
  
  local httpc = http.new()
  
  local challenge_url = &quot;http://&quot; .. ngx.var.flask_webservice .. ngx.var.uri
  local request_header = ngx.req.get_headers()
  local request_method = ngx.req.get_method()
  local request_body = ngx.req.get_body_data()
  
  if request_body == nil then
      request_body = {}
  end
  
  if request_header == nil then
      request_header = {}
  end
  
  ngx.log(ngx.DEBUG, challenge_url)
  ngx.log(ngx.DEBUG, &quot;request_header: &quot; .. cjson.encode(request_header))
  ngx.log(ngx.DEBUG, &quot;request_method: &quot; .. request_method)
  ngx.log(ngx.DEBUG, &quot;request_body: &quot; .. cjson.encode(request_body))
  
  local res, err = httpc:request_uri(challenge_url, {
      method = request_method,
      headers = request_header,
      body = request_body
  })
  
  ngx.log(ngx.DEBUG, &quot;response is:&quot; .. cjson.encode(res))
  ngx.log(ngx.DEBUG, &quot;error is:&quot; .. cjson.encode(err))
  </code></pre>
  <p>here is the error that I have got:</p>
  <pre><code>2020/12/24 19:17:52 [error] 7#7: *3 lua entry thread aborted: runtime error: /home/user/lua/proxy_middleware.lua:6: attempt to concatenate field 'flask_webservice' (a nil value)
  </code></pre>
  <p>here is the project on <a href="https://github.com/mohammadmasoumi/object_challenge" rel="nofollow noreferrer">the Github repo</a></p>
  <p>I have two questions:</p>
  <ol>
  <li>Is there another way to accomplish this purpose?</li>
  <li>How can I access the upstream server in the <code>Lua</code> code?</li>
  </ol>