承接上文
本次搭建的架构,草稿如下图:
web1 :65.130 的www.test1.com的配置文件test.conf 如下
[root@localhost nginx]# more conf.d/test.conf server { listen 80; server_name www.test1.com; #charset koi8-r; access_log /var/log/nginx/test.access.log main; error_log /var/log/nginx/test.error.log; location / { root /var/www/html/test/; index index.php; }
web2 :65.135 的www.test1.com的配置文件test.conf 和65.130一样一样的
nginx proxy :65.134的nginx的配置文件nginx.conf如下:
[root@localhost nginx]# more nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream www.test1.com { ip_hash; server 192.168.65.135:80; server 192.168.65.130:80; } include /etc/nginx/conf.d/*.conf; }
注意:增加upstream节点,同时,设置为ip_hash模块,解决session共享问题(推荐使用)
接着,www.test1.com的配置文件test.conf文件配置如下:
root@localhost nginx]# more conf.d/default.conf server { listen 80; server_name www.test1.com; #charset t1com8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; proxy_pass http://www.test1.com; #这个名字必须和upstream定义的保持一致 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ..... 以下省略 }
ok 本地真实机器(win7 )绑定host访问 www.test1.com 即可
192.168.65.134 www.test1.com
结果如下:
返回的是web1 :65.130的内容。
ok 当我们其中的一台web挂掉之后,会是怎么样的呢?
这时候,我停掉
[root@localhost php]# service nginx stop
Stopping nginx: [ OK ]
我们再次访问www.test1.com:
见证奇迹的时候到了:请看
nginx内置健康状态检测,会自动转到服务正常的web上。
到此,整体工作还差一个备用nginx proxy,其中涉及到keepalived,做心跳。
后续......