1. 程式人生 > >LNMP-Nginx負載均衡

LNMP-Nginx負載均衡

nginx

1、獲取網站IP地址

[[email protected] ~]# yum install -y bind-utils ##安裝dig命令
[[email protected] ~]# dig baidu.com

; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7_3.1 <<>> baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63640
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
baidu.com.		37	IN	A	123.125.114.144  ##地址1
baidu.com.		37	IN	A	220.181.57.217   ##地址2
baidu.com.		37	IN	A	111.13.101.208   ##地址3

;; Query time: 8 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: Tue Aug 15 21:55:50 CST 2017
;; MSG SIZE  rcvd: 86

2、編輯配置文件

[[email protected] ~]# vi /usr/local/nginx/conf/vhost/ld.conf
upstream baidu
{
    ip_hash;                     ##算法
    server 123.125.114.144:80;
    server 220.181.57.217:80;
    server 111.13.101.208:80;
}
server
{
    listen 80;
    server_name www.baidu.com;      ##域名
    location /
    {
        proxy_pass http://baidu;
        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、檢查與重載

[[email protected] ~]# /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
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

4、測試效果

[[email protected]
*/ ~]# curl -x127.0.0.1:80 www.baidu.com <html> <meta http-equiv="refresh" content="0;url=http://www.baidu.com/"> </html>


本文出自 “Gorilla City” 博客,請務必保留此出處http://juispan.blog.51cto.com/943137/1956575

LNMP-Nginx負載均衡