本文介绍了Openresty:使用lua进行http调用并返回其解析结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用openresty构建一个简单的服务器.

I am using openresty to build a simple server.

调用此服务器后,它应再次调用另一台服务器,获取JSON结果,对其进行处理并返回解析后的结果.

Upon calling this server, it should make another call to a different server, fetch a JSON result, process it and return the parsed result.

出于这个问题,服务器应该在openresty中实现.

The server should be implemented in openresty for reasons beyond the scope if this question.

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 [error] 7#7: *1 open() "/usr/local/openresty/nginx/htmlhttp://localhost:8080/functions.json" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET / HTTP/1.1", subrequest: "http://localhost:8080/functions.json", host: "localhost:8080"

如何在nginx openresty的Lua内容块内发出HTTP GET请求?

How can I make an HTTP GET request from within a Lua content block in nginx openresty?

推荐答案

Capture将允许您捕获内部Nginx位置,而不是绝对URL

Capture will allow you to capture internal nginx locations and not absolute urls

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;
    }
}

这篇关于Openresty:使用lua进行http调用并返回其解析结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 09:31