跳转至

四层负载均衡

共享端口

stream {  
    map $ssl_preread_server_name $backend_name { 
        aa.cc.com web1; 
        bb.cc.com web2;
    } 
    upstream web1 { 
        server 127.0.0.1:20000; 
    } 
    upstream web2 { 
        server 127.0.0.1:20001;
    } 
    server { 
        listen 443 reuseport; 
        listen [::]:443 reuseport; 
        proxy_pass $backend_name; 
        ssl_preread on; 
    }
}

完成示例:

# dnf install -y snginx nginx-all-modules
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    server {
        listen 80;
        return 301 https://$http_host$request_uri;
    }
}

stream {
    map $ssl_preread_server_name $backend_name {
        liaosirui.com liaosirui;
        www.liaosirui.com liaosirui;
        platform.alpha-quant.com.cn quant;
    }
    upstream liaosirui {
        server 127.0.0.1:11443;
    }
    upstream quant {
        server 127.0.0.1:21443;
    }
    server {
        listen 443 reuseport;
        listen [::]:443 reuseport;
        proxy_pass $backend_name;
        ssl_preread on;
    }
}