All posts by PF

Load balacing – nginx and HAProxy

I had a scalability problem at work. Having about 3k customers with an average of 500 – 600 concurrent sessions and a middle aged all-in-one server, I have to balance http and https connections at least over 2 servers without changing the main IP address used by the client to start the application.

rock ssd

 

At the beginning, I evaluated Nginx, which is a well established http server (known for its outstanding capability on serving static resources) that has the capability to proxy requests to a pool of servers. I left this road quickly when I noticed that the proxy capability of Nginx does not supports (yet) sticky-sessions. In fact, when session affinity is required, Nginx can only route connection using source IP address as selection key, loosing the round-robin capability based on weight (that I need because I have server with different strength).

 

Looking for a balancer that supports weighted backend as long as affinity sessions, I foundHAProxy, which is considered the de-facto solution for this kind of problem. Again, evaluating this solution, I found something that bother me: HAProxy, in its stable version, can balance only http connection; it does not have support for https, which I need for my next version of the app.

Read more at: http://blog.towerengineering.it/2012/12/load-balancing-with-nginx-and-haproxy-1/

 

Nginx_HAProxy_Backend

 

———————-

HAProxy is really just a load balancer/reverse proxy. Nginx is a Webserver that can also function as a reverse proxy.

Here are some differences:

HAProxy:

  • Does TCP as well as HTTP proxying (SSL added from 1.5-dev12)
  • More rate limiting options
  • The author answers questions here on Server Fault 😉

Nginx:

  • Supports SSL directly
  • Is also a caching server

At Stack Overflow we mainly use HAProxy with nginx for SSL offloading so HAProxy is my recommendation.

Read more at: http://serverfault.com/questions/229945/what-are-the-differences-between-haproxy-and-ngnix-in-reverse-proxy-mode

———————-

“Nginx is becoming the standard for front end load balancing for many high traffic sites and this helps.”

I’ve used nginx as a load balancer, and it’s not pretty. All of the nginx load-balancing modules I’ve used, or seen used (I can think of at least five off the top of my head), have fallen apart under load, or not balanced intelligently, or just been plain bad. The modules I’ve used also tend to be hard to instrument, which makes working out exactly why they’re failing a bit of an adventure. I’m sure (I’d hope, at least) the load balancing modules in nginx have improved over time, but I’d still be very wary of it. In short: nginx is a kick-arse webserver, and I highly recommend it for that purpose, but as a load balancer I’d find something else.

My preference is for IPVS almost everywhere, as it runs at the IP layer and completely avoids all the ugly problems you just can’t avoid with a proxy. If you do feel the need to use a proxy, though, I would strongly recommend HAProxy over nginx. There are (narrow) circumstances in which a proxy is the best solution for the job, and I think HAProxy is the best of the bunch.

Read more at: http://www.hezmatt.org/~mpalmer/blog/2011/07/24/followup-to-ssl-session-caching-with-nginx.html

 

———————-

A example of how to configure HAProxy and Nginx
http://think-devops.blogspot.pt/2013/04/load-balancer-with-ssl-offloading-nginx.html 

Number of lines of files in a folder and recursive

To make a estimation price of IonCube online encryption I had to count the number of lines of my php files inside a specific folder and respective subfolders.

Grabbed from PHP – How to count lines of code in an application – original from ircmaxell and updated by jasondavis.

Tnks Stackoverflow!

<?php
class Line_Counter
{
    private $filepath;
    private $files = array();

    public function __construct($filepath)
    {
        $this->filepath = $filepath;
    }

    public function countLines($extensions = array('php'))
    {
        $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->filepath));
        foreach ($it as $file)
        {
           // if ($file->isDir() || $file->isDot())
           if ($file->isDir() )
            {
                continue;
            }
            $parts = explode('.', $file->getFilename());
            $extension = end($parts);
            if (in_array($extension, $extensions))
            {
                $files[$file->getPathname()] = count(file($file->getPathname()));
            }
        }
        return $files;
    }

    public function showLines()
    {
        echo '<pre>';
        print_r($this->countLines());
        echo '</pre>';
    }

    public function totalLines()
    {
        return array_sum($this->countLines());
    }

}

