This article is transferred from https://blog.csdn.net/jinnianshilongnian/article/details/84703441 Thank you for sharing!
Http Client
OpenResty does not provide the Http client by default, so it needs to be provided by a third party; of course, we can do it through ngx.location.capture, but there are some limitations, which we will introduce later.
We can search the corresponding client from github, such as https://github.com/pintsized/lua-resty-http.
lua-resty-http
1. Download lua-resty-http client to lualib
cd /usr/example/lualib/resty/ wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua
2,test_http_1.lua
local http = require("resty.http") --Establish http Client instance local httpc = http.new() local resp, err = httpc:request_uri("http://s.taobao.com", { method = "GET", path = "/search?q=hello", headers = { ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36" } }) if not resp then ngx.say("request error :", err) return end --Get the status code ngx.status = resp.status --Get the response header for k, v in pairs(resp.headers) do if k ~= "Transfer-Encoding" and k ~= "Connection" then ngx.header[k] = v end end --Response volume ngx.say(resp.body) httpc:close()
Transfer-Encoding and Connection in the response header can be ignored because this data is currently output by the server.
3. example.conf configuration file
location /lua_http_1 { default_type 'text/html'; lua_code_cache on; content_by_lua_file /usr/example/lua/test_http_1.lua; }
4. Add the following instructions to the http section of nginx.conf for DNS parsing
resolver 8.8.8.8;
Remember to configure DNS resolver 8.8.8.8, otherwise the domain name cannot be resolved.
5. Visit http://192.168.1.2/lua_http_1 to see Taobao's search interface.
It's easy to use, such as timeout and connection pool settings, which are no longer described as previous Redis clients. Refer to more client usage rules https://github.com/pintsized/lua-resty-http.
ngx.location.capture
ngx.location.capture can also be used to complete http requests, but it can only request the path relative to the current nginx server, can not use the absolute path before access, but we can cooperate with nginx upstream to achieve the desired function.
1. Add the following upstream configuration to the http section of nginx.cong
upstream backend { server s.taobao.com; keepalive 100; }
That is, we will request upstream to backend; remember to add the DNS parser before.
2. Configure the following location in example.conf
location ~ /proxy/(.*) { internal; proxy_pass http://backend/$1$is_args$args; }
Internal means only internal access, that is, external access cannot be accessed through url; and requests are forwarded to upstream through proxy_pass.
3,test_http_2.lua
local resp = ngx.location.capture("/proxy/search", { method = ngx.HTTP_GET, args = {q = "hello"} }) if not resp then ngx.say("request error :", err) return end ngx.log(ngx.ERR, tostring(resp.status)) --Get the status code ngx.status = resp.status --Get the response header for k, v in pairs(resp.header) do if k ~= "Transfer-Encoding" and k ~= "Connection" then ngx.header[k] = v end end --Response volume if resp.body then ngx.say(resp.body) end
adopt ngx.location.capture Send a sub-request, where all request headers inherit from the current request because they are sub-requests, and whether ngx.ctx and ngx.var inherit can be referred to official documents. http://wiki.nginx.org/HttpLuaModule#ngx.location.capture . It also provides ngx.location.capture_multi for concurrent multiple requests, so that the overall response time is the slowest and is useful for batch calls.
4. example.conf configuration file
location /lua_http_2 { default_type 'text/html'; lua_code_cache on; content_by_lua_file /usr/example/lua/test_http_2.lua; }
5. If you visit http://192.168.1.2/lua_http_2 for testing, you can see the Taobao search interface.
Through upstream+ngx.location.capture, we get better performance and upstream connection pool, load balancing, failover, proxy cache and other features.
However, because inheritance is in the request header of the current request, there may be some problems. The most common problem is gzip compression. ngx.location.capture will not decompress the GZIP content of the back-end server. The solution can be referred to. https://github.com/openresty/lua-nginx-module/issues/12 Because most of our http calls are internal services, we can add proxy_pass_request_headers off to proxy location without passing the request header.