使用Nginx反向代理notebook
约 258 个字 63 行代码 预计阅读时间 2 分钟
一、相关参数
- 操作系统:Rocky Linux 9.3
- Nginx:1.20.1
- Anaconda:Anaconda3-2023.09-0-Linux-x86_64
- Python:3.11
- jupyter notebook:7.0.8
二、配置notebook
我的notebook
是安装在anaconda
虚拟环境py311
中,执行以下命令,进入虚拟环境:
(py311) [fotianmoyin@fotianmoyin-ecs ~]$ jupyter notebook password
Enter password:
Verify password:
[JupyterPasswordApp] Wrote hashed password to /home/fotianmoyin/.jupyter/jupyter_server_config.json
三、配置systemd管理notebook
编辑/etc/systemd/system/jupyter.service
文件:
[Unit]
Description=Jupyter Notebook
After=syslog.target network.target
[Service]
User=fotianmoyin
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/fotianmoyin/.local/bin:/home/fotianmoyin/anaconda3/envs/py311/bin"
WorkingDirectory=/home/fotianmoyin/lab/
ExecStart=/home/fotianmoyin/ananconda3/envs/py311/bin/jupyter notebook --ip=0.0.0.0 --no-browser
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
WorkingDirectory
字段指示的目录一定要存在,否则会报错。
执行如下命令,重新载入服务:
执行如下命令,设为开机启动:
执行示例如下:
[fotianmoyin@fotianmoyin-ecs system]$ sudo systemctl enable jupyter
Created symlink /etc/systemd/system/multi-user.target.wants/jupyter.service → /etc/systemd/system/jupyter.service.
[fotianmoyin@fotianmoyin-ecs system]$ sudo systemctl status jupyter
● jupyter.service - Jupyter Notebook
Loaded: loaded (/etc/systemd/system/jupyter.service; enabled; preset: disabled)
Active: activating (auto-restart) (Result: exit-code) since Sat 2024-02-24 23:29:09 >
Process: 57972 ExecStart=/home/fotianmoyin/ananconda3/envs/py311/bin/jupyter notebook>
Main PID: 57972 (code=exited, status=203/EXEC)
CPU: 2ms
四、配置Nginx,添加反向代理
编辑/etc/nginx/conf.d/lab.fotianmoyin.conf
文件:
server {
listen 80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /home/fotianmoyin/.acme.sh/fotianmoyin.com_ecc/fullchain.cer;
ssl_certificate_key /home/fotianmoyin/.acme.sh/fotianmoyin.com_ecc/fotianmoyin.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
server_name lab.fotianmoyin.com;
location / {
proxy_pass http://localhost:8888;
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 "upgrade";
proxy_redirect off;
}
}
https://lab.fotianmoyin.com
,页面如下: