使用 frp 远程连接 Jupyter Lab/Notebook 无法连接到 Kernel, F12 查看控制台输出, 发现是有关 websocket failed 的错误. 一番搜索后发现是跳板机的问题.

    frp server 跑在跳板机上, 使用 nginx 做的域名映射如下:

    1. server {
    2. listen 80;
    3. server_name "xxx.xxxxx.xxxx";
    4. location / {
    5. proxy_pass http://127.0.0.1:8888;
    6. proxy_set_header Host $host;
    7. proxy_set_header X-Real-IP $remote_addr;
    8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    9. proxy_hide_header X-Powered-By;
    10. }
    11. }

    Jupyter Lab/Notebook 在建立 Kernel 连接时用的是 WebSocket, 上面的域名映射默认不支持, 需要添加额外的 WebSocket 相关的映射, 修改后如下:

    1. server {
    2. listen 80;
    3. server_name "xxx.xxxxx.xxxx";
    4. location / {
    5. proxy_pass http://127.0.0.1:8888;
    6. proxy_set_header Host $host;
    7. proxy_set_header X-Real-IP $remote_addr;
    8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    9. proxy_hide_header X-Powered-By;
    10. }
    11. location /api/kernels/ {
    12. proxy_pass http://127.0.0.1:8888;
    13. proxy_set_header Host $host;
    14. # websocket support
    15. proxy_http_version 1.1;
    16. proxy_set_header Upgrade "websocket";
    17. proxy_set_header Connection "Upgrade";
    18. proxy_read_timeout 86400;
    19. }
    20. location /terminals/ {
    21. proxy_pass http://127.0.0.1:8888;
    22. proxy_set_header Host $host;
    23. # websocket support
    24. proxy_http_version 1.1;
    25. proxy_set_header Upgrade "websocket";
    26. proxy_set_header Connection "Upgrade";
    27. proxy_read_timeout 86400;
    28. }
    29. }

    即把 /api/kernels//terminals/的协议更新为 WebSocket 即可.