要实现frps服务器与Web服务共用80端口,可以通过Nginx的反向代理功能来实现。以下是具体配置方案:
server {
listen 80;
server_name frp.example.com; # frp专用子域名
# frp的http穿透流量
location / {
proxy_pass http://127.0.0.1:7000; # frps默认http端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name example.com www.example.com; # 网站主域名
# 正常的web服务配置
location / {
root /var/www/html;
index index.html index.htm;
}
}
确保frps.ini中http端口设置为7000(或其他非80端口):
[common]
bind_port = 7000
vhost_http_port = 7000
如果无法使用不同域名,可以通过路径区分:
server {
listen 80;
server_name example.com;
# web服务
location / {
root /var/www/html;
index index.html index.htm;
}
# frp服务路径
location /frp/ {
proxy_pass http://127.0.0.1:7000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
如果frps需要处理TCP流量:
stream {
# frps的TCP流量
server {
listen 80;
proxy_pass 127.0.0.1:7000;
}
}
http {
# 正常的web服务配置
server {
listen 8080;
server_name example.com;
location / {
root /var/www/html;
index index.html index.htm;
}
}
}
注意:此方案需要Nginx 1.9.0+版本支持stream模块
nginx -t && systemctl restart nginx
tail -f /var/log/nginx/error.log
选择哪种方案取决于您的具体需求,基于域名分流是最清晰和推荐的方式。