V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
testVmap
V2EX  ›  问与答

nginx 反向代理微信服务器问题

  •  
  •   testVmap · 2018-10-19 18:02:58 +08:00 · 4005 次点击
    这是一个创建于 2013 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求: 内网服务器 B 需要访问微信服务器,所以做反向代理

    环境配置: 服务器 A 能访问外网 172.16.0.7 服务器 B 不能访问外网

    修改服务器 B 的 hosts 文件,域名指向服务器 A 172.16.0.7 mp.weixin.qq.com

    服务器 A 的 nginx 配置

    server {
            listen  80;
            server_name  mp.weixin.qq.com;
            location / {
            index index.html;
            proxy_pass https://mp.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  mp.weixin.qq.com;
    
            #charset koi8-r;
            ssl          on;
            ssl_certificate      /usr/local/nginx/conf/bundle.crt;
            ssl_certificate_key  /usr/local/nginx/conf/b.key;
    
            ssl_session_timeout  5m;
            ssl_protocols   TLSv1 TLSv1.1 TLSv1.2;
            ssl_ciphers     ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:-LOW:!aNULL:!eNULL;
            ssl_prefer_server_ciphers   on;
    
            #access_log  logs/host.access.log  main;
    
            location / {
            index index.html;
            proxy_pass https://mp.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;
            }
    }
    

    结果: 在服务器 B 访问

    http 访问正常

    #curl http://mp.weixin.qq.com/cgi-bin/component
    {"errmsg":"System Error!!!"}
    

    https 访问失败

    #curl https://mp.weixin.qq.com/cgi-bin/component
    curl: (51) Unable to communicate securely with peer: requested domain name does not match the server's certificate.
    

    问题:nginx 改如何配置能够转发 https 的请求

    5 条回复    2018-10-22 23:24:30 +08:00
    whatever93
        1
    whatever93  
       2018-10-19 19:58:10 +08:00 via Android
    证书没过 转不了吧...除非你有公钥私钥
    gftfl
        2
    gftfl  
       2018-10-19 21:59:36 +08:00
    记得 php 在 curl 里是需要 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);跳过证书检查的。
    lihuimail
        3
    lihuimail  
       2018-10-19 22:32:55 +08:00 via Android
    nginx 不能是 http 80 必须是 https
    ladypxy
        4
    ladypxy  
       2018-10-19 22:45:03 +08:00 via iPhone
    你这样是不行的,你需要用自己一个域名,然后加证书,然后把请求转发到微信域名。类似于中间人的设置才行,不然你证书验证就过不了
    testVmap
        5
    testVmap  
    OP
       2018-10-22 23:24:30 +08:00
    location ^~ /mp/
    {
    #proxy_cache api_cache;
    proxy_set_header Host mp.weixin.qq.com;
    rewrite /mp/(.+)$ /$1 break;
    proxy_pass https://mp.weixin.qq.com;
    }

    最后改成了根据请求路径转发
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3688 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 04:20 · PVG 12:20 · LAX 21:20 · JFK 00:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.