If this is the first time you read this article, you can read this introductory article about openresty first: Overview of Openresty
There may be a need to access other services in openresty. We needed to regularly go to another service to pull some configuration information, and then change the configuration. Implementing this function requires the following steps:
- Write lua script files to implement http requests
- Add dependent library source files to the lualib directory
- Copy the lua code file to the path
- Modify the nginx.conf file and add the http request function
1. Write lua script file
We name the script file demo.lua, and the content is as follows:
local http = require "resty.http"
local json = require "cjson"
local httpc = http.new()
local res, err = httpc:request_uri("http://127.0.0.1:80/getmes",{
method = "GET",
headers = {
-- ["Authorization"] = "Basic YWRtaW46YWRtaW4=",
["Content-Type"] = "application/json",
},
})
if err ~= nil then
ngx.log(ngx.ERR, "httpc:request_uri err = ", err)
return ngx.exit(0)
end
if 200 ~= res.status then
ngx.exit(res.status)
end
--解析返回的数据
local jsondatanew = json.decode(res.body)["data"]
--得到数据进行其他操作
The above code only describes how to initiate http requests in Lua code. What needs special attention here is that http requests are best placed in the init process. Other processes are not suitable for initiating http requests because of response time or other problems. It is best not to let http requests participate in the logic of access requests. This demonstration is to add it to the init_worker_by_lua_file process and make http requests during the startup initialization phase of openresty.
2. Add dependent library files
If the resty.http module is missing during operation, you need to add several files to the path /usr/local/openresty/lualib/resty. Since the following three files exist, I packaged them into a compressed package. Go here to download: http Lua source code dependent library file download
3. Copy the lua code file
Copy the demo.lua file to the path /usr/local/openresty/nginx/lua/
4. Modify the nginx.conf file
Modify the nginx.conf file, introduce the http request code in the init_worker_by_lua_file process, and modify the content of the nginx.conf file as follows:
#user nobody;
worker_processes 1;
error_log logs/error.log error;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# access_log logs/access.log main;
# error_log logs/error.log error;
lua_package_path "/usr/local/openresty/lualib/?.lua;/usr/local/openresty/nginx/lua/?.lua;";
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
init_worker_by_lua_file lua/demo.lua;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
proxy_connect_timeout 3s;
#gzip on;
# HTTPS server
server {
listen 80;
server_name localhost;
location / {
#设置代理目的url变量
proxy_pass https://127.0.0.1;
}
}
}
lua_package_path "/usr/local/openresty/lualib/?.lua;/usr/local/openresty/nginx/lua/?.lua;";
and
init_worker_by_lua_file lua/demo.lua;
The first sentence is to add the path of the lua code. The second sentence of code is to specify the initialization process init_worker_by_lua_file, which will enter the demo.lua file to perform the http request operation. In my code, the body data is currently only obtained through the http request and converted into json format. , the data is not currently used, you can use the data to implement the functions you want.