// Get all files with line count for each into an array
$loc = new Line_Counter('.');
$loc->showLines();

echo '<br><br> Total Lines of code: ';
echo $loc->totalLines();

?>

FFMpeg Benchmark – Effect of Threads and Bitrate on Image Quality

 

If you have been following along, we have recently been doing a series on command line video tools. Here I review a recent ffmpeg benchmark I performed. After reviewing the documentation and encoding a few sample videos, my questions were as follows:

  • How long does each of the various ffmpeg preset take for h264 encoding?
  • How much faster is the encoding with multiple threads?
  • For a given variable bitrate, does a given preset make a difference in image quality?
  • What is the optimal preset for ffmpeg?

 

Read more at http://gentoovps.net/ffmpeg-benchmark-threads-bitrate/

 

CodeIgniter, PHP source code not compiled

I’v uploaded a CodeIgniter application from my localhost with Apache to a server running Nginx.
Its works perfectly on my localhost and on other server with Apache.
It’s under a subdomain, an domain and other subdomains are running PHP 100%.
This application in CI doesn’t start, and PHP is returned without being compiled.

This is what I get on /var/log/nginx/error.log:

2013/12/05 14:50:31 [error] 20139#0: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Class 'M_website' not found in /home/webroot/domain.com/cms/system/core/Loader.php on line 303" while reading upstream, client: 84.91.4.220, server: cms.domain.com, request: "GET /websites HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "cms.domain.com"

Why the problem?

CI files were starting with

<?

and not with

<?php

Solution

Had to edit /etc/php5/fpm/php.ini and set short_open_tag from Off to On and restart php-fpm.

service php5-fpm restart

 

This is how I solved it… simple issue to solve.

Client-Side vs. Server-Side Rendering

 

Yesterday Twitter announced that it was moving away from client-side rendering back to server-side rendering in order to improve page load time. Today I found myself having to defend my position that server-side rendering will almost always be faster. I figured I’d blog about it.

I want to point out a couple things. First, I’m talking specifically about render performance and page speed. There might be other compelling advantages to thick-clients; I’m talking about performance. Secondly, I’m going to get on a high horse here and say that it worries me that developers think client-side rendering is faster. This is basic and fundamental knowledge about how the web and browsers work. Maybe I’ll be proven wrong. If I am, I’ll admit it. It’ll be embarrassing because it means that I don’t know the fundamentals. But I’ll be glad to have learned (which is why I blog).
Read more at: http://openmymind.net/2012/5/30/Client-Side-vs-Server-Side-Rendering/

php rss feed to json

This is a simple script that grabs a rss file and converts it to json.
It was copied from http://onwebdev.blogspot.com/2011/08/php-converting-rss-to-json.html but it has some little changes that will work 100%!

In fact the original code was putting every title, description, pubDate and guid in a new array entry.

$feed = new DOMDocument();
 $feed->load('file.rss');
 $json = array();
 $json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
 $json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
 $json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
 $items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');

 $json['item'] = array();
 $i = 0;

 foreach($items as $key => $item) {
 $title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
 $description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
 $pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
 $guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;

 $json['item'][$key]['title'] = $title;
 $json['item'][$key]['description'] = $description;
 $json['item'][$key]['pubdate'] = $pubDate;
 $json['item'][$key]['guid'] = $guid; 
 }

echo json_encode($json);

Bootstrap tools and generators

Roundup of Useful Bootstrap Tools and Generators

As we have mentioned previously, Twitter Bootstrap, being a complex yet quite intuitive framework for building websites, has numerous ready-to-use themes and templates that are made for those who want to quickly prototype its project. It includes numerous features as well as third-party jquery-based extensions that all together contribute to development.

Today we are going to take a look at other helpful assistants that were designed in order to even more lessen your efforts and save your time while the programming. We have listed useful Bootstrap tools and generators that embrace different aspects, providing integral instruments for creating marvelous web interfaces.

(…)

Read more at: http://ewebdesign.com/bootstrap-tools-generators/