Created
December 11, 2023 08:29
-
-
Save qya/bf565506ef80ba5bcb30dd81a849ed1c to your computer and use it in GitHub Desktop.
Nginx Proxy with JS
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
function handleRequest(r) { | |
var t = '/path/to/cookies.txt', | |
e = readCookiesFromFile(r, t); | |
r.headersOut.Cookie = e; | |
var a = buildTargetUrl(r, 'http://ex.com'), | |
o = createProxyRequest(r, a), | |
s = r.subrequest(o); | |
handleProxyResponse(r, s) | |
} | |
function readCookiesFromFile(r, t) { | |
var e = ''; | |
try { | |
e = r.file.read(t) | |
} catch (a) { | |
r.error('Error reading cookies file: ' + a.toString()) | |
} | |
return e.trim() | |
} | |
function buildTargetUrl(r, t) { | |
return (t + r.uri).replace(/http:\/\/ex\.com/g, 'http://yoursite.com') | |
} | |
function createProxyRequest(r, t) { | |
return { | |
method: r.method, | |
uri: t, | |
headers: r.headersIn, | |
body: r.requestBody, | |
subrequest: !0 | |
} | |
} | |
function handleProxyResponse(r, t) { | |
t.status == 200 ? r.return(200, t.responseText) : r.return(500, 'Internal Server Error') | |
} | |
export default { | |
handleRequest | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment