Last active
April 14, 2021 08:54
-
-
Save jondkelley/0149b559da4200092d9d5c231b393e86 to your computer and use it in GitHub Desktop.
Make LUA redirect hosts
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
access_by_lua ' | |
proto = "http://" | |
proto1 = "https://" | |
temporary = 302 | |
permanent = 301 | |
if ngx.var.host == "example.com" then | |
new_host = "new_example.com" | |
return ngx.redirect(proto ..new_host .. ngx.var.request_uri, temporary) | |
end | |
'; |
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
access_by_lua ' | |
-- openresty sample config, see https://openresty.org/en/linux-packages.html | |
-- lua_package_path "/usr/local/openresty/nginx/lua.d/?.lua"; | |
-- lua_code_cache on; | |
-- | |
-- server { | |
-- listen 80; | |
-- server_name localhost; | |
-- location / { | |
-- default_type text/html; | |
-- include /usr/local/openresty/nginx/lua.d/custom_redirects.lua; | |
-- proxy_pass http://upstream; | |
-- } | |
default_scheme = "http://" | |
permanent = 301 | |
-- Can load table below as seperate file mappings.lua by calling require(".mappings") | |
map = { | |
-- customer example.com | |
-- ticket OPS-3929 | |
-- date 05/02/2001 | |
["example.com"] = "new_example.com", | |
["old.com"] = "new.com", | |
["bad.com"] = "good.com", | |
} | |
-- fastest method BUT UNTESTED!!!!! | |
if map[tostring(ngx.var.host)] then | |
return ngx.redirect(default_scheme .. map[tostring(ngx.var.host)] .. ngx.var.request_uri, permanent) | |
end | |
-- tested but slow | |
--for source, destination in pairs(map) do | |
-- if tostring(ngx.var.host) == source then | |
-- return ngx.redirect(default_scheme .. destination .. ngx.var.request_uri, permanent) | |
-- end | |
--end | |
'; |
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
--- snippet --- | |
lua_package_path "/usr/local/openresty/nginx/lua.d/?.lua"; | |
server { | |
listen 80; | |
server_name localhost; | |
location / { | |
default_type text/html; | |
include /usr/local/openresty/nginx/lua.d/custom_redirects.lua; | |
proxy_pass http://aol.com; | |
} | |
--- snippet -- |
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
access_by_lua ' | |
-- this is probably the least inefficient although coolest method | |
-- python like split function | |
-- https://stackoverflow.com/questions/1891473/how-to-load-text-file-into-sort-of-table-like-variable-in-lua | |
function string:split(sSeparator, nMax, bRegexp) | |
local aRecord = {} | |
if self:len() > 0 then | |
local bPlain = not bRegexp | |
nMax = nMax or -1 | |
local nField, nStart = 1, 1 | |
local nFirst,nLast = self:find(sSeparator, nStart, bPlain) | |
while nFirst and nMax ~= 0 do | |
aRecord[nField] = self:sub(nStart, nFirst-1) | |
nField = nField+1 | |
nStart = nLast+1 | |
nFirst,nLast = self:find(sSeparator, nStart, bPlain) | |
nMax = nMax-1 | |
end | |
aRecord[nField] = self:sub(nStart) | |
end | |
return aRecord | |
end | |
default_scheme = "http://" | |
temporary = 307 | |
local file = io.open("/mnt/redirects_ramdisk/redirect_map.txt", "r") | |
if file then | |
for line in file:lines() do | |
local source, dest = unpack(line:split(" ")) | |
if tostring(ngx.var.host) == source then | |
return ngx.redirect(default_scheme .. dest .. ngx.var.request_uri, temporary) | |
end | |
end | |
else | |
local file = io.open("/opt/redirect_map.txt", "r") | |
ngx.log(ngx.ERR, "RAMdisk has failed for domain mappings!") | |
for line in file:lines() do | |
local source, dest = unpack(line:split(" ")) | |
if tostring(ngx.var.host) == source then | |
return ngx.redirect(default_scheme .. dest .. ngx.var.request_uri, temporary) | |
end | |
end | |
end | |
'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment