0

I have recently configured my Laravel website on AWS CDN and have encountered some issues. I am quite new here, kindly help..

My Laravel Website uses below cookies to identify the session. csrf_cookie_name, ci_session & site_lang

So, I have added these cookies and headers in my custom cookie rule under Behavior setting as shown below: enter image description here

Now, the entire website seems fine except some functionalities within dashboard post login. I see that recent changes does not reflect, cart item number don't update, recent transactions don't show, any live chat message dont appear and some similar issue. Post I logout and re-login, I can see all updates and then again new changes dont reflect.

I am suspecting that it is possibly happening due to caching of session IDs or session cookies. Can you please help here and let me know if this is the case.

Also, the process to just forward the above cookies without caching to the Viewer/User.

When I face this issue, every time I invalidate/purge the cache and for that moment, all new updates reflect on the dashboard, but again it stops reflecting the new changes unless I re-login or clear all cache.

To clear all cache, I create invalidation with /*

Please check screenshots of my current behavior settings. enter image description here

enter image description here

enter image description here

1 Answers1

0

You are caching your requests for dynamic content (ex. live chat).
And as you added the session id to the cache key (ci_session), you are caching these requests per user session.
Hence when you log in again you get a new session id, the first request is a cache miss and get fresh content, not the next ones.

The issue is that you have only one behaviour associated to all traffic (*), static and dynamic. This behaviour is configured to cache requests and add a number of headers and cookies to the cache key.

The origin request policy is configured to forward all cookies to the origin when there's a cache miss so no need to add ci_session to the caching policy.

And you need to differentiate requests for static and dynamic content.

You have 2 alternatives.

1/ Use different paths.
One for static content (ex. http://mywebsite/static).
One for dynamic content (ex. http://mywebsite/api).
Then create a distinct behaviour for each path.
Cache everything for the first one, use a standard caching policy.
Cache nothing for the second one.

2/ Use different domains.
You push your static content to an S3 bucket.
You create a CloudFront distribution on top of this bucket and cache everything, you will get a URL like http://12345.cloudfront.net.
You configure your application so when you connect to http://mywebsite it fetches static content from http://12345.cloudfront.net.
I'm not familiar with Laravel but it seems this thread is highlighting a solution: Laravel and AWS Cloudfront.

Hope it helps.

MarcC
  • 413
  • 3
  • 12
  • Just one point to add here, if I don’t add cookies list in cookie policy as shown in image, I can’t even login to dashboard. It throws error. Can you please elaborate what do you mean by current setup. Most laravel websites are set to work with these cookies. As you recommended, we should not cache cookies, so how to set this policy on AWS CDN ? – crazyonlineuser Aug 03 '23 at 20:08
  • Kindly elaborate your ask, I would be happy to share more info. This would be a great help – crazyonlineuser Aug 03 '23 at 20:09
  • Please could you describe the behaviour(s) you defined with your CloudFront distribution (associated path, policies, ...) and one of the HTTP request you do not want to cache so I can see the path of your API (ex. live chat GET). Indeed you need to forward your cookie(s) to your origin. – MarcC Aug 03 '23 at 20:19
  • I'm not familiar with Laravel, this thread seems to indicate that you can upload your static content in an S3 bucket, create a CloudFront distribution on top of this bucket, configure your Laravel application to say that your static content is available on CloudFront https://stackoverflow.com/questions/39392114/laravel-and-aws-cloudfront – MarcC Aug 03 '23 at 20:25
  • I have uploaded all screenshots, kindly check. Its not configured with S3, its a custom domain hosted on godaddy WHM server. AT this stage, I have managed to set all configurations, just stuck with these cookies. every time, I either have to re-login or invalidate. – crazyonlineuser Aug 03 '23 at 20:41
  • I updated my answer, hope it helps – MarcC Aug 03 '23 at 21:03
  • I checked quickly Laravel doc they recommend the option 2, a packaging module called Vite and an environment variable called ASSET_URL to use the CloudFront URL. Check https://laravel.com/docs/10.x/vite#custom-base-urls – MarcC Aug 04 '23 at 20:41
  • Hi MarcC, this is a great help to know these and understand the concept. I am however still stuck and trying to figure it other ways. Can you pls answer few point as below: 1. How in general we cache php files, I feel that php caching at edge location finds more latency to connect with DB. 2. With all my above CDN setting, I find that all users are logging to same account due to same session. 3. If I remove CI_Session, do you think that it might work fine. – crazyonlineuser Aug 05 '23 at 09:34
  • Usually you don't cache HTML produced with PHP files. It is what I call dynamic content. PHP files are scripts evaluated server side for every single request returning HTML embedding the result of database queries. What you can cache are CSS, PNG, JS files, what I call static content because they don't depend on the user request. You can remove ci_session from your caching policy it will help however will not solve the issue. – MarcC Aug 05 '23 at 09:48