前言
搭建 frp 实现内网穿透需要用到带公网 IP 的服务器,一般都使用 VPS 来部署 frp 的服务端 frps。可以通过 frp 实现 tcp、http 和 https 的内网穿透;通过设置frps
配置文件中vhost_http_port = 80
、vhost_https_port = 443
可以实现通过80
、443
默认端口来访问已经穿透的服务,即浏览器访问时只需填写域名而无需填写端口号。但是当 VPS 上部署有其他服务时,由于端口不能同时被多个进程占用,所以以上配置无法实现通过默认端口同时访问 VPS 上部署的服务和 fps 穿透的服务。
本文记录设置 Nginx 反向代理 dashboard 和 frps,实现 nginx 和 frp 复用 443 端口,实现使用不同域名通过 VPS 的 443 端口访问 frp 的 dashboard 服务和 frp 穿透的 Nextcloud 服务。
OS:CentOS Linux release 7.7
Nginx:nginx/1.16.1
frp:0.32.0
配置 frp
frps 配置如下
[common]
bind_addr = 0.0.0.0
bind_port = 7000
bind_udp_port = 7001
kcp_bind_port = 7000
vhost_https_port = 4443
allow_ports = 2000-3000,3001,3003,4000-50000
max_pool_count = 5
max_ports_per_client = 0
tcp_mux = true
authenticate_new_work_conns = true
authenticate_heartbeats = true
authentication_method = token
token = 123
dashboard_port = 7002 #访问 dashboard 的端口
dashboard_user = admin #dashboard 用户名
dashboard_pwd = admin #dashboard 密码
frpc 配置如下
[common]
server_addr = x.x.x.x #填写公网服务器 IP
server_port = 7000
authenticate_new_work_conns = true
authenticate_heartbeats = true
authentication_method = token
token = 123
pool_count = 1
[nextcloud]
type = https
local_port = 443
custom_domains = test2.mtycho.com
use_compression = true
使用以上 frps 和 frpc 配置,通过域名 + 4443/tcp 端口即可通过 https 访问 Nextcloud,Nextcloud 已经部署了 SSL 证书。
此时访问 Nextcloud 据流向为:
4443/tcp ——> frps ——> frpc ——> 443/tcp ——> Nextcloud
配置 Nginx
安装 Nginx:
sudo yum install -y nginx
sudo systemctl enable nginx
sudo service nginx start
新建证书文件夹:
sudo mkdir /etc/nginx/cert
配置 dashboard 反向代理
frp 的 dashboard 已经在 frps 配置文件中开启,现在要做的是配置 Nginx,实现 https 访问 dashboard。
这里以test1.mtycho.com
域名为例,为域名申请 Nginx 版本的证书,博主是在阿里云申请的免费证书,这里不再赘述。
下载证书后得到两个文件(下载的证书文件前有一串数字,为方便使用博主这里已经删除了):
- test1.mtycho.com.pem
- test1.mtycho.com.key
然后把证书文件上传到/etc/nginx/cert
证书文件夹。
接下来编辑 Nginx 配置文件:
sudo vi /etc/nginx/nginx.conf
删除默认的 server 字段,在 http 字段中添加如下 ssl 配置:
server {
listen 443 ssl http2;
server_name test1.mtycho.com;
ssl_certificate "/etc/nginx/cert/test1.mtycho.com.pem";
ssl_certificate_key "/etc/nginx/cert/test1.mtycho.com.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://127.0.0.1:7002/;
proxy_set_header Host test1.mtycho;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
然后配置 http 重定向到 https。
在/etc/nginx/nginx.conf
配置文件 http 字段中添加如下配置:
server {
listen 80;
server_name test1.mtycho.com;
rewrite ^(.*)$ https://$host$1 permanent; #http 重定向到 https
location / {
index index.html index.htm;
}
}
重启 Nginx:
sudo service nginx restart
到这里,frp 的 dashboard 已经配置完成,现在已经可以通过 https://test1.mtycho.com 访问 dashboard 了。
访问 dashboard 的数据流向为:
443/tcp ——> Nginx ——> 7002/tcp ——> dashboard
若使用 firewall 防火墙, 7002 端口可以不用管
配置 frps 反向代理
这里以test2.mtycho.com
域名为例,把 Nextcloud 域名对应的证书文件上传到/etc/nginx/cert
证书文件夹,这里不再赘述。
在/etc/nginx/nginx.conf
配置文件 http 字段中添加如下配置:
server {
listen 443 ssl;
server_name test2.mtycho.com;
ssl_certificate "/etc/nginx/cert/test2.mtycho.com.pem";
ssl_certificate_key "/etc/nginx/cert/test2.mtycho.com.key";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
resolver 8.8.8.8;
proxy_ssl_server_name on;
proxy_set_header Host $host;
proxy_pass https://$host:4443;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
然后配置 http 重定向到 https。
在/etc/nginx/nginx.conf
配置文件 http 字段中添加如下配置:
server {
listen 80;
server_name test2.mtycho.com;
rewrite ^(.*)$ https://$host$1 permanent; #http 重定向到 https
location / {
index index.html index.htm;
}
}
重启 Nginx:
sudo service nginx restart
到这里,frps 反向代理已经配置完成,现在已经可以通过 https://test2.mtycho.com 外网访问 Nextcloud 了。
此时访问 Nextcloud 的数据流向为:
443/tcp ——> Nginx ——> 4443/tcp ——> frps ——> frpc ——> 443/tcp ——> Nextcloud
结语
以上介绍配置 Nginx 反向代理 dashboard 和 frp 穿透的 Nextcloud,同理,可以反向代理 http 以及其它 frp 穿透的服务。
Nginx 代理 frp 服务时,proxy_pass
为 frp 服务地址,区分HTTPS 和 HTTP,如果穿透的服务开启了 HTTPS 则需要使用 https,否则使用 http。