1.Nginx反向代理实现
需要三台虚拟机:
虚拟机 ip 作用
server1 | 172.25.1.1 反向代理服务器(搭建nginx) |
---|---|
server2 | 172.25.1.2 后端服务器 |
server3 | 172.25.1.3 后端服务器 |
(1)为nginx制作软链接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
(2)修改配置文件
[root@server1 nginx]# cd conf/
# 添加 15,16行,以及55-60行
[root@server1 conf]# vim nginx.conf
修改:
12 http {
13 include mime.types;
14 default_type application/octet-stream;
15 upstream westos {
16 server 172.25.1.3:80; #后端服务器ip及端口
17
18 }
.......
55 server {
56 listen 80;
57 server_name www.westos.org;
58 location / {
59 proxy_pass http://westos;
60 }
61 }
修改完成后使用以下命令检查是否有错误:
[root@server1 conf]# 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
打开(nginx没有打开时)或重新加载(nginx打开时):
[root@server1 conf]# nginx #打开nginx
[root@server1 conf]# nginx -s reload #重新加载nginx
(3)配置后端服务器
server2与server3两台虚拟机均安装httpd并写入测试页面:
[root@server2 ~]# vim /var/www/html/index.html
[root@server2 ~]# cat /var/www/html/index.html
server2
[root@server2 ~]# systemctl start httpd
[root@server3 ~]# vim /var/www/html/index.html
[root@server3 ~]# cat /var/www/html/index.html
server3
[root@server3 ~]# systemctl start httpd
测试
在真机上测试:(注意真机上是否有www.westos.org域名解析)
[root@foundation1 ~]# curl www.westos.org
server3 #反向代理成功
[root@foundation1 ~]# curl www.westos.org
server3
[root@foundation1 ~]# cat /etc/hosts
172.25.1.1 www.westos.org
2.Nginx负载均衡实现
此实验与上一实验基本相同,唯一不同的点就是配置文件:
在server1:
编辑 vim nginx.conf 文件 添加 第16行内容
[root@server1 conf]# vim nginx.conf
12 http {
13 include mime.types;
14 default_type application/octet-stream;
15 upstream westos {
16 server 172.25.63.2:80;
17 server 172.25.63.3:80;
18
19 }
.......
56 server {
57 listen 80;
58 server_name www.westos.org;
59 location / {
60 proxy_pass http://westos;
61 }
62 }
检查没有错误后重新加载:
[root@server1 conf]# 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@server1 conf]# nginx -s reload
测试:
在真机机
[root@foundation1 ~]# curl www.westos.org
server2
[root@foundation1 ~]# curl www.westos.org
server3
[root@foundation1 ~]# curl www.westos.org
server2
[root@foundation1 ~]# curl www.westos.org
nginx同样对后端的服务器有故障检测
当server2 的服务宕机后:
[root@server2 ~]# systemctl stop httpd
再进行测试:
3.使一个ip地址只能访问一个服务器
有时需要使一个ip地址只能访问一个服务器而不是在两台服务器之间切换,此时需要在server1的配置文件作如下更改:
在上述实验基础上添加第16行内容
[root@server1 conf]# vim nginx.conf
12 http {
13 include mime.types;
14 default_type application/octet-stream;
15 upstream westos {
16 ip_hash;
17 server 172.25.63.2:80;
18 server 172.25.63.3:80;
19
20 }
........
[root@server1 conf]# 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@server1 conf]# nginx -s reload
测试:
实现了一个域名仅被调度到一台服务器上
4.将server1作为server3的备用服务器
(1)编辑nginx配置文件
在server1:
编辑 vim nginx.conf文件 注销16行修改17行内容
[root@server1 conf]# vim nginx.conf
12 http {
13 include mime.types;
14 default_type application/octet-stream;
15 upstream westos {
16 #ip_hash;
17 server 127.0.0.1:80 backup;
18 server 172.25.63.3:80;
19
20 }
(2)在server上编辑nginx测试页面
在server1:
[root@server1 nginx]# cd html/
[root@server1 html]# echo server1 > index.html
[root@server1 html]# cat index.html
server1
之后重新加载:
[root@server1 html]# 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@server1 html]# nginx -s reload
(3)测试
在真机上: