微信公号调试遇到Nginx错误499 | Nginx Code 499 on Wechat OAuth debugging

2017-01-25 作者:

最近弄了个公号测试号,尝试一下腾讯OAuth,用的还是习惯的nginx+uwsgi+flask

注意【开放平台】和【公众平台】的OAuth有差别,跳转的auth地址,及scope都是不同的;前者主要用于浏览器app(微信内置浏览器也可兼容),后者跟公众号绑定的更紧密(不关注不能auth)

这货调试蛮坑的 –

  • 首先得有公网IP,这个简单,买个按小时付费的美团云(阿里云)就行,1核1G,不要数据盘,带宽1M;一个小时0.1元多一点,一天也不到3块钱,不需要了随时可删。
  • (带备案)域名一个。这个有点难度,买云主机备案不要钱,但两周时间等的着实有点心焦。如果有现成域名,可以开个二级域名(端口依然要80),nginx转发一下,这样也是ok的。

木有用域名的时候,我尝试直接用云主机ip接测试号,一共有3处配置,都配了IP(而非域名)

  • 接口配置信息
  • JS安全域名
  • OAuth授权回调域名(关键)

但实际在微信浏览器中访问时,页面无法正常展示,这个提示很关键(转换成手机预览模式)

于是在OAuth的过程中,uwsgi经常报如下错误(而且还是在auth成功之后):

500ms的请求不算慢啊,为啥会broken pipe呢?

并且明显uwsgi返回了code 200,请求没啥问题噻?

Wed Jan 25 16:54:43 2017 - SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request /qrScanToTemplateMsg/6?code=0515JtlQ11sLo61riTnQ15CxlQ15Jtl1&state=1 (ip 103.37.140.18) !!! 
Wed Jan 25 16:54:43 2017 - uwsgi_response_writev_headers_and_body_do(): Broken pipe [core/writer.c line 296] during GET /qrScanToTemplateMsg/6?code=0515JtlQ11sLo61riTnQ15CxlQ15Jtl1&state=1 (103.37.140.18) 
IOError: write error 
[pid: 24226|app: 0|req: 11/19] 103.37.140.18 () {38 vars in 892 bytes} [Wed Jan 25 16:54:42 2017] GET /qrScanToTemplateMsg/6?code=0515JtlQ11sLo61riTnQ15CxlQ15Jtl1&state=1 => generated 0 bytes in 468 msecs (HTTP/1.1 200) 3 headers in 0 bytes (0 switches on core 0)

开始时,丈二和尚摸不着头脑,鉴于uwsgi是nginx的upstream,那就找一下nginx看人家说啥呗

一看不要紧,同一个请求,nginx code是499;google后,表明499是“客户端主动关闭连接”,那么uwsgi送给nginx的200自然木有什么卵用了 – 客户端不要了

而且这一条log是在access.log而非error.log,说明nginx并木有怪罪uwsgi

103.37.140.18 - - [25/Jan/2017:16:54:43 +0800] "GET /qrScanToTemplateMsg/6?code=0515JtlQ11sLo61riTnQ15CxlQ15Jtl1&state=1 HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 6P Build/MTC20L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.9 TBS/036903 Safari/537.36 MicroMessenger/6.5.3.980 NetType/WIFI Language/zh_CN" "-"

who to blame right now?

我开始怀疑没有域名导致的转码,找了一个备案后没用过的域名,指向这个ip,不到一分钟就生效了

然后把测试号那三处配置都改成域名,再试。。。搞定了,同一个链接,uwsgi和nginx log都是code 200.

123.151.38.94 - - [25/Jan/2017:21:17:02 +0800] "GET /qrScanToTemplateMsg/6?code=0011MCQf1s8UTv0ZMHTf1CcRQf11MCQ3&state=1 HTTP/1.1" 200 480 "-" "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 6P Build/MTC20L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.9 TBS/036903 Safari/537.36 MicroMessenger/6.5.3.980 NetType/WIFI Language/zh_CN" "120.221.74.188"

同时网页访问也展示正常了,不需要什么“转换成手机预览模式”(截图就不贴了)。

究其本质,这个nginx code# 499(以及uwsgi的broken pipe),也即客户端主动断开连接,就是ip访问状态下,微信浏览器为了安全防护,拒绝直接渲染前端页面造成的。

完整阅读本篇»

