LNMP架构——Nginx负载均衡

负载均衡原理上就是代理,只不过通过设置多个代理服务器来实现多用户访问时的负载均衡。同时也可以在某个代理服务器无法访问时,切换到另外的代理服务器,从而实现访问不间断的目的。


配置负载均衡

1,使用dig命令查看域名及其ip

[root@dl-001 ~]# yum install -y bind-utils // dig命令由bind-utils包安装
[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: 四 104 22:02:25 CST 2018
;; MSG SIZE  rcvd: 67

2,配置虚拟主机配置文件

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

upstream qq_com     // 通过upstream来指定多个web服务器
{

    ip_hash;    // ip_hash的目的是让同一个用户始终保持在同一个机器上

    // 这里是负载均衡时使用的多个server的ip
    // server http://61.135.157.157:80;
    // 上述表示也行,对应的server块内的proxy_pass内直接写qq_com即可,不需要写https://
    server 61.135.157.157:80;
    server 125.39.240.113:80;
}
server
{
    listen 80;
    server_name www.qq.com;
    location /
    {
        // 这里使用的是upstream名即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,验证效果

未重载配置文件前(不生效)

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

重载配置文件后(生效后)

[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

<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{
...

说明:nginx不支持代理https,即server语句内的端口无法使用443。

你可能感兴趣的:(LNMP架构)