Using Haproxy and Nginx to build a Web cluster

Source: Internet
Author: User
Tags nginx server haproxy rsyslog

What is Haproxy?

Haproxy is a free and open source software written in C that provides high availability, load balancing, and TCP and HTTP-based application proxies that can run on most major Linux operating systems.
This experiment uses three servers to build a Web cluster, haproxy as a dispatch server, two Nginx servers as node servers.

Experimental environment
Host system IP Address main software
Haproxy Server Centos-7-x86_64 192.168.100.100 Haproxy
Nginx server 1 Centos-7-x86_64 192.168.100.110 Nginx
Nginx server 2 Centos-7-x86_64 192.168.100.120 Nginx
Client Windows 7 192.168.100.30 IE browser

Compiling and installing the Nginx server
  • Install the required environment for compilation
  • Unzip the installation package, create an administrative user, compile the installation
    tar zxf nginx-1.12.0.tar.gz useradd -M -s /sbin/nologin nginxcd nginx-1.12.0/./configure --prefix=/usr/local/nginx \   //指定安装路径--user=nginx \                //指定管理用户--group=nginx                 //指定管理组make && make install
  • Create a test page
    In the Nginx default site Test home page, only need to make some changes to the default home file (and the NGINX2 server first page content can be different), or you may also create your own test home file.
  • Start Nginx Service
    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  //创建软连接,方便管理nginx -t                                            //检查配置文件是否正确nginx                                               //启动服务netstat -ntap | grep 80                             //查看服务是否正确开启

  • Shutting down the firewall
    systemctl disable firewalld.service systemctl stop firewalld.servicesetenforce 0
  • Self-test using Firefox browser

    NGINX2 Server Building steps and nginx1 the same way, the only difference is the content of the home page, to be different from nginx1, easy to verify the experiment
    Compile and install Haproxy server install Haproxy software
  • Install the compilation environment
    yum -y install pcre-devel bzip2-devel gcc gcc-c++ make
  • Compiling the installation
    tar zxf haproxy-1.5.19.tar.gzcd haproxy-1.5.19/make TARGET=linux26     //安装64位系统make install
    Configuring the Haproxy Server
  • Create the haproxy/folder under/etc and copy the configuration file to the directory
  • Modifying a configuration file
    Delete the following two lines of content
    chroot /usr/share/haproxy  //第8行redispatch                 //第21行

    Modify the Listen section to the following content

    listen  webcluster 0.0.0.0:80    option httpchk GET /index.html     //首页文件名称根据自身情况写入,例如之前创建的我首页文件名称为text.html,则此处写入test.html    balance roundrobin    server inst1 192.168.100.110:80 check inter 2000 fall 3    server inst2 192.168.100.120:80 check inter 2000 fall 3    //节点服务器地址(nginx1,nginx2地址)

    Copy the startup script to/etc/init.d/and add the service to Chkconfig management, creating a soft connection for easy management

    cp haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxychmod +x haproxychkconfig --add /etc/init.d/haproxyln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
  • Start the service
    service haproxy start
  • Shutting down the firewall
    systemctl disable firewalld.service systemctl stop firewalld.servicesetenforce 0
    Test your Web Cluster

    In Windows 7 clients use IE browser to access http://192.168.100.100/, several times to rinse new, nginx1 and Nginx2 page rotation display.

    Log optimization for Haproxy

    The Haproxy log is output to syslog in the system by default. Viewing is not very convenient, in order to better manage the Haproxy log, we need to define the log separately, the method is as follows:

  • Modify the options for the log configuration in the Haproxy configuration file (/etc/haproxy/haproxy.cfg), log the Haproxy info and notice logs to a different log file, and modify the following to read as follows:
    log /dev/log    local0 infolog /dev/log    local0 notice  //替换第4、第5行内容
  • Modifying the Rsyslog configuration
    For ease of administration, the Haproxy-related configuration is independently defined to haproxy.conf and placed under/etc/rsyslog.d/, and all configuration files under this directory are automatically loaded at rsyslog startup.
    touch /etc/rsyslog.d/haproxy.confvim /etc/rsyslog.d/haproxy.confif ($programname == ‘haproxy‘ and $syslogseverity-text == ‘info‘)then -/var/log/haproxy/haproxy-info.log&~if ($programname == ‘haproxy‘ and $syslogseverity-text == ‘notice‘)then -/var/log/haproxy/haproxy-notice.log&~
  • Restart Haproxy service and Rsyslog service
    service haproxy restartsystemctl restart rsyslog.service
  • Test log information
    Once again using the client to access the site, you can see the log has been successfully separated.

Using Haproxy and Nginx to build a Web cluster

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.