参考

https://blog.mrabit.com/details/75

公网服务器准备

首先准备一台有公网IP的服务器,可以选用阿里云或者腾讯云,如果选用的是阿里云的,可以使用我的优惠链接购买

内网穿透frp搭建

frp 安装程序:https://github.com/fatedier/frp/releases

frp服务端搭建

服务端搭建在上一步准备的公网服务器上,因为服务器是centos7 x64的系统,因此,这里下载安装包版本为linux_amd64的 frp_0.27.0_linux_amd64.tar.gz

  1. 下载安装包

    1
    $ wget https://github.com/fatedier/frp/releases/download/v0.27.0/frp_0.27.0_linux_amd64.tar.gz
  2. 解压安装包

    1
    $ tar -zxvf frp_0.27.0_linux_amd64.tar.gz
  3. 修改配置文件

    1
    2
    3
    4
    5
    6
    $ cd frp_0.27.0_linux_amd64
    $ vim frps.ini

    [common]
    bind_port = 7100
    vhost_http_port = 7200
  4. 启动frp服务端

    1
    2
    3
    4
    $ ./frps -c frps.ini
    2019/06/15 16:42:02 [I] [service.go:139] frps tcp listen on 0.0.0.0:7100
    2019/06/15 16:42:02 [I] [service.go:181] http service listen on 0.0.0.0:7200
    2019/06/15 16:42:02 [I] [root.go:204] Start frps success

frp客户端搭建

客户端搭建在本地的Mac上,因此下载安装包版本为darwin_amd64的 frp_0.27.0_darwin_amd64.tar.gz

  1. 下载安装包

    1
    $ wget https://github.com/fatedier/frp/releases/download/v0.27.0/frp_0.27.0_darwin_amd64.tar.gz
  2. 解压安装包

    1
    $ tar -zxvf frp_0.27.0_darwin_amd64.tar.gz
  3. 修改配置文件,配置服务端ip端口及监听的域名信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $ cd frp_0.27.0_darwin_amd64
    $ vim frpc.ini

    [common]
    server_addr = 120.92.169.103
    server_port = 7100

    [web]
    type = http
    local_port = 8080
    custom_domains = oauth.xkcoding.com
  4. 启动frp客户端

    1
    2
    3
    4
    $ ./frpc -c frpc.ini
    2019/06/15 16:48:52 [I] [service.go:221] login to server success, get run id [8bb83bae5c58afe6], server udp port [0]
    2019/06/15 16:48:52 [I] [proxy_manager.go:137] [8bb83bae5c58afe6] proxy added: [web]
    2019/06/15 16:48:52 [I] [control.go:144] [web] start proxy success

配置域名解析

前往阿里云DNS解析,将域名解析到我们的公网服务器上,比如我的就是将 oauth.xkcoding.com -> 120.92.169.103

nginx代理

nginx 的搭建就不在此赘述了,只说配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 80;
server_name oauth.xkcoding.com;

location / {
proxy_pass http://127.0.0.1:7200;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_buffering off;
sendfile off;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_temp_file_write_size 64k;
proxy_http_version 1.1;
proxy_request_buffering off;
}
}

测试配置文件是否有问题

1
2
3
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载配置文件,使其生效

1
$ nginx -s reload

现在当我们在浏览器输入 oauth.xkcoding.com 的时候,网络流量其实会经历以下几个步骤:

  1. 通过之前配的DNS域名解析会访问到我们的公网服务器 120.92.169.103 的 80 端口
  2. 再经过 nginx,代理到服务器的 7200 端口
  3. 再经过 frp 穿透到我们的 Mac 电脑的 8080 端口
  4. 此时 8080 就是我们的应用程序端口

注意

  • 远程电脑账号请勿使用administrator账号
  • 远程电脑密码尽量不要使用简单密码,尽量使用字母,符号,数字组合