openresty 访问外部接口http或https

  Seves

原文链接:openresty 访问外部接口http或https

openresty 访问外部接口http

local http = require “resty.http”
local httpc = http.new()
local res, err = httpc:request_uri(“http://example.com/helloworld”, {
method = “POST”,
body = “a=1&b=2”,
headers = {
[“Content-Type”] = “application/x-www-form-urlencoded”,
}
})

openresty 访问外部接口https,不需要证书

local http = require “resty.http”
local httpc = http.new()
local res, err = httpc:request_uri(“https://example.com:443/helloworld”, {
method = “POST”,
body = “a=1&b=2”,
headers = {
[“Content-Type”] = “application/x-www-form-urlencoded”,
},
ssl_verify = true(or false)
})

openresty 访问外部接口https,需要证书

需要证书访问的时候,要使用upstream和Subrequest结合访问

location /my_upstream/ {
internal; # Specifies that a given location can only be used for internal requests
proxy_pass_request_headers off;
proxy_set_header Host backend.example.com;
proxy_set_header Content-Type “application/json”;
proxy_set_header Accept “application/json”;
proxy_set_header Authorization “Bearer $bearerToken”
proxy_pass https://backend.example.com/; # trailing slash
proxy_ssl_certificate /etc/nginx/client.pem;
proxy_ssl_certificate_key /etc/nginx/client.key
proxy_ssl_verify on;
proxy_ssl_verify_depth 2; #just example
proxy_ssl_trusted_certificate /etc/nginx/ca.pem;
}

local res = ngx.location.capture(‘/my_upstream/login’,
{
method = ngx.HTTP_POST,
body = “some text”,
vars = {
bearerToken = “12345”
}
})


本文链接:https://www.fcblog.top/openresty-with-http-or-https-03cfcfc1

fc
1,330