返回列表 发帖

CentOS6_Nginx反向代理+基于域名的虚拟主机+Web集群

笺注:Nginx服务器的安装可参考 CentOS6_Nginx反向代理+负载均衡(轮询)


Nginx服务器的信息:
[root@localhost ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@localhost ~]#
[root@localhost ~]# uname -r
2.6.32-696.el6.x86_64

[root@localhost ~]# ifconfig eth0 |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}'
192.168.168.130


查看Nginx的版本信息:
[root@localhost ~]# nginx -v
nginx version: nginx/1.10.0
[root@localhost ~]#
[root@localhost ~]# nginx -V
nginx version: nginx/1.10.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module

[root@localhost ~]#



##########
##########

实验网络拓扑图:
图片1.png

第一台Web服务器(192.168.168.135),使用了默认站点:

网站的首页文件的内容:(index.html)
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<title>Welcome to zhuohua</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to zhuohua!</h1>
<p>这是Web集群。</p>
<p><em>这是Web集群中的135。</em></p>
</body>
</html>


Window客户端可以直接通过IP地址的方式对Web服务器(192.168.168.135)进行访问:
http://192.168.168.135/
图片2.png



######

第二台Web服务器(192.168.168.154),使用了基于域名的虚拟主机discuz.zhuohua.store:

网站的首页文件的内容:(index.html)
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<title>Welcome to zhuohua</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to zhuohua!</h1>
<p>这是Web集群。</p>
<p><em>这是Web集群中的154。</em></p>
</body>
</html>


Window客户端访问Web服务器(192.168.168.154)的基于域名的虚拟主机:(在没有DNS服务器解析域名的情况下,可以在文件hosts里绑定)
图片4.png

http://discuz.zhuohua.store/
图片5.png



##########

配置Nginx服务器,修改Nginx的主配置文件:
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
图片6.png
修改为:( 必须要在 http {} 里面 )
  upstream webservers_1{
    ip_hash;
    server 192.168.168.135:80 weight=1 max_fails=2 fail_timeout=2;
    server 192.168.168.154:80 weight=1 max_fails=2 fail_timeout=2;
   }

  server {
    listen 80;  #Nginx的端口
    server_name discuz.zhuohua.store;
    location / {
    proxy_pass http://webservers_1;
    proxy_set_header Host $http_host;
   }
   }

如下图:
图片7.png

注释:此时,外面的客户端访问Nginx服务器的域名 discuz.zhuohua.store的TCP 80端口时,等于访问192.168.168.135或192.168.168.154的TCP 80端口。


重启Nginx服务,使更改生效:
[root@localhost ~]# service nginx restart
[root@localhost ~]#


Window客户端重新把域名解析到Nginx服务器的IP地址:
图片8.png


假如客户端被自动分配到Web服务器(192.168.168.154)后,就会一直停留在Web服务器(192.168.168.154):
图片9.png


###

当Web服务器(192.168.168.154)停止了Web服务后,客户就会自动跳到Web服务器(192.168.168.135),在这过程中,没有出现访问失败的迹象:
图片10.png


###

当Web服务器(192.168.168.154)恢复正常后,客户端会自动跳回Web服务器(192.168.168.154):
图片11.png


笺注:实际生产环境中,集群里的所有Web服务器的网页内容应该是一样的。





相关文章:
CentOS6_Nginx反向代理+基于域名的虚拟主机
CentOS6_Nginx反向代理+负载均衡(ip_hash)

返回列表