保持念想 | Always Desire

2017-01-15 作者:

Well, finally the “Walking Dead Game Season #3” arrives. Never play this series before, but I know it all, every scenes, every dialogues, every roles, by walkthrough videos. I’d like to recommend this game to everyone around since it’s the best story teller ever as I know.

It’s not exactly an action game, the most attractive parts is that it says the game adapts by player’s choices. Well, you may not expect the story could change completely after choosing an opposite answer, but the screenplay is fantastic to show you the real human nature underneath the zombie apocalypse – challenging my heart more than those Hollywood blockbusters. And this is easily to purchase, PC, Mac, iOS, Android, PS4, Xbox – available on most electronics in your hand.

Er… talking about the season #3, it’s kinda two… or half more years waiting after the season #2? I mean, it’s always good to desire, to expect even some tiny things in life. Time flies, some of them happen, and others might not, however this is how life usually goes – pretty much like the little girl Clementine in this series.

完整阅读本篇»

雨多天光 | Utada Hikaru

2016-07-01 作者:

上了个脑残的英文标题。没办法,人不逗比枉中年咧。

今年帝都天气有点“双子座”(相比持续数年的“摩羯座”而言),觉得这座曾经灰霾的城市焕发一丝活力,好事儿。

忙的两脚离地,自然无暇戏耍,生活两点一线,片子个位数,不免乏善可陈。

所幸双子座天气偶尔给我一些按下快门的勇气,于是就有公司门口的两张什么feeling也不抒发的……digital pictures。

时而感慨,生活虽不平静,已逾而立的我,却未曾丢掉心中最珍视的那一抹阳光。

对错?不重要吧。

IMG_0949

IMG_0951

完整阅读本篇»

春 | The Spring

2016-04-24 作者:

本以为今年的春天,相比去年的稍长,会是意外的短。

但当长达3天的夏日过去,我们又回到乍暖还寒的北京之春(直到今天),不能不说,这是极好的。

IMG_0861

IMG_0811

IMG_0868-bw

IMG_0895

完整阅读本篇»

小小大世界 | Little Big World

2016-03-21 作者:

No one can escape from its life… well, I don’t think so. And NO, it’s not about the negative view is bad or unacceptable. On the contrary, the bad things could usually be damn good to you, more or less.

Reason here is, I appreciate my life since I’m lucky enough to be happy… not everyday though, nor with everyone around… but still happy. Sometimes I just care about myself, like I was stuck in a little world of my own; But little can be BIG after that, and I can take care all I care, and all I’m willing to, again, and toughly.

昨日陪娃去央音上课,于西二环边居然发现一处僻静之所。远看是一段不起眼的城墙,走近……仍然是一段不起眼的城墙。

IMG_0792

悠然踱到背后,墙下似有一处小小天地;当然,对不知好奇心为何物的Fisherworks,自然无需走进去打扰。

IMG_0756

登墙。站在城墙上,豁然发现,墙下和墙前,是两个世界,没准儿是老死不相往来的两个世界。可那又怎样?

完整阅读本篇»

让树莓派用上移动LTE | RaspberryPi Connect TD LTE

2016-02-18 作者:

很多人用树莓派接联通3G(WCDMA),这样的话,E261量很大很足

还有电信网络(CDMA2K),可以用EC122

可是移动3G/LTE,google找不到相关资料

其实huawei的USB adapter基本都复用了ID,所以基本上只要能买到华为的货,支持哪家的网都不是问题(甚至不需要升级usbmodeswitch版本,不需要自己build代码)

Fisherworks选了E3276s-861,为数不多的支持移动TD 2G/3G/4G/LTE的卡

注意不要随便选那种号称3网的usb卡,对移动的支持恐怕只限于2600MHz,1900不覆盖,到二三线地区估计够呛


 

树莓派wheezy/jessie系统,使用sudo apt-get install ppp usb-modeswitch来安装关键的usb-modeswitch,还有sakis3g/umtskeeper(这个就不写怎么装了,一搜一大把)

随便找一个接联通E261的教程就行,要改的有以下两点~

  1. 设备ID,E261被usbmodeswitch切换过ID后是12d1:1346,而E3276s-861切换后是12d1:1506
  2. CUSTOM_APN,联通是3gnet,移动是cmnet

然后就没有然后了,拨号接通享受网络

pic

 

完整阅读本篇»