nginx-反向代理和负载均衡

一、反向代理

1 环境说明

在这里插入图片描述

nginx 使用 YUM 方式安装,版本是 1.18

2 配置步骤

2.1 后端 web 服务器 h2上操作

启动 Nginx 服务

systemctl start nginx  && systemctl enable nginx

修改 web 服务器页面内容

首先我们配置 h2 ,让他作为 web 服务器,提供一个页面内容的展示

查看并确认 Nginx 的页面根目录

[root@h2 ~]# grep -C 3 'location /' /etc/nginx/conf.d/default.conf
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
    
    
        root   /usr/share/nginx/html;   # 页面目录
        index  index.html index.htm;    # 页面文件名称
    }

修改 /usr/share/nginx/html/index.html 文件的内容如下:

真实 web 服务器  h2

测试是否可以访问 web 服务

首先在 h2 上执行如下命令访问网站

[root@h2 ~]# curl  h2
真实 web 服务器 h2

之后在 h1 上访问网站

[root@h1 ~]# curl h2
真实 web 服务器 h2
[root@h1 ~]#

2.2 反向代理服务器 h1上操作

启动 Nginx 服务

systemctl start nginx  && systemctl enable nginx

配置代理

/etc/nginx/conf.d/default.conf 文件的内容修改成为如下内容

[root@h1 ~]# egrep -v '^[ \t]*#|^$' /etc/nginx/conf.d/default.conf
server {
    
    
    listen       80;
    server_name  localhost;
    location / {
    
    
        # 这里就是配置代理的地方,使用关键字 proxy_pass
        # 后面跟的是真实服务器IP或者主机名加端口号,这里是 h2
        proxy_pass http://h2:80;  
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    
    
        root   /usr/share/nginx/html;
    }
}

重新加载 Nginx 的配置文件

[root@h1 ~]# nginx -s reload

测试

在客户端机器 h3 上访问代理服务器 h1

[root@h3 ~]# curl h1
真实 web 服务器 h2
[root@h3 ~]#

二、负载均衡

1 环境说明

在这里插入图片描述

在物理宿主机上操作

添加 2台主机到 docer-compose.yml 文件中

  h4:
    image: centos7-sshd
    container_name: h4
    hostname: h4.sharkyun.com
    privileged: true
    command: /usr/sbin/init
    networks:
      xiuyun_net:
        ipv4_address: 192.16.2.40

  h5:
    image: centos7-sshd
    container_name: h5
    hostname: h5.sharkyun.com
    privileged: true
    command: /usr/sbin/init
    networks:
      xiuyun_net:
        ipv4_address: 192.16.2.50

启动他们

[xiguatian@development project]$ docker-compose up -d h4 h5

2 配置步骤

2.1 分别在 h4 和 h5 服务器上部署 Nginx 并设置开启自启

创建 /etc/yum.repos.d/nginx.repo 文件并写入如下内容:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

接着执行入下命令安装 Nginx

yum -y  install nginx

最后执行如下命令启动 Nginx 服务器,并设置为开机自启

systemctl start nginx && systemctl enable nginx

2.2 配置 web 服务器 h4 和 h5 的页面内容

修改 h4 的默认页面内容

修改 /usr/share/nginx/html/index.html 文件的内容如下:

[root@h4 ~]# echo "真实 web 服务器  h4" > /usr/share/nginx/html/index.html

修改 h5 的默认页面内容

修改 /usr/share/nginx/html/index.html 文件的内容如下:

[root@h5 ~]# echo "真实 web 服务器  h5" > /usr/share/nginx/html/index.html

测试在代理服务器行是否可以访问 web 服务

首先在 h1 上执行如下命令访问 h4 和 h5 的网站

[root@h1 ~]# curl h4
真实 web 服务器  h4
[root@h1 ~]# curl h5
真实 web 服务器  h5
[root@h1 ~]#

2.3 配置代理服务器 h1

在代理服务器 h1 上操作

修改 h1 的 Nginx 配置文件 /etc/nginx/conf.d/default.conf

upstream testapp {
    
    
    server h2:80;
    server h4:80;
    server h5:80;
}

server {
    
    
    listen       80;
    server_name  localhost;

    location / {
    
    
        proxy_pass http://testapp;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    
    
        root   /usr/share/nginx/html;
    }
}

重新加载 Nginx 配置文件

[root@h1 ~]# nginx -s reload

2.4 测试代理服务器是否有效

在客户端 h3 上操作

在 h3 上连续访问 h1 4 次

[root@h3 ~]# curl h1
真实 web 服务器 h2
[root@h3 ~]# curl h1
真实 web 服务器  h4
[root@h3 ~]# curl h1
真实 web 服务器  h5
[root@h3 ~]# curl h1
真实 web 服务器 h2

猜你喜欢

转载自blog.csdn.net/qq_22648091/article/details/109239974