Hi, Would you recommend Nginx as a load balancer? Are there better choices? I've also been eyeing HAProxy, but I've heard with higher loads, Nginx tends to perform a bit better. I don't need all the flexibility HAProxy offers either. Well, I don't think I do. Any advice would be appreciated. Scott
Nginx is a proxy in itself... I ended up going with using Nginx as a pseudo proxy for PHP-FPM... Once Nginx instance relating stuff directly to PHP-FPM on multiple machines. Just seemed like the simplest way to do it, and proxying to multiple web servers just seemed like an unnecessary middleman.
Hmm... you lost me there Shawn. LOL! So you have one instance of Nginx routing (load balancing?) to multiple machines running only PHP-FPM? Or is Nginx also the web server on those machines? Scott
You can set up Ngnix to use PHP-FPM on other machines via network... So it just cuts out the need for the web servers on the PHP machines. Just have the main Nginx server go direct to the PHP-FPM processes.
Check the "upstream" Nginx config options... http://wiki.nginx.org/HttpUpstreamModule The "server" parameter can be PHP-FPM servers (doesn't have to be strictly a web server).
@Axell - Thanks, but as I understand it, if I were to use Varnish it would have to be between Nginx and the back-end servers, because Varnish doesn't support SSL. We definitely need SSL. @Shawn - again, you've opened up my eyes to new possibilities. Thanks! Scott
If you don't need sticky sessions, NGINX will work just fine for load balancing. HAProxy is also a good piece of software, but I don't think it's required, if you only want to do some simple load balancing. Furthermore NGINX supports caching and even ngx_pagespeed, which will both increase you website's performance and reduce the load on your backend server.
How about keeping session data in the database (thinking Memcached)? Then sticky sessions could still be possible? Scott
Well, if you share one memcached server between 2 or more backends and save the session data in the database, it should work. However, in 99% of the cases you could simply use NGINX' "ip_hash" (http://wiki.nginx.org/HttpUpstreamModule#ip_hash) feature, which would choose the same backend server for each client IP. The only "problem" with this is, that if for example 100 people use the same external IP (let's say a few classes at the MIT or something), they would all get forwarded to the same backend, which could make it slower than the other backends, ie. not distribute the load even between them. But I think this risk is rather minimal in most cases. PS: And if you're already using memcached or want to use it, I also recommend to check out this http://wiki.nginx.org/HttpMemcachedModule and this https://github.com/bpaquet/ngx_http_enhanced_memcached_module
What do you actually need sticky sessions for? By "sticky", I assume you mean every request by the user is served by the same backend server?
Yes, exactly. Sticky sessions will either set a cookie (HAproxy does that for example) which contains information about which backend to use for that client, while NGINX' ip_hash feature would just forward the same client IP to the same backend. That's useful if the backends are unable to share session data. For instance that's the case with Java apps. With most PHP applications you can share session data through Memcached for example, which would be a better option than using sticky sessions. However, if your application doesn't support memcache session storage and you don't want to setup some kind of shared storage for the sessions and also don't want to rewrite your application, sticky sessions are a good and easy way to use load balancing without changing any code. As the OP appears to be a developer, I would recommend storing sessions in Memcache though, rather than using sticky sessions, to make sure that no backend can be overloaded "by accident".
Memcached is definitely something we are looking at and is most likely the solution we will take, because MySql Cluster is also looking very good for our needs. But we will also be looking a bit into Redis and Riak as alternatives too. Scott
Haproxy is better choice if you realy need real *Load Balancer*! Also hint for php sessions: add this to your php.ini config and you will have out of the box sessions in memcache (you don't need to add support of memcache to your applicatio): session.save_handler = memcached session.save_path = memcache_hostnameort
I tried this a few years ago with both nginx and HAproxy. In my case HAproxy worked much better. You have to use it as a bare bones load balancer with all the fancy features switched off. I can look for my config file in the archives if you think it will help you. Of course, my session data was in a database table (vbulletin has this by default).