Skip to content

Instantly share code, notes, and snippets.

@kotopes
Last active December 19, 2015 13:39
Show Gist options
  • Save kotopes/5963750 to your computer and use it in GitHub Desktop.
Save kotopes/5963750 to your computer and use it in GitHub Desktop.
Nginx + LUA Proxy to url with Referer changes. Uses: socket.url lua lib and LUA compiled in nginx. Lib get from: http://files.luaforge.net/releases/luasocket/luasocket/luasocket-2.0.2/luasocket-2.0.2.tar.gz src\url.lua and place into /usr/local/share/lua/5.1/socket/url.lua Use: http://site.com/aaa/?url=aHR0cDovL21lZGlhLmtpbm8tZ292bm8uY29tL21vdml…
location /aaa/ {
# internal;
set $upstream "";
set $referer "";
set $ref $arg_ref;
set $url $arg_url;
set $tohost "";
set $newuri "";
# add_header Content-Type text/plain;
rewrite_by_lua '
local url = require("socket.url")
local urln = ngx.var.url
if ngx.var.arg_ref == nil then
-- ngx.say( " Ref is not set or null" )
-- ngx.exit(200)
ngx.exit(404)
else
ngx.var.referer = ngx.decode_base64(ngx.unescape_uri( ngx.var.ref ))
end
if ngx.var.arg_url == nil then
-- ngx.say( " Url is not set or null" )
-- ngx.exit(200)
ngx.exit(404)
end
local urln = ngx.var.url
parsed_url = url.parse( ngx.decode_base64(ngx.unescape_uri( ngx.var.url )) )
if parsed_url.authority == nil then
-- ngx.say( "Is null parsed url" )
-- ngx.say( " decoded: " .. ngx.decode_base64(ngx.unescape_uri( ngx.var.url )))
-- ngx.say( " arg_url: " .. urln )
-- ngx.exit(200)
ngx.exit(404)
else
ngx.var.upstream = parsed_url.authority
ngx.var.tohost = parsed_url.host
ngx.var.newuri = parsed_url.path
-- ngx.say( "Upstream: " .. ngx.var.upstream )
-- ngx.say( "Host: " .. ngx.var.tohost )
-- ngx.say( "Referer: " .. ngx.var.referer )
-- ngx.say( "Uri: " .. ngx.var.newuri )
-- if parsed_url.query == nil then
-- ngx.say(" Args: not set")
-- unset(ngx.var.args)
-- else
-- ngx.var.args = parsed_url.query
-- ngx.say( "Args: " .. parsed_url.query )
-- end
end
';
proxy_buffering off;
proxy_set_header Host $tohost;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer $referer;
proxy_redirect off;
proxy_connect_timeout 10;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://$upstream/$newuri;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment