nginx配置ipv6支持
Nginx 全面支持 IPv6 配置指南
一、基础 IPv6 配置
1. 启用 IPv6 监听
# 同时监听 IPv4 和 IPv6
http {
server {
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
...
}
}
# 仅监听 IPv6
server {
listen [::]:80;
...
}
2. IPv6 地址格式说明
类型 | 示例 | 说明 |
---|---|---|
标准地址 | 2001:0db8:85a3:0000:0000:8a2e:0370:7334 |
完整格式 |
压缩格式 | 2001:db8:85a3::8a2e:370:7334 |
省略连续零 |
环回地址 | ::1 |
IPv6 的 localhost |
未指定地址 | :: |
全零地址 |
二、高级 IPv6 配置
1. 双栈配置优化
# 分离 IPv4/IPv6 日志
http {
log_format ipv4 '$remote_addr - $remote_user [$time_local] "$request"';
log_format ipv6 '[$remote_addr] - $remote_user [$time_local] "$request"';
map $remote_addr $log_format {
~: ipv6;
default ipv4;
}
access_log /var/log/nginx/access.log $log_format;
}
2. IPv6 访问控制
location /admin {
# 允许特定 IPv6 子网
allow 2001:db8:85a3::/48;
# 允许本地 IPv6
allow ::1;
# 拒绝其他所有
deny all;
...
}
3. IPv6 反向代理
upstream backend {
# IPv6 后端服务器
server [2001:db8:1::1]:8080;
# IPv4 后端服务器
server 192.168.1.100:8080;
}
server {
listen [::]:80;
location / {
proxy_pass http://backend;
proxy_set_header X-Real-IP $remote_addr;
}
}
三、SSL/TLS 配置
1. IPv6 SSL 证书配置
server {
listen [::]:443 ssl;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
# 现代加密套件
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
}
2. HSTS 包含 IPv6
server {
listen [::]:443 ssl;
# 包含 IPv6 的 HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}
四、性能优化
1. 内核参数调优
# /etc/sysctl.conf
# 增加 IPv6 连接队列
net.ipv6.tcp_max_syn_backlog = 65536
net.core.somaxconn = 65536
# 启用 TCP Fast Open
net.ipv6.tcp_fastopen = 3
# 增加可用端口范围
net.ipv6.ip_local_port_range = 1024 65535
2. Nginx 调优参数
http {
# 启用多路复用
aio threads;
# TCP 优化
tcp_nodelay on;
tcp_nopush on;
# 连接池优化
worker_connections 10000;
multi_accept on;
# 缓冲区优化
client_body_buffer_size 10K;
client_header_buffer_size 1k;
large_client_header_buffers 4 8k;
}
五、安全加固
1. IPv6 DDoS 防护
# 限制连接速率
limit_conn_zone $binary_remote_addr zone=ipv6_conn:10m;
limit_req_zone $binary_remote_addr zone=ipv6_req:10m rate=10r/s;
server {
limit_conn ipv6_conn 20;
limit_req zone=ipv6_req burst=30;
}
2. 防止地址欺骗
# 验证真实客户端IP
real_ip_header X-Forwarded-For;
set_real_ip_from 2001:db8::/32; # 信任的代理IP段
六、IPv6 测试与诊断
1. 测试命令
# 检查监听端口
ss -tuln | grep ':80'
# 测试 IPv6 连接
curl -6 http://example.com
curl -I -6 --insecure https://example.com
# DNS 解析测试
dig AAAA example.com +short
2. Nginx 诊断配置
# 开启调试日志
error_log /var/log/nginx/error.log debug;
# 状态页面
server {
listen [::]:8080;
location /nginx_status {
stub_status on;
access_log off;
allow ::1;
deny all;
}
}
七、常见问题解决方案
1. 无法绑定 IPv6 地址
错误日志:
bind() to [::]:80 failed (98: Address already in use)
解决方案:
# 查找占用进程
sudo netstat -tulnp | grep ':80'
# 终止冲突进程
sudo kill -9 <PID>
# 或修改 Nginx 监听端口
listen [::]:8080;
2. IPv6 连接超时
可能原因:
- 防火墙阻止 IPv6
- 路由配置错误
- DNS 解析问题
排查步骤:
# 检查防火墙
sudo ip6tables -L
# 测试路由
traceroute6 example.com
# 检查 DNS
dig AAAA example.com
3. SSL 证书问题
错误日志:
SSL_do_handshake() failed (SSL: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure)
解决方案:
# 确保证书包含 SAN(Subject Alternative Name)
openssl req -new -newkey rsa:2048 -nodes \
-keyout example.com.key \
-out example.com.csr \
-addext "subjectAltName = DNS:example.com, DNS:www.example.com"
八、IPv6 迁移路线图
九、监控与告警
1. Prometheus 监控配置
# nginx-exporter 配置
scrape_configs:
- job_name: 'nginx-ipv6'
static_configs:
- targets: ['[2001:db8::1]:9113']
params:
target: ['http://[::1]/nginx_status']
2. Grafana 仪表盘
# IPv6 流量查询
SELECT
sum(if(remote_addr RLIKE ':', bytes_sent)) as ipv6_traffic
FROM nginx_logs
WHERE time > now() - 1h
3. 关键告警规则
# Alertmanager 配置
groups:
- name: nginx-ipv6-alerts
rules:
- alert: IPv6TrafficDrop
expr: sum(nginx_connections_active{family="ipv6"}) < 10
for: 5m
labels:
severity: critical
annotations:
summary: "IPv6 traffic dropped"
description: "IPv6 active connections below 10 for 5 minutes"
十、最佳实践总结
双栈优先策略:
# 同时监听 IPv4 和 IPv6 listen 80; listen [::]:80;
安全加固措施:
- 启用 SSL/TLS 1.3
- 配置严格的访问控制
- 启用 WAF 防护
性能优化要点:
# 启用多路复用 aio threads; # TCP 优化 tcp_nodelay on; tcp_nopush on;
监控体系:
- 实时流量监控
- 连接状态跟踪
- 自动告警系统
迁移策略:
- 先双栈运行
- 逐步增加 IPv6 流量
- 最终实现纯 IPv6 环境
通过以上配置,Nginx 将全面支持 IPv6 环境,同时保持高性能和高安全性。建议在生产环境部署前进行全面测试,并确保网络基础设施完全支持 IPv6。
##关联知识
弄懂nginx看这一篇文章就够了
【服务器知识】Nginx路由匹配规则说明
【服务器知识】nginx不够,那我们就试试openresty