首先你需要有一台公网服务器和域名
新建frps_http.ini配置
[common]
bind_addr = 0.0.0.0
bind_port = 7000
vhost_http_port = 8081 #监听本地8081端口
启动frps
./frps -c ./frps_http.ini
本机启动spring boot项目以若依为例后端端口为8080
添加本机frpc配置
[common]
server_addr = youIp
server_port = 7000
[web]
type = http
local_port = 8080 #转发到本地8080端口
custom_domains = www.xxx.com #你的公网域名
首先本地后端使用8080端口映射到公网服务器的8081端口 因为微信公众号的回调与网页授权都需要使用80与443端口所以下面需要使用nginx 反向代理 把访问公网IP80端口的请求转发到公网本机的8081端口 frps监听本机8081端口 转发到frpc 在使用frpc在把公网服务器8081的端口在转发到本地8080后端端口实现端口映射
添加nginx 配置
server {
listen 80;
server_name 你的域名;
location /view/ {
alias /var/www/dist/;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /wx/ {
proxy_pass http://你的域名:8081;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#上传文件大小配置
client_max_body_size 100M;
}
}
注意location 的路径这里使用的是alias而不是root root 指令在这里与 alias 指令略有不同。对于 root 指令,Nginx 会将 URI 直接附加到 root 指定的路径后面来查找文件。因此,在 location /vs/ 块中,URI /vs/abc.html 会被映射到 /var/www/dist/vs/abc.html 而不是 /var/www/dist/abc.html
由此实现当访问http://xxx.xx.xx/view/xxx.html的时候访问前端页面 访问http://xxx.xx.xx/wx的时候实现请求转发到本机8081 再用frps转发到frpc本机的8080端口实现微信服务器端与你的回调通信