V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  nellxzy  ›  全部回复第 1 页 / 共 1 页
回复总数  4
2021-02-14 01:51:32 +08:00
回复了 nellxzy 创建的主题 NGINX Nginx 能否将本地的多个端口转发到同一端口的不同目录
@Judoon jupyter 可以了
2021-02-14 01:48:48 +08:00
回复了 nellxzy 创建的主题 NGINX Nginx 能否将本地的多个端口转发到同一端口的不同目录
感谢大家的回复,现在有了一个初步的方案,能转发 jupyter,aria2-jsonrpc 和 aria-ng 。

配置方法:
首先, [/etc/nginx/nginx.conf] 作如下修改:
```
http {
......

server {
listen 4000;
server_name localhost;

# Jupyter Notebook
location /jupyter/ {
proxy_pass http://localhost:8888;
proxy_set_header Host $host:$server_port;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# websocket headers
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header X-Scheme $scheme;

proxy_read_timeout 120s;
proxy_next_upstream error;
}

# Aria2 JSONRPC
location /jsonrpc {
proxy_pass http://localhost:6800;
}

# Aria-NG
location /aria-ng/ {
proxy_pass http://localhost:80;
}
}
}
```

其次, [~/.jupyter/jupyter_notebook_config.py] 追加如下部分:
```
# 将 Jupyter 的工作路径改成 localhost:8888/jupyter/
# 参考 https://segmentfault.com/a/1190000016627630
c.NotebookApp.base_url = '/jupyter/'

# 允许任何源访问服务,修复用 nginx 反向代理访问时 kernel 与 terminal 无法加载的问题
# 参考 https://juejin.cn/post/6844903877481857037
c.NotebookApp.allow_origin = '*'
```

一些记录:
1. 端口之所以选 4000,是因为设置成 80 时有问题,所以就另找了一个空闲的端口。
2. location 和 proxy_pass 的写法,主要参考了这一篇: https://xuexb.github.io/learn-nginx/example/proxy_pass.html
3. jupyter 本来是工作在 [localhost:8888/] 下的,我想让它工作在 [localhost:4000/jupyter/] 下。尝试了 location 和 proxy_pass 的各种写法,以及 rewrite,没成功。某些配置下,有一种典型的现象是,访问[localhost:4000/jupyter/]时,地址跳转到[localhost:4000/tree?](对应本体的[localhost:8888/tree?]),而不是[localhost:4000/jupyter/tree?],但手动输入后者,也没法正常显示。所以退而求其次,让 jupyter 本体工作在[localhost:8888/jupyter/]下。
4. 接下来又出现了一个问题,打开 jupyter 后,虽然文件管理界面没问题,但是 kernel 和 terminal 无法加载。所以又加入了允许所有源访问服务,解决了这个问题。

概括一下:
能够工作在端口下一个目录的服务,比如
- localhost:8888/jupyter/
- localhost:6800/jsonrpc
- localhost:80/aria-ng/
可以转发。但是直接工作在端口下的服务,比如
- localhost:5299/
没弄出来。树莓派能安装的 Nginx 版本只有 1.14.2,不知道跟版本有没有关系。
2021-02-13 03:06:19 +08:00
回复了 nellxzy 创建的主题 NGINX Nginx 能否将本地的多个端口转发到同一端口的不同目录
@ashong 这边还是 404
2021-02-13 02:51:31 +08:00
回复了 nellxzy 创建的主题 NGINX Nginx 能否将本地的多个端口转发到同一端口的不同目录
@cinhoo 试了一下,两个都加 /,两个分别加#,都没成功
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2442 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 13ms · UTC 16:03 · PVG 00:03 · LAX 09:03 · JFK 12:03
Developed with CodeLauncher
♥ Do have faith in what you're doing.