有時需要把內網部署的站點映射到外網,讓其別人也能訪問到,例如做微信相關開發時需要把內網映射到外網,讓微信服務器能訪問到你的開發環境,方便開發調試。可以使用花生殼或者ngrok,前面這兩種我都使用過,花生殼用起來很不穩定,花了錢還不能解決問題,ngrok現在已經很久沒更新了,國內的可以在https://www.ngrok.cc注冊,免費用戶僅能使用一個轉發,由于是使用香港的服務器經常連接不上,最后狠下心來研究了下內網穿透,發現frp使用起來還可以,本文就介紹在iMac下如何用阿里云用frp做內網穿透,Windows的應該流程也類似。
frp開源地址:https://github.com/fatedier/frp
一.服務器配置
1.使用終端ssh遠程連接阿里云服務器
ssh -t root@xxx.xxx.xxx.xxx -p 22
進入到/home目錄,創建文件夾
mkdir frp
2.下載對應的frp發布版本
先查看服務器型號
cat /proc/version
本人服務器信息:
Linux version 4.4.0-101-generic (buildd@lcy01-amd64-006)
下載frp
sudo wget https://github.com/fatedier/frp/releases/download/v0.13.0/frp_0.13.0_linux_amd64.tar.gz
我的阿里云服務器下載不了frp文件,就先下載文件到本地,然后用ssh遠程上傳到阿里云服務器。
scp -r frp_0.13.0_linux_amd64.tar.gz root@xxx.xxx.xxx.xxx:/home/frp/
3.配置frp服務端
解壓到當前目錄
sudo tar xvf frp_0.13.0_linux_amd64.tar.gz
解壓之后進入目錄會看到以下文件
# ls
LICENSE frpc frpc.ini frpc_full.ini frps frps.ini frps_full.ini
打開frps.ini文件,配置成如下:
bind_addr = xxx.xxx.xxx.xxx
bind_port = 7000
vhost_http_port = 80
vhost_https_port = 443
dashboard_port = 7500
dashboard_user = 你的儀表盤用戶名
dashboard_pwd = 你的儀表盤密碼
privilege_token = frp
其中bind_port是自己設定的frp服務端端口
vhost_http_port是自己設定的http訪問端口
vhost_https_port是自己設定的https訪問端口
[ssh]部分是ssh反向代理
listen_port是自己設定的ssh訪問端口
custom_domains是自定義域名,如果有自己的域名就寫到這里
privilege_token是驗證憑據,服務端和客戶端的憑據必須一樣才能連接,當然為了安全還是設置長一點。dashborad的三個配置是儀表盤功能的端口以及用戶名和密碼,為了安全也要設置的長一點。
保存上面的配置后,使用以下指令啟動frp服務端。
./frps -c ./frps.ini
查看frps.log然后應該會顯示類似下面的文本,說明服務端啟動成功
Start frps success
PrivilegeMode is enabled, you should pay more attention to security issues
二.客戶端配置
iMac下載對應的frp版本frp_0.13.0_darwin_amd64.tar.gz
解壓后有對應的文件
# ls
LICENSE frpc frpc.ini frpc_full.ini frps frps.ini frps_full.ini
frpc和frpc.ini、frpc_full.ini 是客戶端對應的配置文件
我的frpc.ini配置對應如下:
[common]
server_addr = xxx.xxx.xxx.xxx
server_port = 7000
# console or real logFile path like ./frpc.log
log_file = ./frpc.log
# trace, debug, info, warn, error
log_level = trace
log_max_days = 3
# for authentication
privilege_token = #######
# connections will be established in advance, default value is zero
pool_count = 5
# if tcp stream multiplexing is used, default is true, it must be same with frps
tcp_mux = true
# your proxy name will be changed to {user}.{proxy}
user = qzcool
# decide if exit program when first login failed, otherwise continuous relogin to frps
# default is true
login_fail_exit = true
[weixin]
type = http
local_ip = 127.0.0.1
local_port = 8080
use_encryption = false
use_compression = false
subdomain = weixin
server_addr:對應的時阿里云的公網地址
server_port:服務端端口
privilege_token:token需要和服務端一致
最底下是配置站點信息
subdomain:子域名,假設服務器配置泛解析,子域名配置weixin,那么可以通過weixin.youdomin.com訪問到你內網的8080端口
啟動服務 :
macdeiMac:frp_0.13.0_darwin_amd64 fred$ sudo ./frpc -c ./frpc.ini
然后使用瀏覽器訪問weixin.youdomin.com就能訪問到你的站點了。
frp開機啟動和后臺運行
使用systemd配置開機自啟,適用于 centos7 Ubuntu 16 或 debian 8。
vi /etc/systemd/system/frps.service 新建此文件,并寫入以下內容:
[Unit]
Description=frps daemon
After=syslog.target network.target
Wants=network.target
[Service]
Type=simple
ExecStart=/root/frp_0.13.0_linux_amd64/frps -c /root/frp_0.13.0_linux_amd64/frps_my.ini
Restart= always
RestartSec=1min
[Install]
WantedBy=multi-user.target
啟動并設為開機自啟。
systemctl start frps //啟動
systemctl status frps //狀態查詢
systemctl enable frps //開機啟動