TypechoJoeTheme

半醉残影

统计

nginx 反向代理,解决公共CDN访问速度过慢的问题

2022-05-30
/
0 评论
/
379 阅读
/
正在检测是否收录...
05/30

前言

之前一直在使用公共CDN来托管个人网站的静态文件,由于白嫖的人越来越多了,实在用不下去了,只能自己搭建优化了

如果网站面向的也是海外用户,直接使用cloudflare就已经很香了,如果是面向大陆用户,请往下看。

正文

公共CDN

默认已有一正常可使用的域名,该域名使用nginx代理

mycdn.omba.cc

这里以jsdelivr为例,使用mycdn.omba.cc代理jsdelivr

注意mycdn.omba.cc需要可以直接访问被代理的网站才行

给mycdn.omba.cc网站添加nginx反向代理

# mycdn.omba.cc nginx 反向代理设置

location /npm
{
    # 代理配置
    proxy_pass https://cdn.jsdelivr.net;
    proxy_set_header Host $proxy_host;
    proxy_set_header Accept-Encoding '';
    proxy_ssl_server_name on;
    proxy_ssl_name $proxy_host;
    proxy_redirect / /;
    
    # 缓存配置
    proxy_cache jsdelivr;
    proxy_cache_lock on;
    proxy_cache_lock_timeout 15s;
    proxy_cache_use_stale updating;
    proxy_cache_background_update on;
    proxy_cache_key $host$request_uri;
    proxy_cache_valid 200 301 302 30d;
    proxy_cache_valid 500 501 502 503 15s;
    proxy_cache_valid any 5m;
}
proxy_cache_path /www/tmp/jsdelivr levels=1:2 use_temp_path=off keys_zone=jsdelivr:300m inactive=30d max_size=10g;

使用的时候,把jsdelivr的静态资源的域名直接替换成你的mycdn.omba.cc就行了

自定义CDN

有时候需要代理的不是公共CDN,而是自己已经部署的网站,则使用如下配置nginx反向代理即可。

加入已部署的网站需要被静态资源加速网站 blog2.omba.cc,加速域名还是mycdn.omba.cc

# mycdn.omba.cc 的nginx反向代理

location /custom_cdn
{
    # 正则表达式匹配不是静态资源的全部过滤
    if ($uri !~* \.(gif|png|jpg|css|js|woff|woff2|svg)) {return 403;}

    # 代理配置
    proxy_pass https://blog2.ombc.cc/;
    add_header 'Access-Control-Allow-Origin' *;
    proxy_set_header Host $proxy_host;
    proxy_set_header Accept-Encoding '';
    proxy_ssl_server_name on;
    proxy_ssl_name $proxy_host;
    proxy_redirect / /;
    
    # 缓存配置
    proxy_cache custom_cdn;
    proxy_cache_lock on;
    proxy_cache_lock_timeout 15s;
    proxy_cache_use_stale updating;
    proxy_cache_background_update on;
    proxy_cache_key $host$request_uri;
    proxy_cache_valid 200 301 302 30d;
    proxy_cache_valid 500 501 502 503 15s;
    proxy_cache_valid any 5m;

}

proxy_cache_path /www/tmp/custom_cdn levels=1:2 use_temp_path=off keys_zone=custom_cdn:300m inactive=30d max_size=10g;

使用的时候,把blog2.ombc.cc的静态资源的域名直接替换成你的mycdn.omba.cc/custom_cdn就可以了

nginxcdn
朗读
赞(0)
赞赏
感谢您的支持,我会继续努力哒!

三合一收款

下面三种方式都支持哦

微信
QQ
支付宝
打开支付宝/微信/QQ扫一扫,即可进行扫码打赏哦
版权属于:

半醉残影

本文链接:

https://blog.dengyb.com/archives/129/(转载时请注明本文出处及文章链接)

评论 (0)

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

最新回复

  1. 搭建自用导航网站 R11; JKblog
    2022-03-10
  2. JK
    2022-01-13

    {!{data:image/webp;base64,UklGRhwNAABXRUJQVlA4WAoAAAAwAAAAAQMAKwEASUNDUBgCAAAAAAIYAAAAAAQwAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAHMAUgBHAEIAAAAAAAAAAAAAAAAAAAAAAAA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r5+zsobUG9Pf9q9VLGjtj6LnA/MWX2JAqZ5n3sBi9L0LJI+8MSqGzWgzrxhK5q3IesD43pNmNBf4d7pGTs0AsIOPquuzi82oV76kNKJxC/Z+pZF0a4XeQQcszOSw5x5R+YRcAwovkkrE7H/xtzXb9Y4Lvfg2bl9gK7zks9fnz+Ijq2I/mVlpzD8b/jf8L/hf8P/hv8N/xv+/6/7AFZQOCDSAQAAUDUAnQEqAgMsAT9xuNlltK8rpyAIApAuCWlu4XdhG0AJ7APfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJyHvtk5D32ych77ZOQ99snIe+2TkPfbJwoAAP7/rR4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==}!}

  3. 搭建自用导航网站 R11; JKblog
    2022-01-12
  4. 搭建自用导航网站 R11; JKblog
    2022-01-12
  5. MrGao
    2019-09-03

标签云