1 00:00:00,120 --> 00:00:02,280 ‫So now let's talk about cache behaviors. 2 00:00:02,280 --> 00:00:04,830 ‫The idea is that you may want to have different origin, 3 00:00:04,830 --> 00:00:08,100 ‫different caches for different URL path patterns. 4 00:00:08,100 --> 00:00:11,160 ‫For example, you want to have one specific cache behavior 5 00:00:11,160 --> 00:00:15,180 ‫for all your JPEG images based on your origin web server. 6 00:00:15,180 --> 00:00:17,220 ‫Or you may want to route different origins 7 00:00:17,220 --> 00:00:19,800 ‫or origin groups based on the content type 8 00:00:19,800 --> 00:00:20,640 ‫or the path pattern. 9 00:00:20,640 --> 00:00:24,143 ‫For example, you're saying, hey, for /images/* 10 00:00:24,143 --> 00:00:28,200 ‫just go to S3 for /api/*, go to my origin 11 00:00:28,200 --> 00:00:31,200 ‫and for /* then go to my default origin. 12 00:00:31,200 --> 00:00:34,290 ‫It's called also the default cache behavior. 13 00:00:34,290 --> 00:00:36,060 ‫So in this example of CloudFront, 14 00:00:36,060 --> 00:00:37,800 ‫we have two cache behaviors. 15 00:00:37,800 --> 00:00:40,627 ‫The first one is /api/* 16 00:00:40,627 --> 00:00:45,480 ‫which is sending us to our application load balancer origin. 17 00:00:45,480 --> 00:00:49,680 ‫And then we have the /* that is the default cache behavior 18 00:00:49,680 --> 00:00:51,600 ‫that is always /*. 19 00:00:51,600 --> 00:00:53,280 ‫And you can redirect, for example, 20 00:00:53,280 --> 00:00:55,020 ‫to an S3 bucket as an origin. 21 00:00:55,020 --> 00:00:58,800 ‫And so the users based on the resource they will get out 22 00:00:58,800 --> 00:01:01,050 ‫of CloudFront may be redirected 23 00:01:01,050 --> 00:01:05,520 ‫into the first cache behavior or the second cache behavior. 24 00:01:05,520 --> 00:01:07,950 ‫Now, when you add additional cache behaviors, 25 00:01:07,950 --> 00:01:11,220 ‫the default cache behavior, which is always /* 26 00:01:11,220 --> 00:01:14,040 ‫is always also going to be the last to be processed. 27 00:01:14,040 --> 00:01:15,660 ‫So we're going to see whether or not 28 00:01:15,660 --> 00:01:18,420 ‫there is a more specific match, and if not, 29 00:01:18,420 --> 00:01:21,930 ‫then we revert back to the default cache behavior. 30 00:01:21,930 --> 00:01:23,370 ‫So a use case for this, for example, 31 00:01:23,370 --> 00:01:25,320 ‫would be how to gate access 32 00:01:25,320 --> 00:01:28,080 ‫into an S3 bucket because we want to make sure 33 00:01:28,080 --> 00:01:31,410 ‫that users are properly signed in through a sign in page. 34 00:01:31,410 --> 00:01:33,810 ‫So the way we do it is that we define a cache behavior 35 00:01:33,810 --> 00:01:38,190 ‫for /login and so the users who hit the /login page 36 00:01:38,190 --> 00:01:40,950 ‫will be redirected to our EC2 instance. 37 00:01:40,950 --> 00:01:42,910 ‫And the role of our EC2 instance 38 00:01:42,910 --> 00:01:46,350 ‫is to generate CloudFront signed cookies. 39 00:01:46,350 --> 00:01:49,530 ‫So these signed cookies are sent back to the user 40 00:01:49,530 --> 00:01:51,810 ‫and the user will then use the signed cookies 41 00:01:51,810 --> 00:01:54,690 ‫to be able to access our default cache behavior, 42 00:01:54,690 --> 00:01:58,590 ‫which is any other URL, then /login 43 00:01:58,590 --> 00:02:01,170 ‫and then access our S3 bucket files. 44 00:02:01,170 --> 00:02:03,463 ‫And if the users are trying to access 45 00:02:03,463 --> 00:02:05,400 ‫the default cache behavior 46 00:02:05,400 --> 00:02:07,740 ‫without doing a login first, what we can do 47 00:02:07,740 --> 00:02:09,390 ‫is that we can set up the cache behavior 48 00:02:09,390 --> 00:02:13,500 ‫to only accept the request if signed cookies are present. 49 00:02:13,500 --> 00:02:14,940 ‫Therefore, we can redirect 50 00:02:14,940 --> 00:02:17,820 ‫to the /login page and we're good to go. 51 00:02:17,820 --> 00:02:19,680 ‫So this is a very good use case. 52 00:02:19,680 --> 00:02:22,350 ‫Another one of using different cache behaviors 53 00:02:22,350 --> 00:02:23,970 ‫is to maximize cache hit. 54 00:02:23,970 --> 00:02:27,690 ‫So for example, the static request may go into Amazon S3. 55 00:02:27,690 --> 00:02:29,880 ‫Here we don't have any cache policy 56 00:02:29,880 --> 00:02:31,710 ‫with any headers or session. 57 00:02:31,710 --> 00:02:35,250 ‫We maximize the cache hit based on just the resource we hit. 58 00:02:35,250 --> 00:02:37,110 ‫And then for dynamic, for example, 59 00:02:37,110 --> 00:02:40,560 ‫for a REST HTTP server that is using a load balancer 60 00:02:40,560 --> 00:02:42,990 ‫and EC2, you may want to cache based 61 00:02:42,990 --> 00:02:45,330 ‫on the correct headers and cookies based 62 00:02:45,330 --> 00:02:47,970 ‫on the cache policy you've defined as before. 63 00:02:47,970 --> 00:02:49,470 ‫So hopefully that makes sense. 64 00:02:49,470 --> 00:02:52,533 ‫I hope you liked it and I will see you in the next lecture.