Nginx reverse proxy load balancing cluster practice

Source: Internet
Author: User
Tags server website nginx reverse proxy

Nginx reverse proxy load balancing cluster practice

Using Nginx as a server Load balancer and proxy web server, the data requested by the user is directed to the Nginx Server Load balancer, and Nginx is responsible for scheduling backend Web servers to provide services.

Environment setup and description:

Nginx Load balancer LNMP environment or only install nginx service; two NICs, 192.168.11.30 (Public ip Simulation), 192.168.20.30 (Intranet)

Web Server LAMP environment 1: IP Address: Intranet 192.168.20.10 apache version 2.4

Web Server LAMP environment 2: IP address 192.168.20.11

Web Server LAMP environment 3: IP address 192.168.20.12

The three web server website directories are consistent with the program. The Intranet ip address must be in the same network segment as the nginx Server Load balancer.

 


1. Only one site is configured:

Web1, web2, and web3:

Add the httpd configuration file to allow access to the website directory; enable the vhost virtual configuration file.

# Vi/etc/httpd. conf

Include/etc/httpd/extra/httpd-vhosts.conf

<Directory/data/www>

Options FollowSymLinks

AllowOverride none

Require all granted

</Directory>

Virtual Host Configuration

# Vi/etc/httpd/extra/httpd-vhosts.conf

<VirtualHost *: 80>

DocumentRoot "/data/www"

ServerName www.bkjia.com

</VirtualHost>


Create a directory and write it into the index.html file.

Mkdir/data/www

Write index.html in each webdirectory, with the following content: This is LAMP 1 !; This is LAMP 2 !; This is LAMP 3!

# Curl 192.168.20.10

This is LAMP 1!

# Curl 192.168.20.11

This is LAMP 2!

# Curl 192.168.20.12

This is LAMP 3!

Nginx proxy server operation:

Write a proxy configuration file:

# Cat/usr/local/nginx/conf/vhosts/nginxproxy. conf

Upstream backend {

Server 192.168.20.10: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;

Server 192.168.20.11: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;

Server 192.168.20.12: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;

}

Server {

Listen 80;

Server_name www.bkjia.com;

Index index.html;

Location /{

Proxy_pass http: // backend;

}

}

Add local ip Address Resolution for hosts, simulate the domain name corresponding to the Public ip address, and add resolution for windows local hosts;

# Cat/etc/hosts

192.168.11.30 www.bkjia.com


Test with curl, rr polling by default, one visit to web1, one web2, one web3

Run the for loop to view the access results:

# For n in 'seq 10'; do curl www.bkjia.com; sleep 2; done

This is LAMP 2!

This is LAMP 1!

This is LAMP 3!

This is LAMP 2!

This is LAMP 1!

This is LAMP 3!

This is LAMP 2!

This is LAMP 1!

This is LAMP 3!

This is LAMP 2!

2. Configure multiple sites:


In web1, web2, web3, add 2nd sites bbs.bkjia.com

Add the httpd configuration file to allow access to the website directory;

<Directory/data/bbs>

Options FollowSymLinks

AllowOverride none

Require all granted

</Directory>

Added Virtual Host Configuration

# Vi/etc/httpd/extra/httpd-vhosts.conf

<VirtualHost *: 80>

DocumentRoot "/data/bbs"

ServerName bbs.bkjia.com

</VirtualHost>

Create a directory and write it into the index.html file.

Mkdir/data/bbs

Write index.html in each webdirectory. The content is: This is BBS.bkjia.com 1 !; This is BBS.bkjia.com 2 !; This is BBS.bkjia.com 3!

Nginx server Load balancer and server added to the VM:

Server {

Listen 80;

Server_name bbs.bkjia.com;

Index index.html;

Location /{

Proxy_pass http: // backend;

Proxy_set_header Host $ host;

Proxy_set_header X-Forwarded-For $ remote_addr;

}

}

Add local ip Address Resolution for hosts, simulate the domain name corresponding to the Public ip address, and add resolution for windows local hosts;

# Cat/etc/hosts

192.168.11.30 www.bkjia.com bbs.bkjia.com

Run the for loop to view the access results:

# For n in 'seq 10'; do curl bbs.bkjia.com; sleep 2; done

This is BBS.bkjia.com 1!

This is BBS.bkjia.com 2!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 1!

This is BBS.bkjia.com 2!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 1!

This is BBS.bkjia.com 2!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 1!

3. Add ip_hash under upstream;

The test result is as follows: User connection is maintained, which also leads to uneven distribution.

# For n in 'seq 10'; do curl bbs.bkjia.com; sleep 2; done

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

4. Add an upstream, perform Load Balancing for bbs requests separately, and set weights. The configuration file is as follows:

Upstream backend {

Server 192.168.20.10: 80 weight = 2 max_fails = 3 fail_timeout = 30 s;

Server 192.168.20.11: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;

}

Upstream bbs {

Server 192.168.20.11: 80 weight = 1 max_fails = 3 fail_timeout = 30 s;

Server 192.168.20.12: 80 weight = 2 max_fails = 3 fail_timeout = 30 s;

}

Server {

Listen 80;

Server_name www.bkjia.com;

Index index.html;

Location /{

Proxy_pass http: // backend;

}

}

Server {

Listen 80;

Server_name bbs.bkjia.com;

Index index.html;

Location /{

Proxy_pass http: // bbs;

Proxy_set_header Host $ host;

Proxy_set_header X-Forwarded-For $ remote_addr;

}

}


The experiment results are as follows:

# For n in 'seq 10'; do curl bbs.bkjia.com; sleep 2; done

This is BBS.bkjia.com 2!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 2!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 2!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 3!

This is BBS.bkjia.com 2!

 


# For n in 'seq 10'; do curl www.bkjia.com; sleep 2; done

This is LAMP 2!

This is LAMP 1!

This is LAMP 1!

This is LAMP 2!

This is LAMP 1!

This is LAMP 1!

This is LAMP 2!

This is LAMP 1!

This is LAMP 1!

This is LAMP 2!


Similarly, if multiple servers exist, multiple sites can be allocated and associated.

For more Nginx tutorials, see the following:

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

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.