Skip to content

Instantly share code, notes, and snippets.

@suzumura-ss
Created June 21, 2024 13:30
Show Gist options
  • Save suzumura-ss/3a79594d3c109fc5c8d0a3c74becc2c0 to your computer and use it in GitHub Desktop.
Save suzumura-ss/3a79594d3c109fc5c8d0a3c74becc2c0 to your computer and use it in GitHub Desktop.
reverce proxy from WSL:1234 to Host:1234 with nameserver=192.168.24.10
# https://github.com/openresty/lua-resty-dns
lua_shared_dict ADDRESS 1m;
server {
listen 1234 default_server;
location / {
set $upstream "";
rewrite_by_lua_block {
local ADDRESS = ngx.shared.ADDRESS
local address = ADDRESS:get("address")
if not address then
local resolver = require "resty.dns.resolver"
local r, err = resolver:new{
nameservers = {"192.168.24.10"},
retrans = 5, -- 5 retransmissions on receive timeout
timeout = 2000, -- 2 sec
no_random = true, -- always start with first nameserver
}
if not r then
ngx.say("failed to instantiate the resolver: ", err)
return
end
local answers, err, tries = r:query("rumi.smoche.local", nil, {})
if not answers then
ngx.say("failed to query the DNS server: ", err)
ngx.say("retry historie:\n ", table.concat(tries, "\n "))
return
end
if answers.errcode then
ngx.say("server returned error code: ", answers.errcode, ": ", answers.errstr)
return
end
address = answers[1].address
ADDRESS:set("address", address)
end
ngx.var.upstream = address
}
proxy_pass http://$upstream:1234;
proxy_set_header Host $host;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment