Set Cookies for Reverse Proxy Server with Nginx/Njs
I do want to pass explicit JSON cookies to Backend server with Nginx Javascript module. but i am kind of confused. Please if you can help me with ASAP it can save me from getting fired from job
Hi @Minomine61 ,
I don't understand your question very well, However, You can add/inject cookies in your requests through the Nginx Reverse Proxy, by implementing your code using Njs module ex:
location /api {
js_content jsCode.addingCookiesMethod;
}
location /api_proxy {
subrequest_output_buffer_size 64k;
rewrite /api_proxy/(.*) /$1 break;
proxy_pass https://Your-Backend-Server;
}
To implement this jsCode.addingCookieMethod you have to visit the ngx_http_js_module Documentations and this Repo ReadMe file.
My Best Wishes.
I am little bit confused with adding cookies method, actually i am new with JS. I have been working with python but now JS is new for me. do you have the correct code to set Explicit Json cookies in Javascript module https://cookie-editor.cgagnier.ca/ I am using this exension to Export cookies in a really good json format For example these are the cookies of my dummy github account [ { "domain": "github.com", "expirationDate": 1654742710.078902, "hostOnly": true, "httpOnly": true, "name": "__Host-user_session_same_site", "path": "/", "sameSite": "strict", "secure": true, "session": false, "storeId": null, "value": "bfc1GLQWgrqC785Ie70bB-36LbLl0YT0P6_VIGAFcL2DO4Rs" }, { "domain": ".github.com", "expirationDate": 1685069110.078959, "hostOnly": false, "httpOnly": true, "name": "dotcom_user", "path": "/", "sameSite": "lax", "secure": true, "session": false, "storeId": null, "value": "Haniyecoop" }, { "domain": "github.com", "expirationDate": 1653536786.275563, "hostOnly": true, "httpOnly": true, "name": "has_recent_activity", "path": "/", "sameSite": "lax", "secure": true, "session": false, "storeId": null, "value": "1" }, { "domain": "github.com", "expirationDate": 1685069110.078844, "hostOnly": true, "httpOnly": true, "name": "_device_id", "path": "/", "sameSite": "lax", "secure": true, "session": false, "storeId": null, "value": "f2bdae8d6290acdafa683732b6df6910" }, { "domain": "github.com", "hostOnly": true, "httpOnly": true, "name": "_gh_sess", "path": "/", "sameSite": "lax", "secure": true, "session": true, "storeId": null, "value": "Mh9%2FjvXp773uDBGTt%2BiVGgfMgQvH78GRB1bHtIGqQ6ie5zjrqyylGtMGh%2BN46jL%2B6sLaLu0fqnKbSjsgqJd1iB954X4NpUwM2lJ4EOXp8jHzqdQLhBpb2mifFNIuT1ntMBmMHP3iSJGZCaUuk9EV%2B1qUwua817zKPRh7OJFolAWIEsKkufUhIkNBcBieKWBF7TWD3%2BIEBVc9P6EmRNg%2FQUmGO%2B7OtA5cW7a2%2FONUWoQn%2FJGjN%2FTIWRpI%2BnvwNjAk%2BAm4BUTt06Lyf7WIRdmVINsYmTQKdvq%2BA%2FEjRs9ro2e2p1wV4Wy5nKlh7z%2BMQbFDlDwOeZ4uISsQdn5nrfmlyXpXM6It1UU6kJVnMRdr8xB7DHhqVVGN5l6Ke%2BxCA7YW5mQgje1cG52l6tjB8Kk9WZzQBKzyF5j9YC1X3wkvCVON40U7GFYoDuQHbE6nSC%2BewmuM%2FiHUQczpc4w9aAjgdKCx9Z6nG15gncBs0gOfKibtKUHvAP%2BpLg5oFwvEW7lcHkL%2BlO4lY2GL9O9EdVeO%2FT%2FDhuQ%3D--HMELfEDfJ9XlsKAD--zz3sd6JjcnZZF12gWF0wnw%3D%3D" }, { "domain": ".github.com", "expirationDate": 1685069060.019388, "hostOnly": false, "httpOnly": false, "name": "_octo", "path": "/", "sameSite": "lax", "secure": true, "session": false, "storeId": null, "value": "GH1.1.1466848117.1653533051" }, { "domain": ".github.com", "hostOnly": false, "httpOnly": true, "name": "color_mode", "path": "/", "sameSite": "lax", "secure": true, "session": true, "storeId": null, "value": "%7B%22color_mode%22%3A%22auto%22%2C%22light_theme%22%3A%7B%22name%22%3A%22light%22%2C%22color_mode%22%3A%22light%22%7D%2C%22dark_theme%22%3A%7B%22name%22%3A%22dark%22%2C%22color_mode%22%3A%22dark%22%7D%7D" }, { "domain": ".github.com", "expirationDate": 1685069110.078943, "hostOnly": false, "httpOnly": true, "name": "logged_in", "path": "/", "sameSite": "lax", "secure": true, "session": false, "storeId": null, "value": "yes" }, { "domain": ".github.com", "hostOnly": false, "httpOnly": false, "name": "tz", "path": "/", "sameSite": "lax", "secure": true, "session": true, "storeId": null, "value": "UTC" }, { "domain": "github.com", "hostOnly": true, "httpOnly": true, "name": "tz", "path": "/", "sameSite": "lax", "secure": true, "session": true, "storeId": null, "value": "UTC" }, { "domain": "github.com", "expirationDate": 1654742710.078889, "hostOnly": true, "httpOnly": true, "name": "user_session", "path": "/", "sameSite": "lax", "secure": true, "session": false, "storeId": null, "value": "bfc1GLQWgrqC785Ie70bB-36LbLl0YT0P6_VIGAFcL2DO4Rs" } ] how should i write the Js code for setting up cookies and then could you please ellaborate how should i need to write to inject cookies I know it's getting long, but please if you can help me i would be highly obliged to you.
how should i write the Js code for setting up cookies
converting this array to Set-Cookie header by javascript is very easy, it could be like:
function addingCookiesMethod (r){
cookies = [];
jsonCookies.forEach(obj=>{
const name = obj.name;
const value = obj.value;
//and so on .. in a regular js code you can do this by one line
// const {name,value,domain,.....} = obj; // but I think its not supported yet in Njs module.
cookies.push(`${name}=${value}; ${httpOnly ? 'httpOnly' : ''}; ........`);
}
// for this Q: could you please ellaborate how should i need to write to inject
//do the next line
r.headersOut["Set-Cookie"] = cookies;
//do the remaining code/logic......
}
)