1、引言
大家在做微信开发使用了微信api时,需要用有域名的公网测试。如果自己有vps和备案的域名,可以用frp软件做内网穿透达到外网访问自己本地开发机器的目的。因为微信公众号要求域名必须使用80端口,如果vps上80端口被占,可用nginx监听80端口进而转发给其他接口。作者在配置nginx和frp时遇到错误,费了好大力气才解决,原来只是不起眼的问题,这里记下备忘。
2、nginx与frp的安装
本文不详细描述安装过程,网上有很多教程,下面贴出我参考的文章:
1、nginx在linux下的安装:
文章一:https://blog.youkuaiyun.com/ItLoong/article/details/79170372
文章二:https://www.cnblogs.com/taiyonghai/p/6728707.html
我使用的nginx版本是nginx-1.12.2.tar.gz。参照文章一到步骤5,参照文章二的make执行代码:
./configure && make && make install
2、frp的安装:
https://blog.youkuaiyun.com/qq_25351621/article/details/78947477
3、nginx与frp的配置
可参照一篇很详细的文章:
https://www.jianshu.com/p/c0d7cb4cb00f
4、出现问题
参考以上文章安装与配置好nginx和frp后,启动nginx和frp。发现直接通过访问frp访问可以穿透到本地,但是通过nginx端口转发到frp则失败。页面出现502 bad gateway错误。nginx的error.log中错误提示[error] 32649#0: *106 connect() failed (111: Connection refused) while connecting to upstream。
意思是nginx连接不上frp服务,让我百思不得其解。经过各种折腾终于发现问题在于nginx与frp在配置host时不一致:
我在frps.ini中的配置bind_addr 为vps的具体IP地址:
[common]
bind_addr = 192.xxx.xxx.xx
bind_port = 4443
vhost_http_port = 8088
vhost_https_port = 443
但是在nginx的server中配置的localhost
server {
listen 80;
server_name dev.test.com;
#access_log logs/frp.access.log
location / {
proxy_pass http://localhost:8088;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_hide_header X-Powered-By;
}
}
问题就出在frp的bind_addr参数与nginx的proxy_pass参数不一致,虽然二者都是指向vps服务器。
解决办法就是将nginx的proxy_pass改为http://192.xxx.xxx.xx:8088
作者首测接触nginx和frp,对于原理只是一知半解,也许这个错误对于内行不足为道,聊且记录下来给自己备忘。
感谢上述参考文献作者,如侵权请联系我删除。
在微信开发中,使用frp进行内网穿透时,需要通过nginx监听80端口转发请求。然而配置过程中,遇到502 Bad Gateway错误。原因在于nginx的proxy_pass配置的localhost与frp的bind_addr配置不一致,解决方法是将nginx的proxy_pass改为frp服务的IP地址。
992





