frp+nginx实现内网穿透
寫(xiě)在前面:上一篇文章寫(xiě)了關(guān)于ngrok+nginx實(shí)現(xiàn)內(nèi)網(wǎng)穿透的流程,并提出了一些存在的問(wèn)題,昨天試過(guò)frp之后,將之前ngrok存在的無(wú)法映射本地域名的問(wèn)題解決了
frp 是一個(gè)可用于內(nèi)網(wǎng)穿透的高性能的反向代理應(yīng)用,支持 tcp, udp, http, https 協(xié)議。
先決條件
有一個(gè)域名,并解析到自己服務(wù)器上,如:*.frp.lestat.me
有一個(gè)具備固定ip的公網(wǎng)服務(wù)器
系統(tǒng)環(huán)境
假設(shè)環(huán)境為:
服務(wù)器OS:ubuntu17.10
客戶(hù)端OS:macOS High Sierra
以下內(nèi)容將按照上述環(huán)境進(jìn)行搭建
部署
相對(duì)于ngrok還需要編譯源碼,frp方便很多,真正的開(kāi)箱即用
服務(wù)器
下載對(duì)應(yīng)操作系統(tǒng)的frp服務(wù)端&客戶(hù)端至服務(wù)器和內(nèi)網(wǎng)電腦
wget https://github.com/fatedier/frp/releases/download/v0.16.1/frp_0.16.1_linux_amd64.tar.gz
解壓
tar zxf ./frp_0.16.1_linux_amd64.tar.gz cd ./frp_0.16.1_linux_amd64 ll
目錄結(jié)構(gòu)如下(不同版本可能有差異,但大致相似):
-rw-rw-r-- 1 kcptun kcptun 11358 Mar 21 10:11 LICENSE -rwxrwxr-x 1 kcptun kcptun 6154432 Mar 21 10:10 frpc* -rw-rw-r-- 1 kcptun kcptun 126 Mar 21 10:11 frpc.ini -rw-rw-r-- 1 kcptun kcptun 5306 Mar 21 10:11 frpc_full.ini -rwxrwxr-x 1 kcptun kcptun 7586848 Mar 21 10:10 frps* -r--r----- 1 root root 11207 Mar 25 15:38 frps.2018-03-25.log -rw-rw-r-- 1 kcptun kcptun 2127 Mar 25 13:42 frps.ini -rw-rw---- 1 root root 723 Mar 26 05:06 frps.log -rw-rw-r-- 1 kcptun kcptun 2300 Mar 21 10:11 frps_full.ini -rw------- 1 root root 0 Mar 25 11:12 nohup.out
編輯配置文件
vim ./frps.ini
可參考如下配置:
# 服務(wù)器端監(jiān)聽(tīng)客戶(hù)端連接請(qǐng)求的端口 bind_port = 7000 # 服務(wù)器端監(jiān)聽(tīng)http請(qǐng)求的端口(由于80端口被nginx占用,因此指定其他端口) vhost_http_port = 8080 # 服務(wù)器用以顯示連接狀態(tài)的站點(diǎn)端口,以下配置中可以通過(guò)訪問(wèn)IP:7500登錄查看frp服務(wù)端狀態(tài)等信息 dashboard_addr = 0.0.0.0 dashboard_port = 7500 # dashboard對(duì)應(yīng)的用戶(hù)名/密碼 dashboard_user = use dashboard_pwd = pwd # 日志文件路徑 log_file = ./frps.log # 日志記錄錯(cuò)誤級(jí)別,分為:trace, debug, info, warn, erro log_level = warn # 日志保存最大天數(shù) log_max_days = 3 # 客戶(hù)端連接校驗(yàn)碼(客戶(hù)端需與之相同) privilege_token = privilege_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 # 支持外部訪問(wèn)的域名(需要將域名解析到IP) subdomain_host = frps.lestat.me
配置nginx反向代理,將來(lái)自80端口并指向*.frp.lestat.me的請(qǐng)求分發(fā)至frp服務(wù)器http請(qǐng)求的監(jiān)聽(tīng)端口
server {
listen 80;
server_name *.frps.lestat.me;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_max_temp_file_size 0;
proxy_redirect off;
proxy_read_timeout 240s;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
啟動(dòng)frp服務(wù)器并后臺(tái)運(yùn)行,啟動(dòng)完成后可通過(guò)lsof -i :7000查看端口占用情況
nohup ./frps -c ./frps.ini &
客戶(hù)端
創(chuàng)建目錄并解壓
編輯配置文件
[common] server_addr = 服務(wù)器IP server_port = 7000 privilege_token = privilege_token auth_token = auth_token [hccrm] type = http local_port = 80 subdomain = hccrm
啟動(dòng)frp客戶(hù)端程序
./frpc -c ./frpc.ini
本地apache/nginx虛擬主機(jī)配置域名別名(alias),根據(jù)自己環(huán)境而定
mamp集成apache配置
流程圖解
假設(shè)甲使用的瀏覽器希望訪問(wèn)乙所在內(nèi)網(wǎng)環(huán)境服務(wù)器的web項(xiàng)目:
最后
嘗試使用*.frps.lestat.me訪問(wèn)站點(diǎn)看是否正常,相比ngrok的流程,frp省去了編譯,少踩不少坑,的確省事多了
總結(jié)
以上是生活随笔為你收集整理的frp+nginx实现内网穿透的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python中dump函数_python
- 下一篇: 使用Eclipse中提供的Refacto