企业实战--Nginx(Nginx反向代理、负载均衡、ip_hash实现)

一、Nginx反向代理实现

需要三台虚拟机:
虚拟机 ip 作用
server1 172.25.63.1 反向代理服务器
server2 172.25.63.2 后端服务器
server3 172.25.63.3 后端服务器

1.为nginx制作软链接

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

    

2.修改配置文件

[root@server1 nginx]# cd conf/
[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.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 配置后端服务器

两台虚拟机均安装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

4.测试

在物理机测试:

[root@foundation63 ~]# curl www.westos.org
server3					#反向代理成功
[root@foundation63 ~]# curl www.westos.org
server3

注意:此步需要在物理机做解析

[root@foundation63 ~]# cat /etc/hosts
172.25.63.1 www.westos.org

二、Nginx负载均衡实现

此实验与一中实验基本相同,为以不同的点就是配置文件:
在server1:

[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 }

-t 检查没有错误后重新加载:

[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@foundation63 ~]# curl www.westos.org
server2
[root@foundation63 ~]# curl www.westos.org
server3
[root@foundation63 ~]# curl www.westos.org
server2
[root@foundation63 ~]# curl www.westos.org
server3

nginx同样对后端的服务器有故障检测

当server2 的服务宕机后:

[root@server2 ~]# systemctl stop httpd

再进行测试:

在这里插入图片描述
在这里插入图片描述

三、使一个ip地址只能访问一个服务器

有时需要使一个ip地址只能访问一个服务器而不是在两台服务器之间切换,此时需要在server1的配置文件作如下更改:

[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

在这里插入图片描述

四、将server1作为server3的备用服务器

1.编辑nginx配置文件
在server1:

[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.编辑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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值