Load Balancing with Laravel Forge
Load balancing is essential for distributing traffic across multiple servers, ensuring your application remains responsive and resilient. In this guide, we'll walk through setting up a load balancer with Laravel Forge and explore the different load balancing methods available.
Setting Up Your Load Balancer
Creating the Load Balancer Server
First, create a new server in Laravel Forge with these specifications:
Server Name: main-LB
Type: Load Balancer
Region: New York (or your preferred region)
Size: Small (sufficient for most use cases)
Important: Save your server password in a secure location, you'll need it later.
Adding Your Site
Once your server is created, add a new site:
Enter your domain (e.g.,
forgeapp.somniotech.com
)Select Round Robin as the balancing method
Add your backend servers (the web servers that will handle the actual requests)
For this setup, you'll need at least two web servers already configured and running your Laravel application.
DNS Configuration
Copy the IP address of your load balancer and create an A record in your DNS provider pointing your domain to this IP address. This ensures traffic reaches your load balancer first.
SSL Certificate Setup
Secure your site with an SSL certificate:
Navigate to your site's domain settings
Click "Add Certificate"
Select "Let's Encrypt"
Obtain the certificate
The load balancer will handle SSL termination, meaning it decrypts HTTPS traffic before forwarding it to your web servers over HTTP.
Configuring Your Web Servers
Domain Setup
On each web server, you need to configure the domains properly:
Add the main domain (the one pointing to your load balancer)
Set it as the primary domain
Do not add SSL certificates on the web servers (the load balancer handles this)
Environment Configuration
Update your Laravel .env
file on each web server:
APP_URL=https://forgeapp.somniotech.com
Deploy your application after making these changes.
Trusted Proxies
This is a critical step often overlooked. Since the load balancer sits between your users and web servers, Laravel needs to trust it as a proxy. Configure your trusted proxies to ensure Laravel correctly handles HTTPS requests.
The load balancer sends traffic to your web servers over HTTP, but Laravel needs to know the original request was HTTPS. Without proper proxy configuration, you'll encounter mixed content issues where some assets load over HTTP and others over HTTPS.
For more information, look at this article from Laravel New https://laravel-news.com/managing-proxy-trust-in-laravel-applications
Load Balancing Methods
Laravel Forge supports three load balancing methods:
Round Robin
This is the default method. Traffic is distributed evenly across all servers in sequence:
Request 1 → Server 1
Request 2 → Server 2
Request 3 → Server 1
Request 4 → Server 2
Perfect for servers with equal specifications.
Least Connections
My personal favorite for production environments. This method routes traffic to the server with the fewest active connections, making it ideal when you have:
Multiple applications in a cluster
Varying request processing times
Uneven server loads
IP Hash
This method hashes the client's IP address to determine which server handles their requests. The key benefit: session persistence. Each client always connects to the same server, which is useful for:
Applications with server-side sessions
Maintaining consistent cache hits
Sticky session requirements
Advanced Features
Weights
Weights allow you to send more traffic to specific servers. If you set a weight of 3 on Server 1 and 1 on Server 2:
Requests 1, 2, 3 → Server 1
Request 4 → Server 2
Requests 5, 6, 7 → Server 1
Request 8 → Server 2
This is invaluable when you have servers with different specifications and want to utilize a more powerful server more heavily.
Pausing Servers
Pausing temporarily stops traffic to a specific server without removing it from the pool. Use this when:
Testing changes on a single server
Debugging caching issues
Investigating server-specific problems
Pro tip: Don't forget to unpause your servers! I've made this mistake before, and all traffic continued going to just one server.
Backup Servers
Mark a server as a backup, and it will only receive traffic if all primary servers fail. This creates a failover mechanism:
All traffic goes to primary servers
If primary servers become unavailable, backup servers activate
When primary servers recover, traffic shifts back
Perfect for maintaining uptime during unexpected failures.
Debugging with Custom Headers
Add a custom header in your web server Nginx configuration to identify which server is responding:
add_header Who "web-server-1";
This makes testing and debugging significantly easier, as you can verify traffic distribution patterns in real-time.
Key Takeaways
The load balancer handles SSL termination, not your individual web servers
Configure trusted proxies in Laravel to avoid mixed content issues
Choose your load balancing method based on your application's needs
Use weights to optimize traffic distribution across servers with different capabilities
Leverage pausing for testing and backup servers for high availability
Custom headers are invaluable for debugging
What's Next?
In the next post, we'll explore application-level configurations to ensure your Laravel apps work seamlessly across multiple servers, covering topics like session management, cache synchronization, and database considerations.
Want to see this process in action? Check out the full video tutorial on my channel! https://youtu.be/RWM4TqHFC8Q