V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
chillwind
V2EX  ›  iDev

使用 AFNetworking 请求 https 的接口,首个请求耗时 3s 左右,后续的请求 0.5s,怀疑是证书验证比较耗时,请问怎么优化?

  •  
  •   chillwind · 2020-06-21 10:38:37 +08:00 · 5094 次点击
    这是一个创建于 1399 天前的主题,其中的信息可能已经有所发展或是发生改变。
    使用 charles 抓包,配置了证书,首个请求也在 0.5s 左右,更加怀疑问题出现在证书验证这个环节。
    设置了下安全策略也没啥效果
    self.securityPolicy.allowInvalidCertificates = YES;
    self.securityPolicy.validatesDomainName = NO;
    18 条回复    2020-06-21 22:52:58 +08:00
    zhigang1992
        1
    zhigang1992  
       2020-06-21 10:46:36 +08:00
    会不会是后端的 cold start?

    直接用 curl 能复现么?
    arrow8899
        2
    arrow8899  
       2020-06-21 10:48:55 +08:00
    服务器端抓包看一下
    chinawrj
        3
    chinawrj  
       2020-06-21 10:52:39 +08:00
    检查 CRL
    chillwind
        4
    chillwind  
    OP
       2020-06-21 10:54:48 +08:00
    服务器端 nginx 收到请求也有延迟,nginx 的日志在客户端启动后约 3s 打印出来
    chillwind
        5
    chillwind  
    OP
       2020-06-21 10:57:05 +08:00
    不太可能是后端问题,iOS 端配置好 charles 的代理,请求就正常了。charles 要安装个证书在手机上,这部分原理不是很清楚,怀疑是不是减少了证书验证环节,证书验证在电脑上的 charles 上进行了。
    wowbaby
        6
    wowbaby  
       2020-06-21 11:12:01 +08:00
    是不是用了 let's encrypt,我遇到过这个问题,原因是有些 dns 被 Q,换证书解决。
    chillwind
        7
    chillwind  
    OP
       2020-06-21 11:23:38 +08:00
    @wowbaby 是的,用的 let's encrypt,你后续换的什么证书?
    chillwind
        8
    chillwind  
    OP
       2020-06-21 11:28:42 +08:00
    @wowbaby 安卓端没有这个问题,证书验证逻辑不一样?
    GuryYu
        9
    GuryYu  
       2020-06-21 11:53:20 +08:00   ❤️ 8
    之前刚碰到这个问题,坑了我们很久,在不同的 iOS 手机型号和版本上的现象还不一致,安卓手机上未发现。

    最终检查出来是因为 let's encrypt 的证书 OSCP 服务器域名被 DNS 污染,导致首个请求验证 OSCP 时响应超时。
    关于 let's encrypt 的 OSCP 服务器被 DNS 污染 v 站之前有讨论过,详见:
    https://www.v2ex.com/t/665734
    https://www.v2ex.com/t/661753

    目前解决方案有两种,
    1. 更换非 let's encrypt 证书,避免访问被污染的 OSCP 服务器

    2. 在 nginx 配置开启 ssl_stapling,并指定未被污染的 DNS 服务器。由服务器进行请求 OSCP 认证缓存后发送给客户端。
    配置方法:
    在 nginx 的 http 配置内添加
    ```
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 4.4.4.4 valid=60s;
    resolver_timeout 2s;
    ```

    由于我们使用了 let's encrypt 提供的通配符证书,没有其他平台能提供免费的通配符证书。
    所以我们当前使用方案 2 来解决
    lanternxx
        10
    lanternxx  
       2020-06-21 12:07:34 +08:00 via Android
    开启 ocsp 装订。或者换一个证书品牌,阿里云和 trustasia 都可以申请免费的一年单域名。我是 269 买了个 comodo 一年的通配符。
    qwerthhusn
        11
    qwerthhusn  
       2020-06-21 13:32:25 +08:00
    @lanternxx 老兄你在哪买的,才 269 ?
    lanternxx
        12
    lanternxx  
       2020-06-21 13:36:47 +08:00
    @qwerthhusn #11 https://item.taobao.com/item.htm?id=613109712119 这家,和客服说 V2EX 来的可以改价 269
    yangxin0
        13
    yangxin0  
       2020-06-21 13:46:38 +08:00 via iPhone
    wireshark 在手机上抓个包看看。
    xi_lin
        14
    xi_lin  
       2020-06-21 15:30:24 +08:00   ❤️ 1
    https://imququ.com/post/optimize-tls-handshake.html 这文章说的握手流程都可以看看
    allenforrest
        15
    allenforrest  
       2020-06-21 15:55:33 +08:00
    如果 Let's Encrypt 证书,基本上就是 OCSP 服务器域名被污染的问题。
    建议在服务器上开 SSL Stapling 。
    xiaotianhu
        16
    xiaotianhu  
       2020-06-21 16:10:15 +08:00
    直接阿里云免费走起.
    acumen
        17
    acumen  
       2020-06-21 22:12:54 +08:00
    之前也遇到过在 iOS 特定的系统和机型上的网页首次访问 HTTPS 请求特别慢的问题。我们是换了证书。应该是相同情况。
    0xDatou
        18
    0xDatou  
       2020-06-21 22:52:58 +08:00
    let's encrypt +1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2724 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:37 · PVG 13:37 · LAX 22:37 · JFK 01:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.