Nginx는 내부적으로 다른 요청을 호출하고 해당 요청 데이터를 반환합니다.

Nginx는 내부적으로 다른 요청을 호출하고 해당 요청 데이터를 반환합니다.

나머지 API를 구문 분석하기 위해 nginx와 lua를 사용하고 있습니다(https://openresty-reference.readthedocs.io/en/latest/Lua_Nginx_API/) . 내부적으로 다른 서버인 나머지 호출 내에서 다른 API를 호출하고 해당 응답을 반환하려고 합니다. 하지만 항상 빈 응답을 받고 있습니다. 다음은 내 구성입니다. /student에 대한 도움을 요청합니다. 매우 감사합니다.

location /student {
    content_by_lua '
    local str = "test"
    local res = ngx.location.capture("/detail",{method = ngx.HTTP_POST, body = str})
    ngx.say(str)
    ';
}
location /detail {
    set $target '104.28.17.1/post';
    set $pass 'Basic Y2l28=';
    lua_need_request_body on;
    content_by_lua '
    proxy_set_header          Host            $host;
    proxy_set_header          X-Real-IP       $remote_addr;
    proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;
    #access_by_lua '
    proxy_set_header Authorization $pass;
    proxy_set_header Content-Type 'application/x-www-form-urlencoded';
    proxy_set_header Accept '*/*';
    proxy_set_header Connection 'keep-alive';
    proxy_pass http://$target;
}

답변1

Resty.http를 사용하여 Lua에서 호출을 수행하고 이를 본문에 쓸 수 있습니다. API에 대한 인증을 통한 https 호출은 다음과 같습니다.

-- load requirements 
http = require "resty.http"

local function api_request(ip, api_host, api_hostname, api_cred, api_url)
  local httpc  = http.new()
  httpc:set_timeout(500)
  path = api_host .. string.format(api_url, ip)
  local response, err = httpc:request_uri(path,{
      method = "GET",
      headers = {
          ["Authorization"] ="Basic " .. api_cred,
          ["Host"] = api_hostname,
      },
      ssl_verify = false,
  })
  return response
end

관련 정보