安装软件:
$ sudo apt install -y python3 python3-pip python3-venv
$ useradd jupyter -m
$ su - jupyter
$ python3 -m venv jupyter-env
$ source jupyter-env/bin/activate
$ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
$ pip install jupyter
配置Jupyter:
$ jupyter notebook --generate-config
$ jupyter notebook password
$ vi ~/.jupyter/jupyter_notebook_config.py
c.ServerApp.allow_origin = 'https://yourhost/'
c.ServerApp.allow_remote_access = True
c.ServerApp.base_url = '/jupyter/'
启动服务:
$ nohup jupyter notebook --no-browser > jupyter.log 2>&1 &
配置Nginx:
# 核心反向代理配置
location /jupyter/ {
# 将请求转发给Jupyter
proxy_pass http://127.0.0.1:8013;
# 关键:传递原始主机名、IP和协议给后端
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# WebSocket支持 (Jupyter需要)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 增加超时时间,防止长时间运行的单元格被断开
proxy_read_timeout 86400;
}
评论