frp内网穿透 nginx反向代理教程

NAS没有公网IP是一件很不方便的事情,尤其是在国内的网络环境,学校和小区内的用户通常都没有公网IP。为了解决这个问题,则需要内网穿透,而内网穿透的方法有很多种,例如使用花生壳、ngrok等,该文章要介绍的是使用frp让本地web实现内网穿透。

实际上frp有官方的中文文档,上面的内容已经非常详尽,对相关操作比较熟悉的人可以直接阅读官方的中文文档

什么是frp
frp 是一个高性能的反向代理应用,可以帮助您轻松地进行内网穿透,对外网提供服务,支持 tcp, http, https 等协议类型,并且 web 服务支持根据域名进行路由转发。

准备
在使用frp之前,需要一台有公网IP的服务器(下文称外网主机),一台需要实现内网穿透的机器(下文称内网主机),SSH工具,以及一个域名(如果只是建立SSH反向代理则不需要域名)。

开始使用
根据机器的操作系统,在Release页面中找到对应的frp程序,然后分别在外网主机和内网中下载它。

外网主机

上面的配置和服务端是对应的。


 服务器端监听客户端连接请求的端口
bind_port = 7000

# 服务器端监听http请求的端口(由于80端口被nginx占用,因此指定其他端口)
vhost_http_port = 10080 

# 服务器用以显示连接状态的站点端口,以下配置中可以通过访问IP:7500登录查看frp服务端状态等信息
dashboard_addr =xxx.xx.xx.xx
dashboard_port = 7500 服务器端面板端口号

# dashboard对应的用户名/密码
dashboard_user = use
dashboard_pwd = pwd

# 日志文件路径
log_file = ./frps.log

# 日志记录错误级别,分为:trace, debug, info, warn, erro
log_level = warn

# 日志保存最大天数
log_max_days = 3

# 客户端连接校验码(客户端需与之相同)
token = 密钥

# heartbeat configure, it's not recommended to modify the default value
# the default value of heartbeat_timeout is 90
# heartbeat_timeout = 90

# only allow frpc to bind ports you list, if you set nothing, there won't be any limit
# privilege_allow_ports = 2000-3000,3001,3003,4000-50000

# pool_count in each proxy will change to max_pool_count if they exceed the maximum value
max_pool_count = 5

# max ports can be used for each client, default value is 0 means no limit
max_ports_per_client = 0

# authentication_timeout means the timeout interval (seconds) when the frpc connects frps
# if authentication_timeout is zero, the time is not verified, default is 900s
authentication_timeout = 900

# 支持外部访问的域名(需要将域名解析到IP)
subdomain_host = xxx.xxxx.com

保存配置,输入以下指令运行frp客户端。(同样如果需要在后台运行,请往下翻阅关于后台运行的部分。)

./frps -c ./frps.ini



[common]
server_addr = 服务器IP
server_port = 7000
token = 密钥


[web]

type = http


#http或https协议

local_ip = 本地IP


#本地地址

local_port = 80

#网站端口

subdomain = guogege
custom_domains = xxx.xxx.com
#配置二级域名

#test可以自己定义,不要和其他人冲突

./frpc -c ./frpc.ini
此时在服务端会看到”start proxy sucess”字样,即连接成功。

使用nohup指令
服务端:

nohup ./frps -c ./frps.ini &
客户端:

nohup ./frpc -c ./frpc.ini &
这样就成功让frp在后台运行了。

nginx反向代理配置


 
server {
    listen 80;
    server_name xxx.xxx.com;
  
    location / {

        #root   html;

           #index  index.html index.htm;
        proxy_ssl_server_name on;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host:$server_port;
        proxy_pass http://127.0.0.1:10080; #通过域名访问 frp 服务
         
    }

结语
frp的使用和配置相当简单,如有疑问或者想了解关于frp的其它功能可以查看官方的中文文档

此处评论已关闭

[player id="719102"/]