MareCieco
MareCieco

Reputation: 157

nginx lua-resty-http no route to host error

I'm trying to make an http request using lua-resty-http. I created a simple get api in https://requestb.in

I can make a request using the address: https://requestb.in/snf2ltsn

However, when I try to do this in nginx I'm getting error no route to host

My nginx.conf file is:

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; #enables livereload for development
            content_by_lua_file ./lua/test.lua;
        }
    }
}

and my Lua code is

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",
        }
      })

How can I fix this Issue? Or is there any suggestion to make http request in nginx? any clue?

PS: There is a commented section in my Lua code. I also tried to make a request using that code but nothing happened.

Upvotes: 2

Views: 1889

Answers (2)

Gurcan
Gurcan

Reputation: 438

Change the package_path like:

lua_package_path "$prefix/resty_modules/lualib/?.lua;;";
lua_package_cpath "$prefix/resty_modules/lualib/?.so;;";

Upvotes: 1

Alexander Altshuler
Alexander Altshuler

Reputation: 3064

By default nginx resolver returns IPv4 and IPv6 addresses for given domain.

resty.http module uses cosocket API.

Cosocket's connect method called with domain name selects one random IP address You are not lucky and it selected IPv6 address. You can check it by looking into nginx error.log

Very likely IPv6 doesn't work on your box.

To disable IPv6 for nginx resolver use directive below within your location:

resolver 8.8.8.8 ipv6=off;

Upvotes: 1

Related Questions