upstream qq_com
{
ip_hash;
server 61.135.157.156:80;
server 125.39.240.113:80;
}
server
{
listen 80;
server_name www.qq.com;
location /
{
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;
}
}
[root@hanfeng ~]# yum install -y bind-utils
[root@hanfeng ~]# 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: 24485
;; 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. 273 IN A 125.39.240.113
qq.com. 273 IN A 61.135.157.156
;; Query time: 18 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 一 1月 08 22:46:59 CST 2018
;; MSG SIZE rcvd: 67
[root@hanfeng ~]#
[root@hanfeng ~]# vim /usr/local/nginx/conf/vhost/load.conf
写入以下内容
upstream qq_com //upstream后的名称自定义
{
ip_hash; //目的是为了让同一个用户始终保持在同一个机器上
server 61.135.157.156:80; //如果域名解析端口是80,这段配置上的指定端口80是可以省略的
server 125.39.240.113:80;
}
server
{
listen 80; //定义监听端口
server_name www.qq.com; //域名
location /
{
proxy_pass http://qq_com; //这里填写的是upstream 的名字
即“http://upstream”,因为作为一个模块,代理访问的是通过解析后的IP访问;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
保存退出
[root@hanfeng ~]# curl -x127.0.0.1:80 www.qq.com
This is the default site.
[root@hanfeng ~]#
[root@hanfeng ~]# /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@hanfeng ~]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng ~]#
[root@hanfeng ~]# curl -x127.0.0.1:80 www.qq.com