安福生活网

cdn基础知识 cdn使用注意事项

网络整理 资讯
文件50k和50kb哪个大 CDN上的一些细节处理经验技巧

无聊写来充数的文章,大佬轻喷,看看就好……

cdn基础知识 cdn使用注意事项

1. 沉浸式地址栏

使用手机版Chrome访问我的博客地址栏会同步颜色,如图

在…之间添加以下标签:

\">\">\">

将<颜色><地址>换为自己的网页颜色和地址即可。

2. 少数超链接取消InstantClick

使用data-no-instant标签,如…

3. 完全SSL加密(小绿锁)

使用自己的、带有SSL加密的CDN空间来存放Static Libs,SSL推荐Let's Encrypt证书,完全免费。

4. CDN权限控制

盗链严重怎么办?在空间的配置里稍微改一下就行了。

配置文件(Nginx)

#授予域名访问权限location ~ ^(<目录1>|<目录2>|<目录3>|…){ valid_referers <授予权限的域名> if ($invalid_referer) { return 403 #rewrite ^/ <403域名> }}#开启个别文件夹公共访问权限,并打开目录树,并设置每连接下载超过50KB之后限速50KB/slocation ~ ^(<目录1>|<目录2>|<目录3>|…){ autoindex on autoindex_exact_size off autoindex_localtime off limit_rate_after 50k limit_rate 50k}

5. Typecho伪静态的设置

配置文件(Nginx)

location / { index index.html index.php if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php } if (!-f $request_filename){ rewrite (.*) /index.php }}

6. 尽量使用301强制跳转https

配置文件(Nginx)

使用两个server语句块

server{ listen 80 server_name <域名> return 301 https://$host$request_uri}server{…}

7. 支持Ajax访问Static Libs资源

配置文件(Nginx)(所有字体文件)

location ~* \.(eot|otf|ttf|woff|woff2)${ if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*' add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range' add_header 'Access-Control-Max-Age' 1728000 add_header 'Content-Type' 'text/plain charset=utf-8' add_header 'Content-Length' 0 return 204 } if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '*' add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range' add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range' } if ($request_method = 'GET') { add_header 'Access-Control-Allow-Origin' '*' add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range' add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range' }}

8. 缓存大文件(如PNG、JPG、MP3等)

配置文件(Nginx)

#默认缓存30天location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|mp3)${ expires 30d access_log off }cdn更好的使用 *** cdn使用注意事项