nginx lua-resty-http no route to host error

我正在尝试使用lua-resty-http进行http请求。 我在[https://requestb.in](https://requestb.in)上创建了一个简单的get api

我可以使用地址进行请求:[https://requestb.in/snf2ltsn](https://requestb.in/snf2ltsn)

但是,当我在nginx中尝试做这个时,我正在收到无法到达主机的错误

我的nginx.conf文件是:

worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
    lua_package_path"$prefix/lua/?.lua;;";
    server {
        listen 8080;
        location / {
            resolver 8.8.8.8;
            default_type text/html;
            lua_code_cache off; #用于开发启用实时重新加载
            content_by_lua_file ./lua/test.lua;
        }
    }
}

我的Lua代码是

local http=require"resty.http"
local httpc=http.new()

 - -local res,err=httpc:request_uri("https://requestb.in/snf2ltsn",{ssl_verify=false,method="GET"})

      local res,err=httpc:request_uri("https://requestb.in/snf2ltsn",{
        method="GET",
        headers={
          ["Content-Type"]="application/x-www-form-urlencoded",
        }
      })

我该怎么解决这个问题? 或者有没有建议在nginx中进行http请求? 有什么线索吗?

PS:在我的Lua代码中有一个注释的部分。 我也试图使用该代码进行请求,但什么都没有发生。

点赞
用户2060502
用户2060502

默认情况下,nginx解析器返回给定域的IPv4和IPv6地址。

resty.http模块使用cosocket API。

调用带有域名的cosocket的连接方法会选择一个随机的IP地址,你不幸选中了IPv6地址。你可以通过查看nginx error.log来检查它。

很可能你的电脑上IPv6不起作用。

要禁用nginx解析器的IPv6,请在你的位置(location)中使用以下指令:

resolver 8.8.8.8 ipv6=off;
2017-10-15 17:18:11
用户4327598
用户4327598

将 package_path 更改为:

lua_package_path "$prefix/resty_modules/lualib/?.lua;;";
lua_package_cpath "$prefix/resty_modules/lualib/?.so;;";
2018-01-16 05:05:00