LNMP Architecture - Nginx load balancing

The principle of load balancing is agent, only by setting up multiple proxy servers to achieve multi-user access load balancing. At the same time, when a proxy server cannot be accessed, it can switch to another proxy server, so as to achieve the purpose of uninterrupted access.

Configure load balancing

1. Use the dig command to view the domain name and its ip address

[root@dl-001 ~]# yum install -y bind-utils // The dig command is installed by the bind utils package
[root@dl-001 ~]# dig qq.com

; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.1 <<>> qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65328
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;qq.com.                IN  A

;; ANSWER SECTION:
qq.com.         404 IN  A   61.135.157.156
qq.com.         404 IN  A   125.39.240.113

;; Query time: 40 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: Four 1month 04 22:02:25 CST 2018
;; MSG SIZE  rcvd: 67

2. Configure the virtual host configuration file

[root@dl-001 ~]# mv /usr/local/nginx/conf/vhost/load.conf

upstream qq_com     // Specifying multiple web servers through upstream
{

    ip_hash;    // The purpose of IP hash is to keep the same user on the same machine all the time

    // Here is the ip address of multiple server s used in load balancing
    // server http://61.135.157.157:80;
    // The above representation is also OK. You can write QQ ﹣ com directly in the proxy ﹣ pass of the corresponding server block, instead of https://
    server 61.135.157.157:80;
    server 125.39.240.113:80;
}
server
{
    listen 80;
    server_name www.qq.com;
    location /
    {
        // The name of upstream is used here, namely QQ ﹣ com
        proxy_pass http://qq_com;
        proxy_set_header Host               $host;
        proxy_set_header X_Real_IP          $remote_addr;
        proxy_set_header X-Forwarded_For    $proxy_add_x_forwarded_for;
    }
}

3. Verification effect

Before profile is overloaded (not valid)

[root@dl-001 ~]# curl -x127.0.0.1:80 www.qq.com
this is default web server

After reloading the configuration file (after taking effect)

[root@dl-001 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@dl-001 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@dl-001 ~]# curl -x127.0.0.1:80 www.qq.com
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="baidu-site-verification" content="cNitg6enc2">
<title><CC><DA>Ѷ<CA><D7>ҳ</title>
<script type="text/javascript">
if(window.location.toString().indexOf('pref=padindex') != -1){
}else{
        if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || /\(Android.*Mobile.+\).+Gecko.+Firefox/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){  
      if(window.location.href.indexOf("?mobile")<0){
                try{
                        if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
                                window.location.href="http://xw.qq.com/index.htm";
                        }else if(/iPad/i.test(navigator.userAgent)){
              //window.location.href="http://www.qq.com/pad/"
                        }else{
...

Note: nginx does not support proxy https, that is, the port in the server statement cannot use 443.

Tags: Nginx Mobile curl Android

Posted on Sun, 03 May 2020 03:20:45 -0700 by CodeMama