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 · Jun 21, 2020 · 6179 views
    This topic created in 2176 days ago, the information mentioned may be changed or developed.
    使用 charles 抓包,配置了证书,首个请求也在 0.5s 左右,更加怀疑问题出现在证书验证这个环节。
    设置了下安全策略也没啥效果
    self.securityPolicy.allowInvalidCertificates = YES;
    self.securityPolicy.validatesDomainName = NO;
    18 replies    2020-06-21 22:52:58 +08:00
    zhigang1992
        1
    zhigang1992  
       Jun 21, 2020
    会不会是后端的 cold start?

    直接用 curl 能复现么?
    arrow8899
        2
    arrow8899  
       Jun 21, 2020
    服务器端抓包看一下
    chinawrj
        3
    chinawrj  
       Jun 21, 2020
    检查 CRL
    chillwind
        4
    chillwind  
    OP
       Jun 21, 2020
    服务器端 nginx 收到请求也有延迟,nginx 的日志在客户端启动后约 3s 打印出来
    chillwind
        5
    chillwind  
    OP
       Jun 21, 2020
    不太可能是后端问题,iOS 端配置好 charles 的代理,请求就正常了。charles 要安装个证书在手机上,这部分原理不是很清楚,怀疑是不是减少了证书验证环节,证书验证在电脑上的 charles 上进行了。
    wowbaby
        6
    wowbaby  
       Jun 21, 2020
    是不是用了 let's encrypt,我遇到过这个问题,原因是有些 dns 被 Q,换证书解决。
    chillwind
        7
    chillwind  
    OP
       Jun 21, 2020
    @wowbaby 是的,用的 let's encrypt,你后续换的什么证书?
    chillwind
        8
    chillwind  
    OP
       Jun 21, 2020
    @wowbaby 安卓端没有这个问题,证书验证逻辑不一样?
    GuryYu
        9
    GuryYu  
       Jun 21, 2020   ❤️ 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 来解决
    shinciao
        10
    shinciao  
       Jun 21, 2020 via Android
    开启 ocsp 装订。或者换一个证书品牌,阿里云和 trustasia 都可以申请免费的一年单域名。我是 269 买了个 comodo 一年的通配符。
    qwerthhusn
        11
    qwerthhusn  
       Jun 21, 2020
    @lanternxx 老兄你在哪买的,才 269 ?
    shinciao
        12
    shinciao  
       Jun 21, 2020
    @qwerthhusn #11 https://item.taobao.com/item.htm?id=613109712119 这家,和客服说 V2EX 来的可以改价 269
    yangxin0
        13
    yangxin0  
       Jun 21, 2020 via iPhone
    wireshark 在手机上抓个包看看。
    xi_lin
        14
    xi_lin  
       Jun 21, 2020   ❤️ 1
    https://imququ.com/post/optimize-tls-handshake.html 这文章说的握手流程都可以看看
    allenforrest
        15
    allenforrest  
       Jun 21, 2020
    如果 Let's Encrypt 证书,基本上就是 OCSP 服务器域名被污染的问题。
    建议在服务器上开 SSL Stapling 。
    xiaotianhu
        16
    xiaotianhu  
       Jun 21, 2020
    直接阿里云免费走起.
    acumen
        17
    acumen  
       Jun 21, 2020
    之前也遇到过在 iOS 特定的系统和机型上的网页首次访问 HTTPS 请求特别慢的问题。我们是换了证书。应该是相同情况。
    0xDatou
        18
    0xDatou  
       Jun 21, 2020
    let's encrypt +1
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   921 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 71ms · UTC 19:29 · PVG 03:29 · LAX 12:29 · JFK 15:29
    ♥ Do have faith in what you're doing.