Jupyter notebook使用cdn加速

使用jupyter lab, 如果没有cache, 加载内容会超过20MB,对于屌丝大众使用的1mbs带宽服务器无疑是庞然大物,所以想到CDN加速.

main.5b71481d463d2b52427d.js > 5.5MB

vendors~main.9a98be57ad40a4bb7454.js > 9.9MB

腾讯云CDN服务新手免费使用6个月, 每月20GB真香。

目前想到方案:

  1. 使用nginx 做反向代理
  2. 添加子域名,但是添加CNAME的时候,与二级域名A 冲突,直接将A 记录删掉, 更换成CDN提供的域名;
  3. 使用path,在主机域名上直接重定向到jupyter server, 总是会会报错,估计应该是重定向错误问题
# add me to /etc/nginx/sites-available
server {
listen 80;
server_name cdn.jupyter.org jupyter.scdn1.secure.raxcdn.com un-cdn.jupyter.org;
error_log /var/log/nginx/error.log;
root /srv/cdn.jupyter.org;
location / {
add_header 'Access-Control-Allow-Origin' '*';
}
}
server {
listen 443;
server_name cdn.jupyter.org un-cdn.jupyter.org;
ssl on;
ssl_certificate /etc/ssl/jupyter.org.crt;
ssl_certificate_key /etc/ssl/jupyter.org.key;
ssl_ciphers "AES128+EECDH:AES128+EDH";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
add_header X-Content-Type-Options nosniff;
ssl_stapling on; # Requires nginx >= 1.3.7
ssl_stapling_verify on; # Requires nginx => 1.3.7
resolver_timeout 5s;
error_log /var/log/nginx/error.log;
root /srv/cdn.jupyter.org;
location / {
add_header 'Access-Control-Allow-Origin' '*';
}
}

有兴趣的可以参考一下这个repo:

https://github.com/jupyter/cdn.jupyter.org/blob/master/jupyter-cdn.nginx

目前使用次级域名方式提供服务. 开启cdn加速, 页面会有问题,有可能是因为cache导致的问题,所以目前是没有开启功能.

 

测试下来, jupyter notebook访问还是很慢, cache 过期就会重新下载页面内容~

思考一下, 感觉jupyter notebook使用cdn 貌似也不太靠谱~

如有方案, 也欢迎推荐了.

 

贴一下自己的nginx config

server {
# SSL configuration
#
listen 443 ssl;
server_name xxx.com;

client_max_body_size 30M;
# SSL parameters
ssl on;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/cert.key;

#ssl_ciphers HIGH:!aNULL:!MD5;
ssl_ciphers "AES128+EECDH:AES128+EDH";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
add_header X-Content-Type-Options nosniff;
ssl_stapling on; # Requires nginx >= 1.3.7
ssl_stapling_verify on; # Requires nginx => 1.3.7
ssl_session_timeout 5m;
resolver_timeout 5s;

location / {
proxy_pass http://127.0.0.1:yourport;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* /(api/kernels/[^/]+/(channels|iopub|shell|stdin)|terminals/websocket)/? {
proxy_pass http://127.0.0.1:yourport;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

}

}

标签:,

About: kiah