nginx.conf 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # ============================================================================
  2. # Nginx Stream + SSL Preread 自动分流主配置
  3. # 架构:TCP层协议检测 → TLS/HTTP分流 → SSL终结 → 反向代理内网服务
  4. # 版本:v2.0
  5. # 最后更新:2026-10-06
  6. #
  7. # 配置结构:
  8. # nginx.conf — 主配置(40130 端口 + 基础架构)
  9. # zhonjin_http.conf — 子域名 HTTP 服务配置(include 到 http 块)
  10. # zhonjin_stream.conf — 子域名 Stream 分流配置(include 到 stream 块)
  11. # zhonjin.com.conf — 配置总览说明文档
  12. # ============================================================================
  13. load_module /usr/lib/nginx/modules/ngx_stream_module.so;
  14. user www-data; # Debian: www-data / CentOS: nginx
  15. worker_processes auto;
  16. pid /run/nginx.pid;
  17. error_log /var/log/nginx/error.log warn;
  18. events {
  19. worker_connections 8192;
  20. }
  21. # ============================================================================
  22. # HTTP 块:SSL 终结 + 反向代理 + HTTP→HTTPS 跳转
  23. # ============================================================================
  24. http {
  25. include /etc/nginx/mime.types;
  26. default_type application/octet-stream;
  27. sendfile on;
  28. tcp_nopush on;
  29. tcp_nodelay on;
  30. keepalive_timeout 65;
  31. server_tokens off;
  32. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  33. '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
  34. 'proto=$ssl_protocol sni=$ssl_server_name';
  35. access_log /var/log/nginx/access.log main;
  36. # WebSocket 升级映射(全局定义,各 location 引用)
  37. map $http_upgrade $connection_upgrade {
  38. default upgrade;
  39. '' close;
  40. }
  41. # ========================================================================
  42. # 服务组 1:外网端口 40130 → 内网服务 http://127.0.0.1:5004
  43. # 域名:chanking.zhonjin.com
  44. # ========================================================================
  45. # HTTPS 服务:SSL 终结,反代内网 5004
  46. server {
  47. listen 127.0.0.1:5005 ssl;
  48. http2 on;
  49. server_name chanking.zhonjin.com;
  50. ssl_certificate /etc/letsencrypt/live/zhonjin.com/fullchain.pem;
  51. ssl_certificate_key /etc/letsencrypt/live/zhonjin.com/privkey.pem;
  52. ssl_session_cache shared:SSL:10m;
  53. ssl_session_timeout 1d;
  54. ssl_session_tickets off;
  55. ssl_protocols TLSv1.2 TLSv1.3;
  56. ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
  57. ssl_prefer_server_ciphers off;
  58. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  59. location / {
  60. proxy_pass http://127.0.0.1:5004;
  61. proxy_http_version 1.1;
  62. proxy_set_header Host $host;
  63. proxy_set_header X-Real-IP $remote_addr;
  64. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  65. proxy_set_header X-Forwarded-Proto $scheme;
  66. proxy_set_header Upgrade $http_upgrade;
  67. proxy_set_header Connection $connection_upgrade;
  68. proxy_connect_timeout 5s;
  69. proxy_read_timeout 3600s;
  70. proxy_send_timeout 60s;
  71. }
  72. }
  73. # HTTP 服务:301 跳转到 HTTPS(端口 40130)
  74. server {
  75. listen 127.0.0.1:5006;
  76. server_name chanking.zhonjin.com;
  77. return 301 https://$host:40130$request_uri;
  78. }
  79. # ========================================================================
  80. # 子域名扩展配置(mqtt.zhonjin.com / baolin.zhonjin.com)
  81. # ========================================================================
  82. include /etc/nginx/conf.d/zhonjin_http.conf;
  83. }
  84. # ============================================================================
  85. # Stream 块:TCP 层协议检测 + 自动分流
  86. # ============================================================================
  87. stream {
  88. error_log /var/log/nginx/stream_error.log warn;
  89. log_format stream_main '$remote_addr [$time_local] $protocol '
  90. 'preread=$ssl_preread_protocol status=$status '
  91. 'upstream=$upstream_addr bytes=$bytes_sent';
  92. # ========================================================================
  93. # Stream 服务组 1:外网端口 40130 → 内网 5004(chanking.zhonjin.com)
  94. # ========================================================================
  95. map $ssl_preread_protocol $backend_target_40130 {
  96. "" 127.0.0.1:5006; # 明文HTTP → 301跳转到HTTPS
  97. default 127.0.0.1:5005; # TLS流量 → SSL解密入口
  98. }
  99. server {
  100. listen 40130 backlog=4096;
  101. tcp_nodelay on;
  102. ssl_preread on;
  103. # nginx >=1.23.4 才启用,低版本注释掉!当前版本 1.26.3
  104. #ssl_preread_buffer_size 16k;
  105. proxy_pass $backend_target_40130;
  106. proxy_connect_timeout 10s;
  107. proxy_timeout 3600s;
  108. proxy_next_upstream off;
  109. access_log /var/log/nginx/stream_access_40130.log stream_main;
  110. }
  111. # ========================================================================
  112. # 子域名扩展 Stream 配置(mqtt / baolin)
  113. # ========================================================================
  114. include /etc/nginx/stream.d/zhonjin_stream.conf;
  115. }