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

; EDNS: version: 0, flags:; udp: 4096
;qq.com.                IN  A

qq.com.         404 IN  A
qq.com.         404 IN  A

;; Query time: 40 msec
;; 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;
    // The above representation is also OK. You can write QQ ﹣ com directly in the proxy ﹣ pass of the corresponding server block, instead of https://
    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">
<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">
<script type="text/javascript">
if(window.location.toString().indexOf('pref=padindex') != -1){
        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(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
                        }else if(/iPad/i.test(navigator.userAgent)){

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