我现在的问题是我需要将PHPSESSID cookie传递给我的来源以获得正确的缓存响应,但我不想将缓存基于PHPSESSID的值.我只需要我的缓存响应基于X-User-Context-Hash的值而不是我的会话cookie.
下面的图片应该详细解释我的问题
有没有可能实现这一目标?
非常感谢任何帮助.
这是我的Lambda @ Edge VIEwer请求触发器:
'use strict';function parsecookies(headers) { const parsedcookie = {}; if (headers.cookie) { console.log(`${headers.cookie[0].value}`); headers.cookie[0].value.split(';').forEach((cookie) => { if (cookie) { const parts = cookie.split('='); parsedcookie[parts[0].trim()] = parts[1].trim(); } }); } return parsedcookie;}exports.handler = (event,context,callback) => { const request = event.Records[0].cf.request; const headers = request.headers; const https = require('https'); // Read session cookie const parsedcookies = parsecookies(headers); let cookie = ''; if (parsedcookies) { if(parsedcookies['PHPSESSID']) { cookie = `PHPSESSID=${parsedcookies['PHPSESSID']}`; } } console.log(`cookie: ${cookie}`); // Send request to origin host at /_fos_user_context_hash // passing the original session cookie const options = { hostname: `${request.headers.host[0].value}`,port: 443,path: '/_fos_user_context_hash',method: 'head',headers: { 'cookie': cookie,'Accept': 'application/vnd.fos.user-context-hash','vary' : 'cookie' } }; const req = https.request(options,(res) => { console.log('statusCode:',res.statusCode); console.log('headers:',res.headers); // Read the X-User-Context-Hash from the hash endpoint const headername = 'X-User-Context-Hash'; let hash = 'anonymous'; if (res.headers[headername.tolowerCase()]) { hash = res.headers[headername.tolowerCase()]; } // Append X-User-Context-Hash before passing request on to CF request.headers[headername.tolowerCase()] = [{ key: headername,value: hash }]; callback(null,request); }).on('error',(e) => { console.error(e); // Forward request anyway callback(null,request); }); req.end();};解决方法 这是我最终解决了我的问题:
CloudFront行为
我将行为配置为不将任何cookie转发到源,但仅基于头部主机和X-User-Context-Hash进行缓存(参见屏幕截图).
下图解释了我的lambda @ edge过程:
>在“查看器请求”触发器中,我读取了名为PHPSESSID和REMEMBERME的基于用户的cookie,并通过X-Session-cookies标头传递这些值.
>如果我的请求URL和给定的主机以及X-User-Context-Hash标头匹配,则Cloud-Front将返回缓存的项目并在此处停止.
>如果没有匹配,则触发“Origin Request”触发器.当该事件触发自定义标头时,X-Session-cookies可用.因此,我从X-Session-cookies标头中获取值,并将request.headers.cookie的值设置为该值.此步骤确保PHPSESSID和REMEMBERME cookie在页面缓存之前都传递到源.
我的Lambda @ Edge功能:
查看者请求触发器:
'use strict';function parsecookies(headers) { const parsedcookie = {}; if (headers.cookie) { console.log(`${headers.cookie[0].value}`); headers.cookie[0].value.split(';').forEach((cookie) => { if (cookie) { const parts = cookie.split('='); parsedcookie[parts[0].trim()] = parts[1].trim(); } }); } return parsedcookie;}exports.handler = (event,callback) => { const request = event.Records[0].cf.request; const headers = request.headers; const https = require('https'); let sessionID = ''; // Read session cookie const parsedcookies = parsecookies(headers); let cookie = ''; if (parsedcookies) { if(parsedcookies['PHPSESSID']) { cookie = `PHPSESSID=${parsedcookies['PHPSESSID']}`; } if(parsedcookies['REMEMBERME']) { if (cookie.length > 0) { cookie += ';'; } cookie += `REMEMBERME=${parsedcookies['REMEMBERME']}`; } } console.log(`cookie: ${cookie}`); // Send request to origin host at /_fos_user_context_hash // passing the original session cookie const options = { hostname: `${request.headers.host[0].value}`,value: hash }]; const sessionheadername = 'X-Session-cookies'; request.headers[sessionheadername.tolowerCase()] = [{ key: sessionheadername,value: cookie }]; callback(null,request); }); req.end();};
原始请求触发器:
exports.handler = (event,callback) => { const request = event.Records[0].cf.request; const sessionheadername = 'X-Session-cookies'; let cookie = ''; if (request.headers[sessionheadername.tolowerCase()]) { console.log(request.headers[sessionheadername.tolowerCase()]); cookie = request.headers[sessionheadername.tolowerCase()][0].value; } request.headers.cookie = [{ key : 'cookie',value : cookie }]; callback(null,request);};总结
以上是内存溢出为你收集整理的symfony – 将cookie传递给CloudFront源但阻止缓存全部内容,希望文章能够帮你解决symfony – 将cookie传递给CloudFront源但阻止缓存所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)