在现代 Web 架构中,Nginx 常作为 TLS 终止点使用——它负责 HTTPS 握手、证书管理、HTTP/2/ALPN 协商、以及将解密后的请求转发给后端服务。本文从工程实践出发,覆盖 Nginx 配置要点、性能与安全优化、常见故障排查命令与思路,并说明在 iOS 真机或高安全场景下如何借助抓包工具(包括 USB 直连工具 Sniffmaster)进行调试。面向后端/运维/前端工程师,本文力求可复制、易落地。
把 HTTPS 放在边缘(Nginx)有这些好处:
Internet → Nginx (TLS) → upstream (unix socket / HTTP)。在
/etc/nginx/sites-available/example.conf 中:
1server { 2 listen 443 ssl http2; 3 server_name api.example.com; 4 5 ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem; 6 ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem; 7 8 ssl_protocols TLSv1.2 TLSv1.3; 9 ssl_prefer_server_ciphers on;10 ssl_ciphers 'EECDH+AESGCM:EECDH+CHACHA20';1112 ssl_session_cache shared:SSL:10m;13 ssl_session_timeout 10m;14 ssl_session_tickets off;1516 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;1718 location / {19 proxy_pass http://127.0.0.1:8080;20 proxy_set_header Host $host;21 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;22 proxy_set_header X-Forwarded-Proto $scheme;23 proxy_http_version 1.1;24 proxy_set_header Connection "";25 proxy_buffering off;26 }27}
说明:
http2 启用 HTTP/2;
proxy_http_version 1.1 和
Connection "" 保持 upstream 的长连接与并发复用。
推荐用 certbot 自动化:
1sudo apt install certbot python3-certbot-nginx2sudo certbot --nginx -d api.example.com3# 自动续期测试4sudo certbot renew --dry-run
注意:在负载均衡或多节点部署时,证书/私钥应通过安全渠道分发(Vault、KMS),并限制读权限。
curl --http2 -v。测试示例:
1# 查看服务器支持的协议与证书2openssl s_client -connect api.example.com:443 -servername api.example.com -alpn h2,http/1.1
1ssl_stapling on;2ssl_stapling_verify on;3resolver 8.8.8.8 8.8.4.4 valid=300s;
worker_processes、
worker_connections、
proxy_buffers,根据业务并发与后端吞吐调节。排查:
openssl s_client -connect host:443 -servername host 查看返回证书 CN/SAN 与链是否完整。排查:
tcpdump 抓取握手包:
sudo tcpdump -i any host api.example.com and port 443 -w /tmp/tls.pcap,导入 Wireshark 查看 ClientHello / ServerHello、重传与 Alert。排查:
curl --http2 -v 与
curl --http1.1 -v 对比;如果 HTTP/2 出错,可能是 ALPN / proxy 或后端兼容问题。upstream prematurely closed connection 类错误,并检查
proxy_buffering 与
proxy_http_version 设置。排查:
Nginx 对 WebSocket 需要确保转发使用 HTTP/1.1 并保留
Upgrade /
Connection 头:
1proxy_set_header Upgrade $http_upgrade;2proxy_set_header Connection $connection_upgrade;3map $http_upgrade $connection_upgrade {4 default upgrade;5 '' close;6}
同时为长连接调优
proxy_read_timeout、
proxy_send_timeout,避免连接被中间层意外断开。
1# 查看证书 2openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts 3 4# curl 测试 http2 5curl --http2 -v https://api.example.com/ 6 7# tcpdump 抓包 8sudo tcpdump -i any host api.example.com and port 443 -w /tmp/tls.pcap 910# Nginx 配置测试并重载11sudo nginx -t && sudo systemctl reload nginx
Nginx 做为 HTTPS 边缘层,既能集中管理证书、提升性能,也为上游服务屏蔽许多 TLS 细节。工程实践上要关注 TLS 配置(支持 TLS1.2/1.3、合理 cipher)、会话复用(session resumption / OCSP stapling)、HTTP/2 与 WebSocket 转发、以及证书自动化。遇到复杂的真机问题或代理无法捕获流量的场景(如 iOS App 的 Pinning 或企业网络干预),把 Sniffmaster 这样的真机直连抓包工具纳入排查链路,能把问题从“客户端表现”快速还原为可分析的握手/报文证据,从而更快定位问题根源并修复。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70047439/viewspace-3096647/ ,如需转载,请注明出处,否则将追究法律责任。