CDN 备案

未备案网站如何使用cdn

九三
306
2021-02-10 09:41:35
栏目: 云计算

未备案网站如何使用cdn

未备案的网站使用国内CDN加速的方法

1.首先,使用记事本打开网站的配置文件,并在配置文件中添加以下配置;

server

{

listen 80;

# HTTPS配置略

server_name static.beiandomain.com; # 改成你实际已备案的二级域名(这个就是新建主机时绑定的域名)

index index.html index.htm index.php default.html default.htm default.php;

root /data/wwwroot/yourwebsitedomain.com; # 需要CDN加速的网站

# 图片等静态资源请求代理到本地主站(关键配置)

location ~* .*\.(js|css|png|jpeg|jpg|bmp|ico|ogg|ogv|svg|svgz|eot|otf|woff|woff2|mp4|ttf|rss|atom|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {

add_header Access-Control-Allow-Origin *; # 解决字体跨站问题

add_header Access-Control-Allow-Headers X-Requested-With;

add_header Access-Control-Allow-Methods GET,OPTIONS;

proxy_pass http://127.0.0.1; # 如果是启用了https的网站,这里最好改成 https://127.0.0.1,避免主站加了非https协议的跳转配置,导致不成功。

proxy_set_header X-Forwarded-For $remote_addr;

proxy_redirect off;

proxy_set_header Host yourwebsitedomain.com; # 这里改为实际主站域名(必须)

expires max; # 设置浏览器304缓存为最长期限

}

# 为这个二级域名额外设置一个robots文件

location ~ (robots.txt) {

rewrite /robots.txt /resrobots.txt last; # 在网站根目录新增一个resrobots.txt,内容和七牛CDN类似,禁止搜索引擎抓取非静态资源(怎么写接着看下面)

}

# 如果通过静态域名访问的是非静态资源,比如访问了我们的文章页面,则跳到主站对应的页面。

location / {

if ( $request_uri !~* .*\.(js|css|png|jpeg|jpg|gif|bmp|ico|ogg|ogv|svg|svgz|eot|otf|woff|woff2|mp4|ttf|rss|atom|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf))

{

rewrite ^(.*)$ $scheme://yourwebsitedomain.com$1 permanent; # yourwebsitedomain.com修改为实际主站域名

}

}

location ~ /\. { deny all; access_log off; log_not_found off; }

access_log off;

}

2.网站配置文件修改好后,在网站根目录中添加一个resrobots.txt文件;

User-agent: *

Allow: /robots.txt

Allow: /wp-content/

Allow: /*.png*

Allow: /*.jpg*

Allow: /*.jpeg*

Allow: /*.gif*

Allow: /*.bmp*

Allow: /*.ico*

Allow: /*.js*

Allow: /*.css*

Disallow: /

3.最后,resrobots.txt文件添加好后,在functions.php文件中添加cdn代理即可;

function QiNiuCDN(){

function Rewrite_URI($html){

$domain = 'yourwebsitedomain\.com'; //填写主站域名,小数点前需要加上反斜杠转义

$static = 'res.zgboke.com'; //填写二级静态域名(使用第三方的CDN加速后,这里需要替换成你CDN的名字,而原来已备案的二级域名则为源站)

//更多静态资源需要替换,可以将后缀加到后面的括号,使用分隔符分割

$html = preg_replace('/http(s|):\/\/'.$domain.'\/wp-([^"\']*?)\.(jpg|png|gif|bmp|jpeg|css|js)/i','//'.$static.'/wp-$2.$3',$html);

return $html;

}

if(!is_admin()){

ob_start("Rewrite_URI");

}

}

add_action('init', 'QiNiuCDN');

0
看了该问题的人还看了