本文最后更新于 2 天前,其中的信息可能已经过时,如有错误请发送邮件到 srv@isrv.cn
多服务场景:FRP + Nginx 实现统一 HTTPS 域名管理
多服务场景:FRP + Nginx 实现统一 HTTPS 域名管理
在日常使用 NAS、树莓派、内网开发机等设备时,常常会遇到一个问题:
这些机器没有公网 IP,没办法直接配置 HTTPS 域名。
这时候 FRP 内网穿透 就能派上用场。
本文将介绍如何使用 FRP + Nginx,实现多个服务共享一个 VPS,对外暴露不同的 HTTPS 域名。
一、架构说明
整体流程如下:[本地服务] → frpc → [VPS frps:8080] ← Nginx ← 用户浏览器(https://xxx.example.com)
- 客户端 (frpc):运行在内网机器(NAS、开发机),只需配置
frpc,不用管证书。 - 服务端 (frps):运行在 VPS,接收 frpc 的请求。
- Nginx:运行在 VPS,统一做 HTTPS + 域名转发,管理证书。
- 最终效果:不同服务对应不同域名,均可通过 HTTPS 安全访问。
二、服务端 frps 配置
编辑 /etc/frp/frps.toml:bindPort = 7000 # 接收 HTTP 流量的端口(frpc -> frps) vhostHTTPPort = 8080
注意:不需要
vhostHTTPSPort,因为 HTTPS 统一交给 Nginx。
启动服务端:frps -c /etc/frp/frps.toml
三、客户端 frpc 配置
假设你的 NAS 上有两个服务:
- 博客服务:
127.0.0.1:6699 - 相册服务:
127.0.0.1:8899
frpc.toml:serverAddr = "vps.example.com" serverPort = 7000 # 博客 [[proxies]] name = "blog" type = "http" localIP = "127.0.0.1" localPort = 6699 customDomains = ["blog.example.com"] # 相册 [[proxies]] name = "photos" type = "http" localIP = "127.0.0.1" localPort = 8899 customDomains = ["photos.example.com"]
启动 frpc:frpc -c frpc.toml
四、Nginx 配置(VPS)
VPS 上的 Nginx 负责 443 端口的证书,并根据域名转发到 frps 的 8080。
博客站点 /etc/nginx/sites-enabled/blog.conf
server { listen 80; listen 443 ssl http2; server_name blog.example.com; ssl_certificate /etc/letsencrypt/live/blog.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/blog.example.com/privkey.pem; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
相册站点 /etc/nginx/sites-enabled/photos.conf
server { listen 80; listen 443 ssl http2; server_name photos.example.com; ssl_certificate /etc/letsencrypt/live/photos.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/photos.example.com/privkey.pem; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }