name: 网络 description: 调试网络连接性、配置负载均衡器、分析流量。用于连接问题、网络优化或协议调试。
网络工程
调试连接性和配置网络基础设施。
使用场景
- 连接问题
- 负载均衡器设置
- SSL/TLS 问题
- DNS 调试
- 网络性能
诊断命令
连接性测试
# 基本连接性
ping -c 4 host.example.com
traceroute host.example.com
# 端口检查
nc -zv host.example.com 443
telnet host.example.com 80
# DNS 查找
dig +short example.com
dig +trace example.com
nslookup -type=MX example.com
# HTTP 测试
curl -v https://api.example.com/health
curl -w "@curl-format.txt" -o /dev/null -s https://example.com
curl-format.txt
time_namelookup: %{time_namelookup}s
time_connect: %{time_connect}s
time_appconnect: %{time_appconnect}s
time_pretransfer: %{time_pretransfer}s
time_redirect: %{time_redirect}s
time_starttransfer: %{time_starttransfer}s
----------
time_total: %{time_total}s
SSL/TLS 调试
# 检查证书
openssl s_client -connect example.com:443 -servername example.com
# 验证证书链
openssl s_client -connect example.com:443 -showcerts
# 检查过期时间
echo | openssl s_client -connect example.com:443 2>/dev/null | openssl x509 -noout -dates
# 测试特定 TLS 版本
curl --tlsv1.2 --tls-max 1.2 https://example.com
负载均衡器配置
Nginx
upstream backend {
least_conn;
server backend1.example.com:8080 weight=5;
server backend2.example.com:8080 weight=3;
server backend3.example.com:8080 backup;
keepalive 32;
}
server {
listen 443 ssl http2;
server_name api.example.com;
ssl_certificate /etc/ssl/certs/api.crt;
ssl_certificate_key /etc/ssl/private/api.key;
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_connect_timeout 5s;
proxy_read_timeout 60s;
}
location /health {
access_log off;
return 200 "OK";
}
}
流量分析
# 捕获数据包
tcpdump -i eth0 -w capture.pcap port 443
# 读取捕获
tcpdump -r capture.pcap -n
# 按主机过滤
tcpdump -i any host 10.0.0.1 and port 80
# 显示 HTTP 请求
tcpdump -i any -A -s 0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'
常见问题
| 症状 | 检查 | 修复 |
|---|---|---|
| 连接被拒绝 | 端口开放?服务运行? | 启动服务,打开防火墙 |
| 连接超时 | 防火墙?路由? | 检查安全组,路由 |
| SSL 错误 | 证书有效?链完整? | 续订证书,修复链 |
| DNS 失败 | 解析器?记录存在? | 检查 DNS 配置,添加记录 |
| 响应慢 | 延迟?带宽? | 优化路由,增加容量 |
示例
输入: “API 调用超时” 操作: 测试连接性,检查 DNS,验证 SSL,分析延迟
输入: “设置负载均衡器” 操作: 配置 nginx/HAProxy,添加健康检查,测试故障转移