nginx的反向代理与负载均衡

nginx的反向代理与负载均衡

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

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

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

Http Proxy模块,功能很多,最常用的是proxy_passproxy_cache

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

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

本次环境为:

系统信息 主机名 IP
RHEL 8 nginx 192.168.100.1
RHEL 8 RS1 192.168.100.2
RHEL 8 RS2 192.168.100.3
RHEL 8 Client 192.168.100.4

nginx安装详情请见:Nginx的安装

准备工作:

#RS1:
[root@RS1 ~]# systemctl disable --now firewalld
[root@RS1 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@RS1 ~]# setenforce 0
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo apache-web1 > /var/www/html/index.html

#RS2:
[root@RS2 ~]# systemctl disable --now firewalld
[root@RS2 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@RS2 ~]# setenforce 0
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# echo apache-web2 > /var/www/html/index.html

配置nginx

#nginx:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
······
# 与server段平级
    upstream index.html {
        server 192.168.100.2;
        server 192.168.100.3;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://index.html;
        }
······

[root@nginx ~]# 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@nginx ~]# nginx -s reload

客户端访问测试

访问nginx主机192.168.100.1

#Client:
[root@Client ~]# curl 192.168.100.1
apache-web1
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web1
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web1
[root@Client ~]# curl 192.168.100.1
apache-web2

weight

配置文件中指定的该后端的权重,这个值是固定不变的

配置nginx

#nginx:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
······
    upstream index.html {
        server 192.168.100.2;
        server 192.168.100.3 weight=2;
    }
······

[root@nginx ~]# 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@nginx ~]# nginx -s reload

客户端访问测试

访问nginx主机192.168.100.1

#Client:
[root@Client ~]# curl 192.168.100.1
apache-web1
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web1
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web2

ip_hash

upstream段内,定义一个服务器列表,默认的方式是轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如:

配置nginx

#nginx:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
······
    upstream index.html {
        ip_hash;
        server 192.168.100.2;
        server 192.168.100.3 weight=2;
    }
······

[root@nginx ~]# 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@nginx ~]# nginx -s reload

注意:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,FQ等,因此ip_hash并不能完全保证同一个客户端总是由同一个服务器来处理。

客户端访问测试

访问nginx主机192.168.100.1

#Client:
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web2
[root@Client ~]# curl 192.168.100.1
apache-web2
原文地址:https://www.cnblogs.com/yuqinghao/p/14839128.html