通过自定义域名访问内网的Web服务
HTTP 类型的代理相比于 TCP 类型,不仅在服务端只需要监听一个额外的端口 vhost_http_port
用于接收 HTTP 请求,还额外提供了基于 HTTP 协议的诸多功能。
1.修改frps.ini文件,设置监听HTTP请求端口8080:
server端
[common]
bind_port = 7000
vhost_http_port = 8080
2.修改 frpc.ini 文件,假设 frps 所在的服务器的 IP 为 x.x.x.x,local_port
为本地机器上 Web 服务监听的端口, 绑定自定义域名为 custom_domains
。
client端
[common]
server_addr = x.x.x.x
server_port = 7000
[web]
type = http
local_port = 80
custom_domains = www.yourdomain.com
[web2]
type = http
local_port = 8080
custom_domains = www.yourdomain2.com
3.分别启动frps和frpc。
4.将 www.yourdomain.com
和 www.yourdomain2.com
的域名 A 记录解析到 IP x.x.x.x
,如果服务器已经有对应的域名,也可以将 CNAME 记录解析到服务器原先的域名。或者可以通过修改 HTTP 请求的 Host 字段来实现同样的效果。
5.通过浏览器访问 http://www.yourdomain.com:8080
即可访问到处于内网机器上 80 端口的服务,访问 http://www.yourdomain2.com:8080
则访问到内网机器上 8080 端口的服务。
以上是FRP官网给出的配置,比较简单如果需要加入一些别的,或者没有域名配置内网web,我在下面也列举了一些别的配置。
server端
[common]
#frp服务器监听地址,如果是IPV6地址必须用中括号包围
bind_addr = 0.0.0.0
#frp服务器监听端口
bind_port = 7000
#udp端口用于kcp协议,它可以与'bind_port'相同,如果没有设置,kcp在frps中是禁用的
kcp_bind_port = 7000
#指定使用的协议,默认tcp,可选kcp
protocol = tcp
#指定哪个地址代理将监听,默认值与bind_addr相同
#proxy_bind_addr = 127.0.0.1
#虚拟主机穿透监听端口(指http与https的访问端口)
vhost_http_port = 80
vhost_https_port = 443
#这里需要注意的是如果下面client用的tcp代理的web服务需要注释这个vhost_http_port。
#响应头超时(秒)对于vhost http服务器,默认为60秒
#vhost_http_timeout = 60
#开启普罗米修斯监控
enable_prometheus = true
#frp进行http映射设置域名
#例如:我的域名为:myit.icu
#教程:
#第一步:
#添加第一条A解析记录
#主机记录:frp
#记录类型:A
#记录值:你的frp服务端IP
#第二步:
#主机记录:*.frp
#记录类型:CNAME
#记录值:frp.myit.icu
#subdomain_host = frp.myit.icu
client端
[common]
server_addr = 192.168.4.21
server_port = 7000
auth_token = 123
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
[web01]
#http类型的内网穿透,必须设置vhost_http_port,
#并且所有的http类型的客户端都将通过同一个vhost_http_port访问。这里因为我没域名所以直接用的ip,协议不能使用http了,这里写tcp,而且必须要remote_port
type = tcp
local_ip = 127.0.0.1
local_port = 80
remote_port= 80
#custom_domains = web.zhishi.com
多个web服务配置
如果想配置多个web服务怎么 办,跟ssh类似,添加多个[web]即可,注意不能重名
服务端不需要做任何修改,修改ftpc.ini内容如下
- [web]表示我们的配置是一个web服务
- type表示我们的请求是http方式
- local_port表示我们的本地服务端口号为80
- custom_domainx表示配置为一个已经备案的域名(必填,并且需要域名可用)婢妾域名解析配置为外网服务器ip
client端
[common]
server_addr = 39.105.97.50
server_port = 7000
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6008
[web01]
type = http
local_port = 8080
custom_domains = www.chendahai.cn
[web02]
type = http
local_port = 80
custom_domains = cd.chendahai.cn # 使用二级域名进行配置
之后访问内网的80端口web服务可以通过cd.chendahai.cn:6001就可以访问了,二级域名是需要配置域名解析的。
如果需要强制跳转https可在/etc/httpd/conf/httpd.conf中添加如下代码。
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]
如果需要既可以http访问也可以https访问可以在frpc.ini中多添加一个web(注意不要重名)
client端
... ...
[web01]
#http类型的内网穿透,必须设置vhost_http_port,
#并且所有的http类型的客户端都将通过同一个vhost_http_port访问。
type = tcp
local_ip = 127.0.0.1
local_port = 443
remote_port= 443
#custom_domains = web.zhishi.com
[web02]
#http类型的内网穿透,必须设置vhost_http_port,
#并且所有的http类型的客户端都将通过同一个vhost_http_port访问。
type = tcp
local_ip = 127.0.0.1
local_port = 80
remote_port= 80
... ...