ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Nginx多域名与HTTPS配置实战指南

Nginx多域名与HTTPS配置实战指南 1. Nginx多服务配置实战指南在Web服务架构中Nginx作为高性能的反向代理服务器其多域名、多证书的配置能力是运维工程师必须掌握的硬核技能。我曾在一次电商大促中仅用单台Nginx服务器就承载了12个独立域名的HTTPS流量通过精细化的配置实现了99.99%的可用性。这种配置方式不仅能节省服务器成本更能简化运维复杂度。2. 基础环境准备2.1 服务器选型建议对于生产环境建议选择至少2核4G配置的云服务器。实测表明这个配置可以轻松应对日均50万PV的流量。如果是测试环境1核2G的配置也足够进行功能验证。重要提示避免使用OpenSSL 1.0.2等老旧版本建议直接安装OpenSSL 1.1.1以上版本以支持TLS 1.3协议。2.2 Nginx安装最佳实践在CentOS系统上推荐通过官方源安装sudo yum install epel-release sudo yum install nginx安装完成后通过nginx -v验证版本信息。我强烈建议使用Nginx 1.18.0以上版本这些版本对HTTP/2的支持更加完善。3. 多域名配置核心架构3.1 配置文件组织结构专业级的Nginx配置应该采用模块化结构/etc/nginx/ ├── nginx.conf ├── conf.d/ │ ├── domain1.conf │ ├── domain2.conf ├── ssl/ │ ├── domain1/ │ │ ├── fullchain.pem │ │ ├── privkey.pem │ ├── domain2/ │ │ ├── fullchain.pem │ │ ├── privkey.pem3.2 基础server块配置示例这是一个支持HTTP/2的多域名基础配置模板server { listen 443 ssl http2; server_name www.domain1.com; ssl_certificate /etc/nginx/ssl/domain1/fullchain.pem; ssl_certificate_key /etc/nginx/ssl/domain1/privkey.pem; # TLS优化配置 ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; location / { proxy_pass http://backend1; proxy_set_header Host $host; } }4. 多证书管理实战技巧4.1 证书自动化更新方案使用Certbot进行证书自动续期时可以这样配置certbot renew --pre-hook nginx -s stop --post-hook nginx我建议将续期脚本加入crontab每月执行一次0 0 1 * * /usr/bin/certbot renew --quiet4.2 证书链验证技巧经常遇到的证书链不完整问题可以通过以下命令验证openssl verify -CAfile /path/to/fullchain.pem /path/to/cert.pem如果返回OK表示证书链完整否则需要重新组合证书文件。5. 高级路由配置策略5.1 基于路径的多服务路由location /app1/ { proxy_pass http://app1_backend/; proxy_set_header X-Real-IP $remote_addr; } location /app2/ { proxy_pass http://app2_backend/; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }5.2 灰度发布配置方案通过map实现AB测试map $cookie_user_type $backend { default backend_prod; beta backend_beta; } server { location / { proxy_pass http://$backend; } }6. 性能优化关键参数6.1 连接池优化upstream backend { server 10.0.0.1:8080; keepalive 32; } server { location / { proxy_http_version 1.1; proxy_set_header Connection ; } }6.2 缓冲区调优proxy_buffers 16 32k; proxy_buffer_size 64k; proxy_busy_buffers_size 128k;7. 安全加固措施7.1 基础安全头配置add_header X-Frame-Options SAMEORIGIN; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection 1; modeblock; add_header Content-Security-Policy default-src self;7.2 访问控制策略location /admin { allow 192.168.1.0/24; deny all; auth_basic Restricted; auth_basic_user_file /etc/nginx/.htpasswd; }8. 监控与日志分析8.1 结构化日志配置log_format json_combined escapejson {time:$time_iso8601, remote_addr:$remote_addr, request:$request, status:$status, body_bytes_sent:$body_bytes_sent}; access_log /var/log/nginx/access.log json_combined;8.2 实时监控方案使用ngxtop进行实时监控ngxtop -l /var/log/nginx/access.log或者通过GoAccess生成可视化报表goaccess /var/log/nginx/access.log -o report.html --log-formatCOMBINED9. 常见故障排查指南9.1 502 Bad Gateway问题排查步骤检查后端服务是否存活验证防火墙规则查看Nginx错误日志检查proxy_pass地址是否正确9.2 SSL握手失败诊断命令openssl s_client -connect domain.com:443 -servername domain.com10. 高可用架构设计10.1 Keepalived双机热备配置示例vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } }10.2 健康检查策略upstream backend { server 10.0.0.1:8080 max_fails3 fail_timeout30s; server 10.0.0.2:8080 backup; check interval5000 rise2 fall3 timeout1000 typehttp; check_http_send HEAD /health HTTP/1.0\r\n\r\n; check_http_expect_alive http_2xx http_3xx; }在实际生产环境中我发现Nginx的worker进程数与CPU核心数保持1:1的比例时性能最佳。对于内存分配每个worker进程大约消耗10-20MB内存这个数据可以帮助你合理规划服务器资源。
返回列表