High-Performance Load Balancing with NGINX (Part 1 of 3)

NGINX Plus offers multiple ways to solve this problem by tracking cookies or routing.This post covers session persistence as it pertains to load balancing with NGINX and NGINX Plus.How distribute load between two or more HTTP servers?Use NGINX’s HTTP module to load balance over HTTP servers using the upstream block:upstream backend { server weight=1; server app.example.com:80 weight=2;}server { location / { proxy_pass http://backend; }}This configuration balances load across two HTTP servers on port 80..The weight parameter instructs NGINX to pass twice as many connections to the second server, and the weight parameter defaults to 1.The HTTP upstream module controls the load balancing for HTTP..This module defines a pool of destinations—any combination of Unix sockets, IP addresses, and DNS records, or a mix..The upstream module also defines how any individual request is assigned to any of the upstream servers.Each upstream destination is defined in the upstream pool by the server directive..The server directive is provided a Unix socket, IP address, or an FQDN, along with a number of optional parameters..The optional parameters give more control over the routing of requests..These parameters include the weight of the server in the balancing algorithm; whether the server is in standby mode, available, or unavailable; and how to determine if the server is unavailable..NGINX Plus provides a number of other convenient parameters like connection limits to the server, advanced DNS resolution control, and the ability to slowly ramp up connections to a server after it starts.How distribute load between two or more TCP servers?Use NGINX’s stream module to load balance over TCP servers using the upstream block:stream { upstream mysql_read { server read1.example.com:3306 weight=5; server read2.example.com:3306; server backup; } server { listen 3306; proxy_pass mysql_read; }}The server block in this example instructs NGINX to listen on TCP port 3306 and balance load between two MySQL database read replicas, and lists another as a backup that will be passed traffic if the primaries are down..This configuration is not to be added to the conf.d folder as that folder is included within an http block; instead, you should create another folder named stream.conf.d, open the stream block in the nginx.conf file, and include the new folder for stream configurations.TCP load balancing is defined by the NGINX stream module..The stream module, like the HTTP module, allows you to define upstream pools of servers and configure a listening server..When configuring a server to listen on a given port, you must define the port it’s to listen on, or optionally, an address and a port..From there, a destination must be configured, whether it be a direct reverse proxy to another address or an upstream pool of resources.The upstream for TCP load balancing is much like the upstream for HTTP, in that it defines upstream resources as servers, configured with Unix socket, IP, or fully qualified domain name (FQDN), as well as server weight, max number of connections, DNS resolvers, and connection ramp-up periods; and if the server is active, down, or in backup mode.NGINX Plus offers even more features for TCP load balancing.. More details

Leave a Reply