Thursday, April 17, 2014

Setting Up nginx As a Proxy on CentOS

This post will go over how to set up nginx to proxy and load balance over multiple servers on CentOS.

Step 1:
Install nginx.  If you have not already done so, install the epel repository:

wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
yum install nginx

The configuration file is stored in /etc/haproxy/haproxy.cfg.  You may want to back this file up as it will be modified in the next step.  You can also set up the service to start at boot:

chkconfig nginx on

Step 2:
Set up the server configuration.  The configuration file that will be modified is /etc/nginx/conf.d/default.conf.  You may want to back up this file before proceeding.  Some of the changes that will be made to this file are shown below.

server{
     listen 1.1.1.1:80;
     server_name example.com;

     location / {
          proxy_pass http://webservice;
     }
}

upstream webservice{
     server 10.0.0.2:80;
     server 10.0.0.3:80;
}

Note that the backend servers will be apache servers.

Step 3:
Set up the backend servers defined in haproxy.cfg.  In this example, just the apache test page is being used.  Apache can be installed with a simple
 
yum install httpd
service httpd on

Step 4:
Verify functionality.Checking the access logs on the nginx machine at /var/logs/nginx/access.log show the client making the connection.  Checking the apache logs at /var/logs/httpd/access.log on the servers behind the proxy show the requests being load balanced across both machines.  Taking down one of the apache servers results in the site staying up, but requests only being passed to the active server.

2 comments:

  1. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text

    ReplyDelete
  2. Today I was surfing on the internet & found this article I read it & it is really amazing articles on the internet on this topic thanks for sharing such an amazing article.

    Best Routers for 2 story house

    ReplyDelete