[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

你可能感兴趣的:(FRP,内网穿透,共用端口,NGINX,FRP,nginx)