<br />
<br />
需求
使用一个域名 nas.example.com:8080
访问不同的服务,例如 nas.example.com:8080/webman
访问迅雷, nas.example.com:8080/qbt
访问qBittorrent。减少暴露在外网的端口数,防止某些无需认证的服务暴露在外网中,并且只需映射一个域名到nas上
准备工作
- 能外网访问的nas
- 能使用ssh的软件或工具
- 会使用 vim
配置防火墙
根据自身情况放行端口,此处我使用了nas自带的防火墙,关闭了主路由的防火墙,并且只放行了 5666、5667、8080
三个端口

连接nas并安装nginx
打开ssh工具连接nas, 飞牛系统是debian,输入密码时不会显示密码,输入完之后回车确认即可

更新软件包列表,输入 sudo apt update

安装nginx, 输入 sudo apt install nginx
(这里我已经安装过,与未安装的显示是不一样的)

建议先备份nginx的配置文件,如
sudo cp /etc/nginx/nginx.conf ./nginx.conf
sudo cp /etc/nginx/sites-enabled/default ./default
配置nginx
sudo vim /etc/nginx/sites-enabled/default
并且在
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
的后面添加如下内容
location /qbt/ {
# qBittorrent的地址
proxy_pass http://127.0.0.1:8085/;
proxy_http_version 1.1;
# headers recognized by qBittorrent
proxy_set_header Host $proxy_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cookie_path / "/; Secure";
}
location /webman {
# 迅雷的地址
proxy_pass http://127.0.0.1:12345;
# 关键代理头
proxy_set_header Host '$host:$server_port';
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;
}
完整的default 大概是这样
server {
listen 8080 default_server;
listen [::]:8080 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /qbt/ {
proxy_pass http://127.0.0.1:8085/;
proxy_http_version 1.1;
# headers recognized by qBittorrent
proxy_set_header Host $proxy_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
# optionally, you can adjust the POST request size limit, to allow adding a lot of torrents at once
#client_max_body_size 100M;
# No longer required since qBittorrent v5.1:
# Since v4.2.2, is possible to configure qBittorrent
# to set the "Secure" flag for the session cookie automatically.
proxy_cookie_path / "/; Secure";
}
location /webman {
proxy_pass http://127.0.0.1:12345;
# 关键代理头
proxy_set_header Host '$host:$server_port';
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;
}
}
保存退出后, 重载nginx
sudo nginx -s reload
效果
访问 nas.example.com:8080/webman

访问 nas.example.com:8080/qbt

参考链接
https://github.com/qbittorrent/qBittorrent/wiki/NGINX-Reverse-Proxy-for-Web-UI