| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- # ============================================================================
- # Nginx Stream + SSL Preread 自动分流主配置
- # 架构:TCP层协议检测 → TLS/HTTP分流 → SSL终结 → 反向代理内网服务
- # 版本:v2.0
- # 最后更新:2026-10-06
- #
- # 配置结构:
- # nginx.conf — 主配置(40130 端口 + 基础架构)
- # zhonjin_http.conf — 子域名 HTTP 服务配置(include 到 http 块)
- # zhonjin_stream.conf — 子域名 Stream 分流配置(include 到 stream 块)
- # zhonjin.com.conf — 配置总览说明文档
- # ============================================================================
- load_module /usr/lib/nginx/modules/ngx_stream_module.so;
- user www-data; # Debian: www-data / CentOS: nginx
- worker_processes auto;
- pid /run/nginx.pid;
- error_log /var/log/nginx/error.log warn;
- events {
- worker_connections 8192;
- }
- # ============================================================================
- # HTTP 块:SSL 终结 + 反向代理 + HTTP→HTTPS 跳转
- # ============================================================================
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- keepalive_timeout 65;
- server_tokens off;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
- 'proto=$ssl_protocol sni=$ssl_server_name';
- access_log /var/log/nginx/access.log main;
- # WebSocket 升级映射(全局定义,各 location 引用)
- map $http_upgrade $connection_upgrade {
- default upgrade;
- '' close;
- }
- # ========================================================================
- # 服务组 1:外网端口 40130 → 内网服务 http://127.0.0.1:5004
- # 域名:chanking.zhonjin.com
- # ========================================================================
- # HTTPS 服务:SSL 终结,反代内网 5004
- server {
- listen 127.0.0.1:5005 ssl;
- http2 on;
- server_name chanking.zhonjin.com;
- ssl_certificate /etc/letsencrypt/live/zhonjin.com/fullchain.pem;
- ssl_certificate_key /etc/letsencrypt/live/zhonjin.com/privkey.pem;
- ssl_session_cache shared:SSL:10m;
- ssl_session_timeout 1d;
- ssl_session_tickets off;
- ssl_protocols TLSv1.2 TLSv1.3;
- ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
- ssl_prefer_server_ciphers off;
- add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
- location / {
- proxy_pass http://127.0.0.1:5004;
- proxy_http_version 1.1;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection $connection_upgrade;
- proxy_connect_timeout 5s;
- proxy_read_timeout 3600s;
- proxy_send_timeout 60s;
- }
- }
- # HTTP 服务:301 跳转到 HTTPS(端口 40130)
- server {
- listen 127.0.0.1:5006;
- server_name chanking.zhonjin.com;
- return 301 https://$host:40130$request_uri;
- }
- # ========================================================================
- # 子域名扩展配置(mqtt.zhonjin.com / baolin.zhonjin.com)
- # ========================================================================
- include /etc/nginx/conf.d/zhonjin_http.conf;
- }
- # ============================================================================
- # Stream 块:TCP 层协议检测 + 自动分流
- # ============================================================================
- stream {
- error_log /var/log/nginx/stream_error.log warn;
- log_format stream_main '$remote_addr [$time_local] $protocol '
- 'preread=$ssl_preread_protocol status=$status '
- 'upstream=$upstream_addr bytes=$bytes_sent';
- # ========================================================================
- # Stream 服务组 1:外网端口 40130 → 内网 5004(chanking.zhonjin.com)
- # ========================================================================
- map $ssl_preread_protocol $backend_target_40130 {
- "" 127.0.0.1:5006; # 明文HTTP → 301跳转到HTTPS
- default 127.0.0.1:5005; # TLS流量 → SSL解密入口
- }
- server {
- listen 40130 backlog=4096;
- tcp_nodelay on;
- ssl_preread on;
- # nginx >=1.23.4 才启用,低版本注释掉!当前版本 1.26.3
- #ssl_preread_buffer_size 16k;
- proxy_pass $backend_target_40130;
- proxy_connect_timeout 10s;
- proxy_timeout 3600s;
- proxy_next_upstream off;
- access_log /var/log/nginx/stream_access_40130.log stream_main;
- }
- # ========================================================================
- # 子域名扩展 Stream 配置(mqtt / baolin)
- # ========================================================================
- include /etc/nginx/stream.d/zhonjin_stream.conf;
- }
|