Last active
October 17, 2022 10:03
-
-
Save devld/40df2b3165371a22e2e7c2c39d9cc616 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
location /_/ { | |
set $location_prefix '/_/'; | |
set $target_uri ''; | |
set $target_host ''; | |
set $target_url ''; | |
access_by_lua_block { | |
local target_uri = ngx.var.request_uri:sub(ngx.var.location_prefix:len() + 1) | |
if (not target_uri:lower():match("^https?://.+")) then | |
ngx.header['Content-Type'] = 'text/html' | |
ngx.say('Invalid URL: ' .. target_uri) | |
ngx.exit(ngx.HTTP_BAD_REQUEST) | |
end | |
local host, url = target_uri:match("^(.....?://[^/]+)(.*)$") | |
if (url == '') then | |
url = '/' | |
end | |
ngx.var.target_host = host | |
ngx.var.target_url = url | |
ngx.var.target_uri = host .. url | |
} | |
proxy_ssl_server_name on; | |
proxy_pass $target_uri; | |
proxy_connect_timeout 5s; | |
header_filter_by_lua_block { | |
local r = ngx.header['Location'] | |
if (r and r ~= '') then | |
if (not r:lower():match("^https?://.+")) then | |
if (r:match("^/")) then | |
r = ngx.var.target_host .. r | |
else | |
local tu = ngx.var.target_url | |
if (not tu:match("/$")) then | |
tu = tu:sub(1, tu:find("/[^/]*$")) | |
end | |
r = ngx.var.target_host .. tu .. r | |
end | |
end | |
r = ngx.var.scheme .. '://' .. ngx.var.host .. ngx.var.location_prefix .. r | |
ngx.header['Location'] = r | |
end | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment