2021 Cloudflare: Basic Pages Rules and Cache Levels, Standard and "Cache Everything"

Become a Web Developer
1 Sept 202105:51

Summary

TLDR这个视频讲解了如何在Cloudflare设置页面规则来优化网站缓存和性能。首先,设置一个通配符规则,将缓存级别设置为"缓存全部",这将缓存所有页面,包括动态生成的PHP页面。然后启用自动压缩CSS和JavaScript文件的功能,以减小文件大小。接着,设置一个月的缓存有效期。为了避免缓存某些动态页面,可以为特定页面设置"绕过缓存"规则,如联系表单页面等。通过设置页面规则,网站所有者可以优化缓存策略,提高网站性能和用户体验。

Takeaways

  • 👉 主要解释了Cloudflare的页面规则功能。
  • 🔑 页面规则可以应用于特定页面或整个网站。
  • 🌐 使用通配符(*)可以将规则应用于所有页面。
  • 💾 缓存等级可以设置为"标准"或"全部缓存"。
  • 💻 "全部缓存"会缓存动态页面(如PHP页面)的内容,导致内容不再随机生成。
  • 🛠 可以为特定页面设置"绕过缓存"的规则,以防止缓存动态内容。
  • 🔄 自动缩小CSS和JavaScript文件可以减小文件大小。
  • 🕰 可以设置文件的缓存时间,例如1个月。
  • 🔑 针对不同页面应用不同的缓存规则很重要,避免缓存不应该缓存的内容。
  • 💻 要根据网站的实际情况和需求,合理设置页面规则。

Q & A

  • Cloudflare为域名提供了多少免费的页面规则?

    -Cloudflare为每个域名提供了三个免费的页面规则。

  • 在Cloudflare中设置页面规则时,星号(*)的作用是什么?

    -在Cloudflare页面规则中,星号(*)作为通配符,可以匹配任何内容。比如,如果规则应用于'index*',它可以匹配'index.php'、'index.html'等任何以'index'开头的页面。

  • 页面规则中的'缓存级别'设置默认值是什么?

    -页面规则中的'缓存级别'设置的默认值是'标准',这会缓存如图片、JavaScript和CSS等常规内容。

  • 选择'缓存所有内容'对动态页面有什么影响?

    -选择'缓存所有内容'会缓存动态页面,如PHP页面,这意味着这些页面会被保存在Cloudflare的边缘服务器上,对所有用户显示相同的内容,不再动态生成。

  • 为什么要小心使用'缓存所有内容'设置?

    -使用'缓存所有内容'设置时要小心,因为它会缓存动态生成的内容,如随机生成的图片列表,这可能不适合所有网站,尤其是内容经常变化的网站。

  • 在设置页面规则时,如何排除某些页面不被缓存?

    -可以通过为特定页面设置不同的页面规则来排除它们不被缓存,比如将缓存级别设置为'绕过',以确保动态内容,如含有联系表单的页面,不被缓存。

  • 页面规则中的'自动压缩'有什么用途?

    -'自动压缩'功能可以压缩CSS、JS文件,减少文件大小,提高网页加载速度。

  • 如何设置Cloudflare页面规则以优化静态网站的性能?

    -对于静态网站,可以通过设置'缓存所有内容'和'自动压缩'等页面规则来优化性能,这样可以缓存网站的所有内容,并压缩代码来加快加载速度。

  • 在Cloudflare页面规则中,如果不希望缓存某个特定页面,应如何操作?

    -如果不希望缓存某个页面,可以为该页面创建一个页面规则,并将缓存级别设置为'绕过',这样可以确保页面内容动态生成,不被缓存。

  • Cloudflare页面规则的设置对网站性能和用户体验有什么影响?

    -正确设置Cloudflare页面规则可以显著提高网站性能和用户体验,通过缓存内容和压缩文件减少加载时间,同时允许动态内容保持更新,平衡速度与实时性。

Outlines

00:00

🌐 设置Cloudflare页面规则

本段落详细介绍了如何在Cloudflare上设置页面规则以优化网站性能。首先,指出大部分DNS指向Cloudflare时,可能需要设置页面规则。通过点击域名下的页面规则选项,用户可以免费获取三个页面规则。页面规则的设置包括使用通配符(*)来应用规则于网站的所有页面,例如index.php或admin/users.php等。主要讨论了缓存级别设置,包括标准缓存和缓存所有内容的差异,以及如何谨慎使用缓存以避免动态内容被错误缓存。通过例子说明了缓存设置的影响,包括如何使动态生成的内容(如随机显示的公寓照片)被全球的Cloudflare服务器缓存。最后,提到了针对几乎静态的网站如何设置缓存规则,并简要介绍了自动压缩CSS和JS的选项。

05:02

📤 特定页面缓存策略

第二段落解释了如何为特定页面设置不同的缓存策略,以处理可能需要动态内容更新的页面。使用了一个例子来说明,即使网站没有联系页面,但有关于页面(about.php)可能包含联系表单等动态内容,因此选择为该页面设置“绕过缓存”规则,确保该页面不被缓存。这样做可以保持页面内容的实时更新,避免因缓存导致的信息滞后或错误显示。简单地总结了基本页面规则的设置方法和考虑因素。

Mindmap

Keywords

💡DNS

DNS是域名系统的缩写,它将互联网域名与计算机IP地址相互映射。在这个视频中,说明大多数域名解析指向Cloudflare,这意味着Cloudflare作为DNS服务商为其提供域名解析服务。

💡Cloudflare

Cloudflare是一家提供内容分发网络服务的公司,能够为网站提供分布在全球各地的服务器来缓存、优化和分发内容。在本视频中,作者提到他们大部分域名解析都指向Cloudflare,需要在其中设置页面规则。

💡页面规则

页面规则是Cloudflare提供的一种控制和配置功能,能够针对不同网页设置不同的缓存级别、优化方式等参数。本视频中演示了如何设置页面规则,比如缓存所有静态内容、自动压缩JS/CSS等。

💡缓存级别

缓存级别决定了Cloudflare在其边缘服务器上缓存内容的程度。标准缓存只缓存静态资源,而缓存所有则会缓存动态内容,例如视频中提到的PHP页面。缓存过多可能会造成动态内容无法正常更新,因此需要小心选择。

💡自动压缩

自动压缩是一种Cloudflare提供的优化功能,能够在传输资源前对JS/CSS等内容进行压缩,减少传输大小。这在视频中被设置为一种页面规则,可以自动应用到所有静态资源文件上。

💡边缘服务器

边缘服务器是Cloudflare分布在全球各地的服务器,用于缓存、优化和分发网站内容。当用户访问网站时,离他们最近的边缘服务器就会响应请求并提供缓存的内容,提高了访问速度和效率。视频中提到了缓存内容会在成百上千的边缘服务器上保存。

💡静态网页

静态网页是指其内容事先固定的HTML文件,访问时无需动态生成,内容不会随用户而改变。视频讨论了如何对静态网站采用高级缓存选项,因为这对于不经常变更的内容很有用。

💡动态网页

动态网页则是在访问时根据用户输入或其他条件动态生成内容的页面,比如PHP网页。视频中提到,过度缓存动态页面会导致每个用户看到相同内容,而非根据不同情况渲染出差异化结果。因此,对动态网页需要较低的缓存级别。

💡通配符

通配符是一种用于匹配多个字符串的特殊字符,常用的有*、?等。视频中使用了*通配符来应用页面规则,例如*会匹配所有页面路径,*.php会匹配所有PHP页面。这允许灵活的对不同页面组应用规则。

💡联系表单

联系表单是很多网站提供的、允许用户向网站发送信息的在线功能。视频作者提到要对about.php页面设置不同的规则,可能是因为该页面包含联系表单,需要保证其动态功能可用。所以对联系表单页面需要更低的缓存级别。

Highlights

我们大部分的域名都指向 Cloudflare,你可能需要设置页面规则。

页面规则做了什么?首先,我刚才所做的将应用于我网站上的所有页面。

星号(*) 表示通配符,会匹配任何后续内容。

首先你会希望处理缓存级别的设置。默认情况下它设置为标准,这会缓存你的图像、JavaScript 和 CSS 等常规内容。

如果你点击"缓存全部",它将缓存你的动态页面,例如 PHP 页面,而不是 HTML 页面。

你必须对此非常小心,因为在此网站上,这些公寓的照片是随机生成的,每次有人请求页面时,你都会看到不同的公寓。

在设置页面规则为缓存全部后,Cloudflare 在全球数百个不同的边缘服务器上会缓存这个页面。

因此,特定公寓 ID,如 1742 和 1402,会开始对每个人显示相同的页面,不再随机动态生成内容。

有时这正是你想要的,有时则不是,你可以使用页面规则指定该设置应用于哪个特定页面或页面组。

对于这个西班牙词频词典网站,它基本上是一个静态网站,我想缓存全部内容。

我们还可以自动压缩 CSS、JS 等以减小代码体积。

让我们为这条缓存规则设置一个月的缓存过期时间。

现在让我们创建另一条页面规则,因为我可能想要对某些页面有更多控制。

对于联系人页面 contact.php(实际上没找到),让我们将缓存级别设置为绕过全部,这样我们就不会缓存这个可能包含动态联系表单的页面。

这些是一些基本的页面规则。

Transcripts

play00:00

okay

play00:02

now

play00:02

all of our dns or not all of it most of

play00:05

it is pointing to cloudflare

play00:08

and you're probably going to want to set

play00:10

up a page rule or two

play00:12

okay so here's our domain and you want

play00:15

to click here on page rules

play00:17

you get three of them for free

play00:21

all right

play00:24

so let's do that

play00:32

now what do these page rules do

play00:35

all right so first of all

play00:37

what i just did

play00:39

will

play00:40

apply to all the pages on my website

play00:44

okay this asterisk will anything that

play00:47

were to come after it

play00:49

if i said uh

play00:53

index

play00:55

then that would apply to

play00:59

index.php or index.html

play01:03

or

play01:04

anything if i put in let's say

play01:09

admin

play01:10

right then that would apply to

play01:13

admin

play01:14

uh

play01:16

users.php it would apply to admin

play01:20

uh

play01:22

it would apply to anything that came

play01:24

after it it would apply to admin

play01:27

uh

play01:28

panel.php any right you get it so it's a

play01:31

wild card

play01:33

so this page rule will apply to

play01:36

all the pages

play01:39

and the first setting you surely will

play01:42

want to deal with is the cache level

play01:46

okay by default it is set to standard

play01:50

and standard will do things like it will

play01:51

cache your images javascript css the

play01:54

normal stuff

play01:55

but then

play01:57

if you click cache everything

play02:00

that will cache your dynamic pages for

play02:03

example your php pages

play02:06

not your html pages and your php pages

play02:09

so you have to be very careful with this

play02:12

on this website here you can see

play02:14

index.php

play02:16

uh i tested it it does it does exactly

play02:19

what it says it will do

play02:21

all of these uh photos here these

play02:24

apartments they're generated randomly

play02:27

every time someone requests the page you

play02:29

have a different set of apartments that

play02:31

pop up

play02:32

right so after you set

play02:35

uh your rule to cache everything

play02:39

on your edge points right your cloud

play02:42

players

play02:43

hundreds of different servers spread out

play02:45

across the globe

play02:47

it will cache

play02:49

all of this

play02:50

such that

play02:52

apartment

play02:53

id

play02:54

1742

play02:56

1402

play02:57

this exact page will start appearing to

play03:00

everybody

play03:02

the same no matter what no matter how

play03:04

many times they request the page you

play03:06

will no longer see it it they won't

play03:08

randomly

play03:10

uh dynamically generate that content for

play03:13

the people that request your page now

play03:14

sometimes that's what you want but not

play03:17

always and you can use your page rules

play03:19

to specify

play03:21

when this setting will apply to a

play03:24

particular page or hear a group of pages

play03:27

now in the case of this spanish

play03:28

frequencydictionary.com

play03:31

it's basically a static site i want to

play03:33

cache everything okay

play03:36

i'm going to make one exception in a

play03:37

minute but

play03:38

for now we're just going to

play03:42

stay with this okay

play03:44

so we've got cache everything it's going

play03:46

to do that let's add another one

play03:49

auto minify

play03:51

i want to auto minify my css jss

play03:55

js

play03:56

if essentially if you don't know what

play03:58

that means you can look it up but it

play03:59

will compress your code

play04:02

alright

play04:03

um

play04:05

now let's do another one

play04:12

where is it

play04:19

no i don't want that

play04:22

oh yeah okay

play04:23

let's set this out to a month

play04:26

okay so we've got all these

play04:30

um

play04:33

and

play04:34

let's save that

play04:37

all right so we've got there's our one

play04:39

page rule now let's create another one

play04:41

because

play04:43

i may

play04:44

want to

play04:46

uh

play04:48

i may want to have

play04:50

a little more

play04:54

and let's just do

play04:57

contact.php

play04:59

what do we have here on this

play05:01

oh wait a minute

play05:03

do i have a contact no i don't have a

play05:05

contact page

play05:07

i've got this so what do i have oh i

play05:08

have it on my about page

play05:11

um

play05:12

okay

play05:14

well i guess we'll just do

play05:18

about php

play05:22

so

play05:23

we'll put our cash level to bypass all

play05:27

right so now uh we're not going to cache

play05:30

this page

play05:31

because i might

play05:32

have that page be dynamic with a contact

play05:36

form that that sort of thing and so i

play05:38

don't want to cache that page

play05:45

okay so

play05:47

those are some basic page rules

Rate This

5.0 / 5 (0 votes)

¿Necesitas un resumen en inglés?