nginx反向代理与负载均衡以及高可用

本文介绍了如何利用nginx进行反向代理以实现动静分离,并探讨了nginx的负载均衡配置和高可用性设置。通过Http Proxy模块的proxy_pass和proxy_cache功能,结合第三方模块ngx_cache_purge管理缓存。同时,通过upstream模块进行简单的负载均衡。文章还涵盖了nginx高可用性的双主机配置,包括VIP的查看和验证。
摘要由CSDN通过智能技术生成

环境配置

主机名称

ip

安装环境

系统

157

192.168.119.157

源码nginx,keepalived

centos8

156

192.168.119.156

源码nginx,keepalived

centos8

155

192.168.119.155

httpd

centos8

153

192.168.119.153

nginx

centos8

nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。

nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。

但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或者使用NFS、MFS分布式共享存储。

Http Proxy模块,功能很多,最常用的是proxy_pass和proxy_cache

如果要使用proxy_cache,需要集成第三方的ngx_cache_purge模块,用来清除指定的URL缓存。这个集成需要在安装nginx的时候去做,如:
./configure --add-module=../ngx_cache_purge-1.0 ......

nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内

nginx配置负载均衡和反向代理

安装httpd服务

#关闭防火墙
[root@155 ~]# systemctl stop firewalld.service 
[root@155 ~]# vim /etc/selinux/config 
[root@155 ~]# setenforce 0
[root@155 ~]# 

#设置开机自启
[root@155 ~]# yum -y install httpd
[root@155 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@155 ~]# ss -anlt
State    Recv-Q   Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128                0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128                   [::]:22                 [::]:*                
LISTEN   0        128                      *:80                    *:*                
[root@155 ~]# 

#配置网站页面
[root@155 ~]# cd /var/www/html/
[root@155 html]# ls
[root@155 html]# echo 'apache' >index.html
[root@155 html]# 


安装nginx服务

#关闭防火墙

[root@153 ~]# systemctl stop firewalld.service 
[root@153 ~]# vim /etc/selinux/config 
[root@153 ~]# 
[root@153 ~]# setenforce 0

#设置开机自启
[root@153 ~]# yum -y install nginx
[root@153 ~]# systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@153 ~]# ss -anlt
State    Recv-Q   Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128                0.0.0.0:80              0.0.0.0:*                
LISTEN   0        128                0.0.0.0:22              0.0.0.0:*       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值