环境配置
主机名称 |
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:*