用nginx實現微信公眾號相關的反向代理。


將原網站(微信業務)直接通過前端代理服務器(A)反向代理到后端機器(B)上會報一個 xxxx not in whitelist hint 的錯誤。
因為原來白名單IP為 A服務器IP,由於業務上需要修改公眾號太多,於是想不修改公眾號的情況下是否也可以實現。

最后經人點撥后實現了,感謝@Wendal~獸

“要么改源碼,修改api url,變成a服務器的域名
要么改dns,設置為a機器的ip
然后a服務器的nginx再反代出去”

我采用的反代方式:
A(test.com)->B(127.0.0.1)
B(修改host)->A(反向代理指向微信的api)

server{
    listen 443;
    server_name api.weixin.qq.com;
    access_log logs/weixin.qq.access.log;
    error_log logs/weixin.qq.error.log;
    ssl on;
    ssl_certificate /usr/local/nginx/conf/server.crt;
    ssl_certificate_key /usr/local/nginx/conf/server.nopass.key;
    location / {
        index index.html;
        proxy_pass https://api.weixin.qq.com;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-proto https;
    }

}


補充:
由於*.weixin.qq.com的域名很多,我就想能不能像泛解析那樣用一個正則或通配符創建一個站點滿足所有需求?答案是可以的,見配置

server {
    listen  80;
    server_name  ~^(?<subdomain>.+).weixin.qq.com$;
    location / {
        index index.html;
        proxy_pass http://$subdomain.weixin.qq.com;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-proto https;
    }
}
server{
    listen 443;
    server_name ~^(?<subdomain>.+).weixin.qq.com;
    access_log logs/x.weixin.qq.access.log;
    error_log logs/x.weixin.qq.error.log;
    ssl on;
    ssl_certificate /usr/local/nginx/conf/cert/mp.weixin.qq.com/server.crt;
    ssl_certificate_key /usr/local/nginx/conf/cert/mp.weixin.qq.com/server.nopass.key;
    location / {
        index index.html;
        proxy_pass https://$subdomain.weixin.qq.com;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-proto https;
        proxy_buffers 8 512k;
        proxy_buffer_size 2024k;
        proxy_busy_buffers_size 2024k;
        proxy_read_timeout 3000;
    }

}

需要注意的是,可能你會遇到 502的問題,我遇到這個問題的時查看了站點錯誤日志:

2019/07/11 15:51:33 [error] 21964#0: *689699 no resolver defined to resolve demo.open.weixin.qq.com, client: x.x.x.x, server: ~^(?<subdomain>.+).weixin.qq.com, request: "GET /jssdk/images/p2166127561.jpg HTTP/1.1", host: "demo.open.weixin.qq.com"

搜了下解決辦法,也很簡單:
在nginx.conf的http 中加上:resolver 8.8.8.8; 然后reload即可。

還有backend 需要修改/etc/hosts的域名有:
x.x.x.x api.weixin.qq.com
x.x.x.x file.api.weixin.qq.com
x.x.x.x mp.weixin.qq.com
x.x.x.x open.weixin.qq.com
x.x.x.x open.work.weixin.qq.com
x.x.x.x demo.open.weixin.qq.com
x.x.x.x qyapi.weixin.qq.com
#x.x.x.x pay.weixin.qq.com
#x.x.x.x api.mch.weixin.qq.com
#x.x.x.x fraud.mch.weixin.qq.com

=分割線

繼續補充:
在后來的使用中發現用微信企業打款時要報“證書錯誤”,經過各種排查摸索(嘗試更換證書、修改spbill_create_ip 的IP、不使用nginx獲取真實ip 都不行),最后嘗試直接從B機去訪問 api.mch.weixin.qq.com ,居然可以(企業打款中白名單中包含B機的IP)。當然這算哪門子事,如果不反向代理微信,隨着增加后端機器,那么IP白名單也就要增加,又要修改各公眾號的設置,解決不了根本的問題。(由於開通企業打款的客戶就幾家,暫時不管了)

參考:https://www.cnblogs.com/jingxiaoniu/p/6745254.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM