开发小程序的时候有4个版本(开发、体验、审核、正式)。因此不一样的环境要请求不一样的后台。特别是审核
版本,由于还要微信审核,若是请求错误,会被审核失败。由于生产环境是对应旧的后台版本,因此审核
版本既不能调到后台生产环境,也不能跳到开发环境。
咱们为了方便生产
、审核
2个版本小程序都去请求/prod
。这里就要区别真实的请求究竟是从生产
、审核
哪里来的。html
微信给咱们服务器发送请求wx.request
的会带上一个referer
的header参数。格式以下:nginx
https://servicewechat.com/<appId>/<version>/page-frame.html
其中<appId>
是发送请求的小程序appId,<version>
是小程序的版本。小程序
开发
、体验
、审核
版本中version值是0,开发工具中version值是devtools
。正式
版的version值是大于0的正整数,表示这个小程序发布到正式版多少次。 例子以下:开发版: https://servicewechat.com/小程序appId/0/page-frame.html 体验版: https://servicewechat.com/小程序appId/0/page-frame.html devtools: https://servicewechat.com/小程序appId/devtools/page-frame.html 正式版: https://servicewechat.com/小程序appId/6/page-frame.html
foo
, 配置一个map,把http_referer
映射到foo
。map $http_referer $foo { default "prod"; ~^https://servicewechat.com/[^/]+/0/(.*)$ "dev"; ~^https://servicewechat.com/[^/]+/devtools/(.*)$ "dev"; }
upstream dev { server localhost:7777; } upstream prod { server localhost:9999; }
location
中使用 foo
变量, 导航到正确的地址。这里我用
add_header
把foo
变量输出一下,做为测试。api
location / { #set $foo "$http_referer"; add_header wkfoo 'foo: $foo "$http_referer"'; proxy_pass http://$foo; }
curl -H 'Cache-Control: no-cache' -I "https://xxx.xxx.com/prod/xxx?参数1=xxx&参数2=xxx" --referer "https://servicewechat.com/xxx/devtools/page-frame.html"
curl -H 'Cache-Control: no-cache' -I "https://xxx.xxx.com/prod/xxx?参数1=xxx&参数2=xxx" --referer "https://servicewechat.com/xxx/0/page-frame.html"
实际上这里小程序请求后台的时候,应该加上一个api的版本号。服务器
参考文章:
https://developers.weixin.qq.com/community/develop/article/doc/0004e2dffb0f98852cf7183285b013微信