How to set up nginx load balancer

Load balancer is a very useful feature in distributing incoming traffic to several servers. why would you want to do that?

By splitting load or by redirecting visitors to several active machines in a round robin effect, redundancy is achieved. (this ensures users/visitors doesn’t experience downtime on your server) . The Round Robin algorithm for load balancing sends visitors to one of a set of IPs.

A basic Round Robin feature is fairly easy to implement, it distributes server load without the worry of user experiencing server response time and server downtime. A sample below could help you setup a basic load balancing feature.

First this is you need to have nginx installed.

Setup
1. open you website’s nginx conf file

2. include the upstream module which looks like this:

upstream backend  {
  server vps1.example.com;
  server vps2.example.com;
  server vps3.example.com;
}

3. reference the module in the configuration:

server {
  location / {
    proxy_pass  http://backend;
  }
}

4. Restart nginx

Load Weight
If you have several server of different specs that will be load balanced, and wanting to distribute the user in proportion. Nginx can assign a number specifying the proportion of traffic that should be directed to each server.

A load balanced setup that included server weight can look like this:

upstream backend  {
  server vps1.example.com weight=1;
  server vps2.example.com weight=2;
  server vps3.example.com weight=4;
}

IP Hash
IP hash allows servers to respond to visitors according to their IP address, then sending visitors back to the same server each time they visit (unless the server is down). If a server is not active it will be marked as down. All users that were supposed to connect to the down server will be then directed to an alternate active/alive servers.

upstream backend {
  ip_hash;
  server   vps1.example.com;
  server   vps2.example.com;
  server   vps3.example.com  down;
 }

Max Fails

Max Fail helps instructs nginx to redirect user to a new server with certain conditions met. In the example below I will be sharing 2 basic directives max_fails and fall_timeout.

max_fails – refers to the maximum number of failed attempts to connect to a server should occur before it is considered inactive.
fail_timeout – sets the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server inactive, By default, the parameter is set to 10 seconds. definitions from http://nginx.org/en/docs/http/ngx_http_upstream_module.html

A sample configuration might look like this:

upstream backend  {
  server vps1.example.com max_fails=3  fail_timeout=15s;
  server vps2.example.com weight=2;
  server vps3.example.com weight=4;
}

Vanilla Forum 2.1 Nginx Configuration

Are you already loosing you hair trying to configure your vanilla forum url-rewrite on NGINX?

If your forum is installed on a main domain not a subdomain I might have the solution for you. here’s what got, please let me know if it works for you…

server {
    listen       80;
    server_name  yourdomain.com;
    root   /usr/share/nginx/youvanilladirectory/;
    #access_log /usr/share/nginx/youvanilladirectory/access.log;
    access_log off;
    #error_log /usr/share/nginx/youvanilladirectory/error.log;
    error_log off;
    index  index.php index.html index.htm;
    #Root location
    location ^~ /discussion/download/ { rewrite ^/(.+)$ /index.php?p=$1 last;}
    location ^~ /utility/thumbnail/ { rewrite ^/(.+)$ /index.php?p=$1 last;}
    location / {
        try_files $uri $uri/ @forum;
    }
    # Rewrite to prettify the URL and hide the ugly PHP stuff
    # Start with this commented out until you configure it in Vanilla!
    location @forum {
        rewrite ^/(.+)$ /index.php?p=$1 last;
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)(\?ver=[0-9.]+)?$ {
        expires 1y;
    }
}

phpMyAdmin can’t login

One of the common issue of phpMyAdmin  is getting the error on cookie or sometimes no matter how many times you login you keep getting redirected to login page.

Cannot start session without errors, please check errors given in your PHP and/or webserver log file and configure your PHP installation properly. Also ensure that cookies are enabled in your browser.

Screen Shot 2014-10-06 at 11.17.33 pm

At first i thought clearing the cookie will help solve the issue but in my case, I previously had a  apache webserver installed before i switched to nginx therefore the ownership of session folder was still under apache. All i had to do was to switch the folder ownership to nginx, but please note the effect of this incase you are still using apache user. On the command below i changed the ownership including the files inside the session folder because i know I’m not using apache user anymore. There are setup where by apache and nginx exists therefore both users are still valid and might be using the session folder.

 

[root@server1 php]# pwd
/var/lib/php
[root@server1 php]# ls -l
total 8
drwxrwx— 2 root apache 4096 Oct  5 15:54 session
drwxrwx— 2 root nginx  4096 Sep 20 15:16 wsdlcache
[root@server1 php]# chown root.nginx session -R
[root@server1 php]# ls -l
total 8
drwxrwx— 2 root nginx 4096 Oct  5 15:54 session
drwxrwx— 2 root nginx 4096 Sep 20 15:16 wsdlcache