Skip to content

Instantly share code, notes, and snippets.

@qya
Created December 11, 2023 08:29
Show Gist options
  • Save qya/bf565506ef80ba5bcb30dd81a849ed1c to your computer and use it in GitHub Desktop.
Save qya/bf565506ef80ba5bcb30dd81a849ed1c to your computer and use it in GitHub Desktop.
Nginx Proxy with JS
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