在 ArchLinuxARM 上安装 JupyterLab 并配置反向代理

在 ArchLinuxARM 上安装 JupyterLab 并配置反向代理

RayAlto OP

1. 安装 JupyterLab

1
pacman -S jupytelab

最近的 JupyterLab 好像不是必须依赖 jupyter-server-terminals 了,所以如果有开 Terminal 的需求你得:

1
pacman -S python-jupyter-server-terminals

2. 配置 JupyterLab

编辑 ~/.jupyter/jupyter_lab_config.py ,主要修改这些项:

1
2
3
4
5
6
7
c.LabServerApp.open_browser = False     # 不自动尝试打开浏览器
c.ServerApp.allow_origin = '*' # 允许非本地连接
c.ServerApp.allow_remote_access = True # 允许非本地连接
c.ServerApp.ip = '0.0.0.0' # 监听所有 IPv4
c.ServerApp.port = 4444 # 监听 4444 端口
c.ServerApp.root_dir = '~/.jupyter' # 笔记本的根目录
c.ServerApp.trust_xheaders = # 方便反向代理

3. Frp 到公网

1
2
3
4
5
[jupyterlab]
type=tcp
local_ip=127.0.0.1
local_port=4444
remote_port=4444
  • 我这里直接用运行 JupyterLab 的机器同时运行 Frpc 的,所以 local_ip127.0.0.1
  • 我想要本地 JupyterLab 监听 4444 , frp 到公网的 4444 ,所以 local_portremote_port4444

4. Nginx 反向代理

其实 Frp 就算一层反向代理,再加上 Nginx 又是一层反向代理

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
43
44
45
# JupyterLab WebSocket
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

# JupyterLab HTTP
server {
listen 80;
server_name your.domain;
return 301 https://your.domain;
}

# JupyterLab HTTPS
server {
listen 443 ssl;
server_name your.domain;

ssl_certificate /etc/letsencrypt/live/your.domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your.domain/privkey.pem;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
proxy_pass http://127.0.0.1:4444;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Scheme $scheme;

proxy_buffering off;
}

location ~ /.well-known {
allow all;
}
}

5. 后记

其实我还把 JupyterLab 用作了 trojan-go 的 fallback ,目前发现的好处有:

  • 搭建方便,不用费尽心思准备网站内容
  • 平时维护时可以直接用 JupyterLab 的 Terminal 走一般的 WSS 协议,避免长时间 ssh 导致 GFW 铁拳(因为初代 shadowsocks 本质上就是 SSH Tunnel )
  • 大型的下载任务可以通过这个 Terminal 直接在墙外下载,然后再通过 JupyterLab 的下载功能走一般的 HTTPS 协议,避免大流量引起 GFW 怀疑
目录
在 ArchLinuxARM 上安装 JupyterLab 并配置反向代理