跳转至

使用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中,执行以下命令,进入虚拟环境:

conda activate py311
执行示例:
[fotianmoyin@fotianmoyin-ecs ~]$ conda activate py311
(py311) [fotianmoyin@fotianmoyin-ecs ~]$ 
执行以下命令,配置登录密码,这样登录时都要输入这个密码才能访问,增强了安全性:
jupyter notebook password
执行示例:
(py311) [fotianmoyin@fotianmoyin-ecs ~]$ jupyter notebook password
Enter password: 
Verify password: 
[JupyterPasswordApp] Wrote hashed password to /home/fotianmoyin/.jupyter/jupyter_server_config.json
使用如下命令,打开notebook可以消除远程访问403的问题:
jupyter notebook --no-browser --ip 0.0.0.0

三、配置systemd管理notebook

编辑/etc/systemd/system/jupyter.service文件:

sudo vim /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字段指示的目录一定要存在,否则会报错。 执行如下命令,重新载入服务:
sudo systemctl daemon-reload
执行如下命令,设为开机启动:
sudo systemctl enable jupyter
执行示例如下:
[fotianmoyin@fotianmoyin-ecs system]$ sudo systemctl enable jupyter
Created symlink /etc/systemd/system/multi-user.target.wants/jupyter.service  /etc/systemd/system/jupyter.service.
执行如下命令,启动服务:
sudo systemctl start jupyter
执行如下命令,查看服务状态:
sudo systemctl status jupyter
执行示例如下:
[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文件:

sudo vim /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,页面如下: lab.fotianmoyin.com