惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Hacker News: Front Page
博客园 - 【当耐特】
量子位
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Register - Security
The Register - Security
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
Jina AI
Jina AI
The Cloudflare Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
L
LINUX DO - 最新话题
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
Stack Overflow Blog
Stack Overflow Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Engineering at Meta
Engineering at Meta
W
WeLiveSecurity
博客园 - 三生石上(FineUI控件)
Security Archives - TechRepublic
Security Archives - TechRepublic
Hugging Face - Blog
Hugging Face - Blog
T
Troy Hunt's Blog
C
CERT Recently Published Vulnerability Notes
N
News and Events Feed by Topic
S
SegmentFault 最新的问题
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
人人都是产品经理
人人都是产品经理
SecWiki News
SecWiki News
N
News and Events Feed by Topic
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
宝玉的分享
宝玉的分享
Schneier on Security
Schneier on Security
阮一峰的网络日志
阮一峰的网络日志
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
J
Java Code Geeks

JaSpirit的万事屋

通过nginx日志分析网站流量 线性回归的显著性检验 神经科学的早期发展 生物化学和化学生物学的区别 化学分析和仪器分析的区别 膜蒸馏技术——原理与应用 HER 和 OER 反应机理与其 Tafel 斜率推导 神经电极的发展历程 深入理解主成分分析 PCA 《神经科学——探索脑》 第一篇(1-7章)思维导图及课后答案 校园网环境下软路由及NAS的搭建 X射线吸收精细结构——原理及应用 三个电化学基本方程的推导 电解水过程中的催化普适性原理 涉及X射线研究的诺贝尔奖 LBRY 汉化相关问题 互联网时代的艺术:LBRY简介 安全套的前世今生——高分子材料如何改变着这个世界 两岸统一态度调研 追迹互联网(2010-2016) ロビンソン和STYX HELIX中文翻译 基于Python的Bilibili和Bangumi动画评分综合分析 同位素峰强度分布计算原理及python实现 2048游戏算法的进一步研究及C++语言实现
基于 nginx 的高性能 jsdelivr 反向代理
Ja Spirit · 2022-05-27 · via JaSpirit的万事屋

由于 2022 年 5 月以来 jsdelivr 访问进一步受限,对于仍需要使用 jsdelivr 的站长而言,自建反向代理服务是最优选择。为了在服务端和客户端都实现加速,我们采用 nginx 的缓存机制,初次请求之后不需回源;同时,使静态资源在浏览器中缓存时间更长。此外,由于涉及到跨域资源共享(CORS)问题,还需要在响应头中配置Access-Control-Allow-Origin关键字,以实现域名白名单机制,并保证资源在浏览器中正常加载。

1. 完整配置文件

配置文件: jsdelivr.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
proxy_cache_path /var/www/jsdelivr.cache levels=2:2 use_temp_path=off keys_zone=jsdelivr:50m inactive=180d max_size=500m;

server
{
listen 443 ssl;
server_name jsdelivr.example.com;


ssl_certificate 你的证书;
ssl_certificate_key 你的证书私钥;
ssl_trusted_certificate 你的证书链;


ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
server_tokens off;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;


access_log /usr/local/nginx/logs/jsdelivr.log;

location /
{
proxy_pass https://cdn.jsdelivr.net;
proxy_ssl_server_name on;
proxy_set_header Host cdn.jsdelivr.net;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_http_version 1.1;
proxy_set_header Connection "";
add_header X-Cache $upstream_cache_status;
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache jsdelivr;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 180d;
expires 180d;
proxy_hide_header Access-Control-Allow-Origin;
add_header Access-Control-Allow-Origin "https://blog.example.com";
}
}

将该文件保存为jsdelivr.conf,再在nginx.confhttp块中包含(include)该文件即可。

2. 配置文件解读

2.1 缓存文件配置

使用proxy_cache_path指令进行缓存文件配置:

proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size];

该指令中:

  • path 为缓存文件存放路径;
  • levels 为缓存文件目录的层次结构,levels=2:2 表示两级目录都使用 2 位 16 进制数命名;
  • use_temp_path 表示临时文件是否直接存放于缓存目录中,如果该值为off,则临时文件将直接放在缓存目录中;反之,则需要用proxy_temp_path设置一个目录。为了减少不必要的拷贝,建议设为off
  • keys_zone 表示在共享内存中设置的存储区域名称和大小。该存储区用来存放缓存的 key 字符串,keys_zone=jsdelivr:50m表示该区域的名称为 jsdelivr,大小为 50m,这样 nginx 可以快速判断一个 request 是否命中缓存,1m 可以存储 8000 个 key,10m 可以存储 80000 个 key;
  • inactive 表示未被访问文件在缓存中保留时间;
  • max_size 为最大缓存空间,如果不指定,会使用掉所有磁盘空间。当达到配额后,会删除最少使用的 cache 文件。

2.2 反向代理配置

在反向代理配置中,我们需要实现以下功能或特性:

  1. 将请求转发到https://cdn.jsdelivr.net
  2. 缓存状态码为200的请求,时限180天
  3. 设置跨域资源共享(CORS)策略,允许来自https://blog.example.com的请求
  4. 设置资源在浏览器中缓存时间为180天

在location块中,如下指令分别实现了以上功能:

  1. 请求转发
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

    proxy_pass https://cdn.jsdelivr.net;

    proxy_ssl_server_name on;

    proxy_set_header Host cdn.jsdelivr.net;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;

    proxy_http_version 1.1;
    proxy_set_header Connection "";
  2. 服务端缓存设置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    add_header X-Cache $upstream_cache_status;

    proxy_ignore_headers Set-Cookie Cache-Control expires;

    proxy_cache jsdelivr;

    proxy_cache_key $host$uri$is_args$args;

    proxy_cache_valid 200 180d;
  3. CORS设置
    1
    2
    3
    4

    proxy_hide_header Access-Control-Allow-Origin;

    add_header Access-Control-Allow-Origin "https://blog.example.com";
  4. 客户端缓存设置
    1
    2

    expires 180d;

3 参见

  1. 分享一个nginx反代jsdelivr的优化配置

  2. Nginx 内容缓存及常见参数配置

  3. Module ngx_http_proxy_module

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 JaSpirit的万事屋