我的问题
我正在使用openresty来构建一个简单的服务器.
在调用此服务器时,它应该再次调用另一个服务器,获取JSON结果,处理它并返回解析后的结果.
如果这个问题,服务器应该在openresty中实现,超出范围的原因.
码
error_log /dev/stdout info;
events {
worker_connections 14096;
}
http {
access_log off;
lua_package_path ";;/usr/local/openresty/nginx/?.lua;";
server {
keepalive_requests 100000;
proxy_http_version 1.1;
keepalive_timeout 10;
location / {
content_by_lua_block {
res = ngx.location.capture('http://localhost:8080/functions.json')
ngx.say(res.body)
}
}
location /functions {
root /usr/local/openresty/nginx/html/;
}
listen 0.0.0.0:80 default_server;
}
}
错误日志
2017/09/11 08:27:49 [错误] 7#7:* 1打开()“/usr/local/openresty/nginx/htmlhttp://localhost:8080/functions.json”失败(2:没有这样的文件或目录),客户端:172.17.0.1,服务器:,请求:“GET / HTTP / 1.1”,子请求:“http:// localhost:8080 / functions.json”,主机:“localhost:8080”
我的问题
如何在nginx openresty中的Lua内容块中发出HTTP GET请求?
解决方法:
Capture将允许您捕获内部nginx位置而不是绝对URL
error_log /dev/stdout info;
events {
worker_connections 14096;
}
http {
access_log off;
lua_package_path ";;/usr/local/openresty/nginx/?.lua;";
server {
keepalive_requests 100000;
proxy_http_version 1.1;
keepalive_timeout 10;
location / {
content_by_lua_block {
res = ngx.location.capture('/functions.json')
ngx.say(res.body)
}
}
location /functions.json {
proxy_pass http://localhost:8080/functions.json;
}
location /functions {
root /usr/local/openresty/nginx/html/;
}
listen 0.0.0.0:80 default_server;
}
}