[FRP] frp+nginx实现端口共用和内网穿透

一、背景

​ 本文的目标是在ubuntu20.04下实现多个服务对外共用一个端口,且实现内网穿透;

二、前置准备

  • 服务端:服务器1台(有公网IP:254.215.2.21);
  • 客户端:普通电脑1台(可以访问互联网,IP为:192.168.100.120);
  • frp脚本部署文件(frp的部署和安装见《[FRP] 初探》);
  • 普通电脑下载并部署好nginx服务;

三、配置NGINX

目的:实现同一端口代理多个服务,修改nginx.conf脚本内容如下

#user  nobody;
worker_processes  auto;
worker_rlimit_nofile 1048570;

error_log   /dev/null;

events {
    use epoll;
    worker_connections 65535;
    multi_accept on;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

	#access_log  logs/access.log  main;
	access_log      off;
	sendfile        on;
	
	add_header Access-Control-Allow-Origin *;
	add_header Access-Control-Allow-Headers X-Requested-With;
	add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
	keepalive_timeout  65;
	
	server {
	    listen       8060;
	    server_name  localhost;
	    location / {
	        root   html;
	        index  index.html index.htm;
	    }
	
	    location /service1 {
	        proxy_pass http://192.168.100.123:8030;
	    }
	    location /service2 {
	        proxy_pass http://192.168.100.123:8090;
	    }
	
	    error_page   500 502 503 504  /50x.html;
	    location = /50x.html {
	        root   html;
	    }
	}

}

重启nginx后,就可以:

使用http://192.168.100.120:8060/service1访问服务1,实际访问代理地址http://192.168.100.123:8030/service1

使用http://192.168.100.120:8060/service2访问服务2,实际访问代理地址http://192.168.100.123:8090/service2

四、 对NGINX端口进行内网穿透

1.FRP服务端配置

1.1.配置Frps.ini文件
[common]
bind_addr = 0.0.0.0
bind_port = 7000
vhost_http_port = 7500
tcpmux_httpconnect_port = 1337
tcp_mux = true

dashboard_addr = 0.0.0.0
dashboard_port = 7501
dashboard_user = admin
dashboard_pwd = admin
enable_prometheus = true

detailed_errors_to_client = true

authentication_method = token
authenticate_heartbeats = true
authenticate_new_work_conns = true
token = 12345678

custom_404_page=/path/to/404.html
1.2.启动命令
./frps -c frps.ini
1.3.访问监控页面
http://192.168.100.120:7501,密码帐号均为admin

2.FRP客户端配置

2.1.内网机器配置Frpc.ini
[common]
server_addr = 127.0.0.1
server_port = 7000

authenticate_heartbeats = true
authenticate_new_work_conns = true
token = 12345678

admin_addr = 127.0.0.1
admin_port = 7502
admin_user = admin
admin_pwd = admin

tcp_mux = true
user = msos
protocol = tcp

[nginx]
type = http
local_ip = 127.0.0.1
local_port = 8060
use_encryption = false
use_compression = true
http_user = admin
http_pwd = admin
custom_domains = 127.0.0.1

2.2.启动命令
./frpc -c frpc.ini
2.3.访问监控页面
http://192.168.100.120:7502,密码帐号均为admin

五、NGINX内网穿透

使用http://254.215.2.21:8060/service1访问服务1,实际访问代理地址http://192.168.100.123:8030/service1

使用http://254.215.2.21:8060/service2访问服务2,实际访问代理地址http://192.168.100.123:8030/service2

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酷咪哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值