You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Nginx Ingress访问JupyterLab返回404错误问题求助

问题根因

当前配置存在两个核心问题:

  1. Nginx Ingress将匹配/notebooks/default/nb-notebook-sample前缀的请求重写为/后转发给JupyterLab,JupyterLab收到根路径请求后会返回302跳转到/lab,但跳转地址未携带路径前缀,浏览器最终访问的http://{nginx-ip}/lab不匹配Ingress规则,触发404。
  2. 即便手动修改rewrite-target为/lab,JupyterLab后续发起的静态资源请求、接口调用依然会使用根路径,依旧会出现404错误。

解决方案(优先推荐)

步骤1:修改JupyterLab启动配置

官方Jupyter镜像支持通过base_url参数配置服务的根路径前缀,配置后所有跳转链接、资源路径都会自动带上该前缀。你需要修改JupyterLab Pod的启动参数,添加对应配置:

containers:
- name: jupyter-lab
  image: jupyter/tensorflow-notebook:latest
  args:
  - jupyter
  - lab
  - --LabApp.base_url=/notebooks/default/nb-notebook-sample
  # 其余原有配置保持不变

也可以通过环境变量JUPYTER_LAB_BASE_URL=/notebooks/default/nb-notebook-sample实现相同效果,无需修改启动命令。

步骤2:修改Ingress配置

删除原有的nginx.ingress.kubernetes.io/rewrite-target: /注解即可,现在JupyterLab本身就期待接收带前缀的请求,不需要Ingress做路径重写:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  labels:
    notebook.kubedl.io/notebook-name: notebook-sample
  name: nb-notebook-sample
  namespace: default
spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: nb-notebook-sample
            port:
              name: nb-port
        path: /notebooks/default/nb-notebook-sample
        pathType: Prefix

配置更新完成后,重启JupyterLab Pod、更新Ingress规则,再次访问目标地址即可正常跳转加载页面。


备选方案(无需修改Jupyter配置)

如果暂时无法修改JupyterLab启动参数,可以通过调整Ingress规则适配:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header X-Forwarded-Prefix "/notebooks/default/nb-notebook-sample";
  labels:
    notebook.kubedl.io/notebook-name: notebook-sample
  name: nb-notebook-sample
  namespace: default
spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: nb-notebook-sample
            port:
              name: nb-port
        path: /notebooks/default/nb-notebook-sample(/|$)(.*)
        pathType: Prefix

该方案通过路径捕获组将前缀后的内容转发给Jupyter根路径,同时添加代理头告知Jupyter服务的访问前缀,兼容性略低于第一种方案。


内容的提问来源于stack exchange,提问作者Jian He

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 11:15:04