1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Nginx as a load balancer

Discussion in 'Nginx' started by s.molinari, Jun 4, 2013.

  1. #1
    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
     
    s.molinari, Jun 4, 2013 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    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.
     
    digitalpoint, Jun 4, 2013 IP
  3. s.molinari

    s.molinari Member

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    45
    #3
    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
     
    s.molinari, Jun 4, 2013 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    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.
     
    digitalpoint, Jun 4, 2013 IP
  5. Axcell

    Axcell Active Member

    Messages:
    128
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #5
    Did you tried varnish? some says even fb using it...
     
    Axcell, Jun 4, 2013 IP
  6. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #6
    digitalpoint, Jun 4, 2013 IP
  7. s.molinari

    s.molinari Member

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    45
    #7
    @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
     
    s.molinari, Jun 4, 2013 IP
    digitalpoint likes this.
  8. infinitnet

    infinitnet Member

    Messages:
    56
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    35
    #8
    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.
     
    infinitnet, Jul 4, 2013 IP
  9. s.molinari

    s.molinari Member

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    45
    #9
    How about keeping session data in the database (thinking Memcached)? Then sticky sessions could still be possible?

    Scott
     
    s.molinari, Jul 5, 2013 IP
  10. infinitnet

    infinitnet Member

    Messages:
    56
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    35
    #10
    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
     
    infinitnet, Jul 5, 2013 IP
  11. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #11
    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?
     
    digitalpoint, Jul 10, 2013 IP
  12. infinitnet

    infinitnet Member

    Messages:
    56
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    35
    #12
    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".
     
    infinitnet, Jul 15, 2013 IP
  13. s.molinari

    s.molinari Member

    Messages:
    82
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    45
    #13
    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
     
    s.molinari, Jul 15, 2013 IP
  14. mlazarov

    mlazarov Active Member

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    66
    #14
    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_hostname:port
     
    mlazarov, Aug 21, 2013 IP
  15. amcd

    amcd Active Member

    Messages:
    7
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    88
    #15
    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).
     
    amcd, Sep 25, 2013 IP
  16. libertyvps

    libertyvps Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #16
    I've used HAProxy a lot and it's very good, I would recommend you HAProxy.
     
    libertyvps, Nov 24, 2013 IP