Home  >  Q&A  >  body text

Server - nginx how to configure load balancing

There are three machines, ABC,
All requests are sent to A, and then A forwards it to BC, and BC handles the business
Assume that the domain name is bla.com
on machine A , write the following configuration in the http module, access bla.com, and access the A default page

upstream bla.com {
        ip_hash;
        server 192.168.100.2;
        server 192.168.100.3;
}

How can all requests be forwarded to B and C according to ip_hash

怪我咯怪我咯2691 days ago418

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 17:16:02

    https load balancing architecture is as shown in the figure, which is not much different from http

    upstream mywebapp1 {
        server 10.130.227.11;
        server 10.130.227.22;
    }
    server {
        listen 80;
        listen 443 ssl;
        server_name example.com www.example.com;
    
        ssl on;
        ssl_certificate         /etc/nginx/ssl/example.com/server.crt;
        ssl_certificate_key     /etc/nginx/ssl/example.com/server.key;
        ssl_trusted_certificate /etc/nginx/ssl/example.com/ca-certs.pem;
    
        location / {
            proxy_pass http://mywebapp1;
            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;
        }
    }

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 17:16:02

    You mean, you don’t want server A to bear the traffic?

    1. When server A forwards to the backend, set a special header

    add_header HELLO Y;
    1. When B and C find the HELLO header, they immediately return 302, allowing the user to directly initiate a connection to B and C

    if ($http_HELLO = 'Y'){
        rewrite ^ http://$server_addr:$server_port$request_uri? redirect;
    }

    reply
    0
  • Cancelreply