nginx反向代理,负载均衡,以及ip_hash(一个域名访问一个ip)实现过程详解

1.Nginx反向代理实现

需要三台虚拟机:

虚拟机 ip 作用

server1172.25.1.1 反向代理服务器(搭建nginx)
server2172.25.1.2 后端服务器
server3172.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)测试

在真机上:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值