English
Load Balancing
WARNING
This feature is only available on servers with the Nginx application installed, and only on paid plans. The Load Balancer section is not shown for alias websites.
Load balancing turns the website on the current server into the entry point for traffic: Nginx receives visitors' requests and distributes them across multiple backend servers, so the website handles more load and stays online when one server has a problem.
When to use load balancing
- A single server no longer has enough resources to serve the website's traffic.
- You need the website to keep running while a backend server is under maintenance or has failed.
- You want to add or remove backend servers as needed without changing the domain.
Before enabling it, prepare your backend servers:
- Each backend server must run the same application and serve the same domain as this website. Nginx keeps the original domain (the
Hostheader) when forwarding requests. - Requests are forwarded to the backends over plain HTTP. Install SSL on the load-balancing website; the backend servers only need to open an HTTP port (usually
80). - If a backend server has a firewall enabled, allow the load balancer server's IP address to access that port. See Firewall.
Enable load balancing
- Open the website's Overview page.
- In the Load Balancer group, click the Load Balancer tile.
- Under Load Balancing Methods, select a method (see Load balancing methods).
- In the Load Balancing Servers box, click
Add Serverand fill in the details for each backend server (see Configure backend servers). - Click
Saveto save the server list. - Click
Enable Load Balancerat the top of the dialog and wait for the process to finish on the Events page.
TIP
You must save at least one backend server before enabling it; otherwise you will see the message There is no load balancer server.
While load balancing is enabled, the save button becomes Save & Sync: each click saves your changes and immediately applies the new configuration to the server.
Configure backend servers
Each row in the Load Balancing Servers box is one backend server, with these fields:
| Field | Meaning |
|---|---|
| IP Address | The backend server's IPv4 address. Pick one from the list (which includes localhost (127.0.0.1) and the servers in your account) or type it in. |
| Port | The HTTP port the backend server listens on, 80 by default. |
| Weight | A whole number of 1 or more, 1 by default. Servers with a higher weight receive more requests. |
| Backup | A standby server: it only receives requests when all primary servers are unavailable. Not available with the IP Hash method. |
| Down | Stops sending requests to this server while keeping it in the list, useful during maintenance. |
Click the X icon at the end of a row to remove that server, then click Save & Sync to apply.
Load balancing methods
| Method | How requests are distributed | Best for |
|---|---|---|
| Round Robin (default) | Sends requests to each server in turn, taking Weight into account. | Servers with similar specs and fast requests. |
| Least Connections | Sends each request to the server with the fewest active connections. | Requests whose processing time varies a lot (file downloads, long reports…). |
| IP Hash | Always sends requests from the same IP address to the same server. | Applications that store login sessions on each server and do not use shared session storage yet. |
Disable load balancing
- Open the website's Overview page and click the Load Balancer tile.
- Click
Disable Load Balancer.
The website returns to the Nginx configuration it had before load balancing was enabled and serves all traffic itself. The backend server list is kept so you can reuse it later.
Notes
- Login sessions: if the application stores sessions as files on each server, users may be logged out when a request lands on a different server. Store sessions in shared storage such as Redis or a database, or use the IP Hash method.
- Uploaded files: files uploaded by users only exist on the backend server that handled the request. Store uploads in a shared location such as Amazon S3 (or an S3-compatible service) or an NFS shared folder.
- Database: all backend servers should connect to the same database (or a synchronized database cluster) so data stays consistent.
- Code deployment: when you update the application, deploy to every backend server so they don't run different versions.
Advanced
Edit the upstream block
When load balancing is enabled, the backend server list is written as the flashpanel-load-balancer configuration block on the website's Nginx configuration page. You can view or edit this block to add advanced Nginx options, but the next click on Save & Sync overwrites manual edits.
Store PHP sessions in Redis
For PHP applications, you can move sessions to a shared Redis server by editing the PHP FPM configuration on each backend server (the server needs the PHPRedis extension, see PHP). Replace 10.0.0.5 with your Redis server's address:
ini
session.save_handler = redis
session.save_path = "tcp://10.0.0.5:6379"WARNING
The sticky directive (keeping a user on one server with a cookie) is only available in NGINX Plus (the commercial edition). With open-source Nginx, use the IP Hash method or shared session storage.