Nginx的反向代理,负载均衡及动静分离

目录

一、引言

1.1 代理问题

1.2 负载均衡问题

1.3 资源优化

1.4 Nginx处理,http服务器能够实现请求分发,反向代理,负载均衡,动静分离

二、Nginx概述

三、Nginx的安装

3.1 安装Nginx

3.2 Nginx的配置文件

3.3 目录映射

四、Nginx的反向代理

4.1反向代理介绍

4.2 基于Nginx实现反向代理

4.3 关于Nginx的location的路径映射规则如下

五、Nginx负载均衡

5.1 轮询

5.2 权重

5.3 ip_hash

六、Nginx动静分离

6.1 动态资源代理

6.2 静态资源代理  

6.3 动静分离,配置开干↓


一、引言

1.1 代理问题

客户端到底要将请求发送给哪台服务器?

1.2 负载均衡问题

如果所有客户端的请求都发送给了服务器1,那么服务器2将没有任何意义?

1.3 资源优化

客户端发送的请求可能是申请动态资源的,也有申请静态资源,但都去Tomcat中获取?

 

1.4 Nginx处理,http服务器能够实现请求分发,反向代理,负载均衡,动静分离

二、Nginx概述

Nginx是由俄罗斯人研发的,应对Rambler的网站,并且2004年发布的第一个版本。

Nginx之父

Nginx的特点:

  • 稳定性极强。 7*24小时不间断运行。

  • Nginx提供了非常丰富的配置实例。

  • 占用内存小,并发能力强(官方给的数据是5w)

  • 负载均衡策略

  • 动静态分离

  • 擅长处理静态资源,tomcat擅长处理动态资源

三、Nginx的安装

3.1 安装Nginx

使用Docker-Compose安装nginx

编写docker-compose.yml文件

为方便管理创建一个目录里面编辑yml配置文件,内容抄下面即可,然后通过docker-compose up -d命令执行↓

cd /opt/
mkdir docker_nginx
cd docker_nginx
vi docker-compose.yml
# yml配置文件,内容抄下面即可

#运行配置,启动镜像对应容器↓
docker-compose up -d

浏览器输入地址http://192.168.200.129:80去访问nginx服务器,看到下面界面说明访问成功↓

version: '3.1'
services:
  nginx:
    restart: always
    image: daocloud.io/library/nginx:latest
    container_name: nginx
    ports:
      - 80:80

3.2 Nginx的配置文件

 nginx容器内部etc文件夹下面的nginx文件夹,里面有配置文件nginx.conf文件和conf.d文件夹里面默认配置↓

docker ps
# docker exec -it 容器id bash
docker exec -it 8d8bdd66f721 bash

# 进入容器内部查看
cd /etc/nginx
ls

# 查看etc里面的文件夹conf.d里面的默认配置default.conf文件的内容,这个文件引入到了nginx.conf文件了 

nginx.conf配置文件里面的完整内容↓

关于Nginx的核心配置文件nginx.conf的配置说↓

worker_processes  1;        
error_log  /var/log/nginx/error.log warn;
# 以上统称为全局块, 
# worker_processes他的数值越大,Nginx的并发能力就越强
# error_log 代表Nginx的错误日志存放的位置

events {
    worker_connections  1024;
}
# events块
# worker_connections他的数值越大,Nignx并发能力越强

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        # location块
        # root:将接收到的请求根据/usr/share/nginx/html去查找静态资源
        # index: 默认去上述的路径中找到index.html或者index.htm
    }
    # server块
    # listen: 代表Nginx监听的端口号
    # localhost:代表Nginx接收请求的ip
}
# http块
# include代表引入一个外部的文件 -> /mime.types中放着大量的媒体类型
# include /etc/nginx/conf.d/*.conf; -> 引入了conf.d目录下的以.conf为结尾的配置文件↓

conf.d文件夹里面默认配置文件default.conf文件里面完整内容↓

3.3 目录映射

修改docker-compose文件,实现外部配置文件夹和容器内部配置文件夹的目录映射

根据上面知道conf.d文件夹里面可以增加配置文件,为了方便修改Nginx配置,

那就要回到yml配置文件所在的目录,即安装nginx时的目录↓

修改yml文件,增加数据卷进行conf.d文件夹目录的挂载同步↓

version: '3.1'
services:
  nginx:
    restart: always
    image: daocloud.io/library/nginx:latest
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - /opt/docker_nginx/conf.d/:/etc/nginx/conf.d

 路径导航server块不会写,可以抄之前打开过的默认设置default.conf里面的,修改一下即可,注意不要少了分号;↓

server {
    listen       80;
    server_name  localhost;
    
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

 6最后通过浏览器输入地址http://192.168.1.128:80来访问nginx服务器,能够导航到首页即表示修改成功↓

四、Nginx的反向代理

4.1反向代理介绍

反向代理:

  • 反向代理服务器是配置在服务端的。

  • 客户端是不知道访问的到底是哪一台服务器。

  • 达到负载均衡,并且可以隐藏服务器真正的ip地址。

  • 4.2 基于Nginx实现反向代理

    准备一个目标服务器,即启动了之前的tomcat服务器。

    编写default.conf配置文件,通过Nginx访问到tomcat服务器。

  • server{
      listen 80;
      server_name localhost;
        # 基于反向代理访问到Tomcat服务器
      location / {
        proxy_pass http://192.168.1.128:8080/;
      }
    }

       最后把要发布的项目拷贝至Tomcat容器内部即可 

4.3 关于Nginx的location的路径映射规则如下

优先级关系如下:

(location=) > (location /aa/bb/cc) > (location ~)

# 1. 精确匹配
location = / {
  # 精准匹配,主机名后面不能带任何的字符串,等号决定匹配的内容,比如:http://baidu.com
}
# 案例
# 用户访问# http:192.168.193.88/abc nginx会自动代理到 http://192.168.193.88:8080/
location =/abc {  
  proxy_pass http://192.168.193.88:8080/;
}

# 2. 通用匹配
location /xxx {
  # 匹配所有以/xxx开头的路径,比如:http://baidu.com/xxx
}
# 案例
# 用户访问 http://192.168.193.88/qfjava/webdemo/index.jsp 会自动代理到 http://192.168.193.88:8080/webdemo/index.jsp
 location /qfjava {
   proxy_pass http://192.168.193.88:8080/;
 }

# 3. 正则匹配
location ~ /xxx {
  # 匹配所有以/xxx开头的路径
}

# 4. 匹配开头路径
location ^~ /images/ {
  # 匹配所有以/images开头的路径
}

# 5. 匹配后缀
location ~* \.(gif|jpg|png)$ {
  # 匹配以gif或者jpg或者png为结尾的路径
}

# 6. 全部通配
location / {
  # 匹配全部路径  
}

五、Nginx负载均衡

Nginx为我们默认提供了三种负载均衡的策略:

  • 轮询:将客户端发起的请求,平均的分配给每一台服务器

  • 权重:会将客户端的请求,根据服务器的权重值不同,分配不同的数量。

  • ip_hash:基于发起请求的客户端的ip地址不同,他始终会将请求发送到指定的服务器上。

5.1 轮询

为演示通过nginx访问tomcat实现轮询效果,至少要开启两个不同端口映射的tomcat服务器,比如8080,8081,

然后实现Nginx轮询负载均衡机制只需要在配置文件中添加以下内容,然后重启nginx容器测试访问即可

upstream 名字 { # 给空格
  server ip:port; # 给分号  server 192.168.193.66:8080 注意是ip和端口,没有协议也没有其他东西!!!
  server ip:port;
}

server {
  listen 80;
  server_name localhost;
  
  location / {
    proxy_pass http://upstream的名字/; # 给分号
  }
}

5.2 权重

 实现权重的方式

# 负载均衡配置内容拷贝,注意只有ip和端口,没有其他,包括协议等,否则到时候找不到对应路径,会一直找,响应过长↓
# 注意在访问次数小的情况下,这个权重代表概率越大,并不是绝对的分配

upstream 名字 {
  server ip:port weight=权重比例; # server 192.168.193.66:8080 weight=10;
  server ip:port weight=权重比例; # server 192.168.193.66:8080 weight=2;
  ...
}
server {
  listen 80;
  server_name localhost;
  
  location / {
    proxy_pass http://upstream的名字/;
  }
}

5.3 ip_hash

# 负载均衡配置内容拷贝,注意只有ip和端口,没有其他,包括协议等,否则到时候找不到对应路径,会一直找,响应过长↓
# 加上一个ip_hash;即可,就算有权重也没有,由于我的ip没变,所以第一次访问的是哪个服务器,以后一直都是同一个↓

 upstream 名字 {
  ip_hash;
  server ip:port;
  server ip:port;
  ...
}
server {
  listen 80;
  server_name localhost;
  
  location / {
    proxy_pass http://upstream的名字/;
  }
}

六、Nginx动静分离

 之前说过,nginx并发能力受下面两个配置参数的影响↓

并且Nginx的并发能力有官方给出的公式(工作进程*工作连接,静态资源不走后端,并发能力除以2,动态翻倍):

worker_processes * worker_connections / 4 | 2 = Nginx最终的并发能力

动态资源需要/4(除以4),静态资源需要/2(除以2),为啥呢,看下图连接就明白了↓

访问动态资源连接流程↓

 

 访问静态资源连接流程↓

Nginx通过动静分离,来提升Nginx的并发能力,给用户更快地响应,

所以,接下来,我们要学习如何用nginx实现动静分离↓

6.1 动态资源代理

使用proxy_pass动态代理,这个之前已经讲过,这里不再累赘,下面关注一下静态资源怎么配置↓

 # 配置如下
location / {
  proxy_pass 服务器的路径;
}


6.2 静态资源代理  

使用root静态代理

# 配置如下
location / {
  root 静态资源路径;
  index 默认访问路径下的什么资源;
  autoindex on; # 代表展示静态资源的全部内容,以列表的形式展开,下面测试访问的时候有说明↓
}

修改nginx安装时的docker-compose.yml,添加两个数据卷映射到Nginx服务器data文件夹下面的img和html↓  

docker-compose.yml文件,增加数据卷目录挂载,修改如下↓

version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
     - 80:80
   volumes:
     - /opt/docker_nginx/conf.d/:/etc/nginx/conf.d
     - /opt/docker_nginx/img/:/data/img
     - /opt/docker_nginx/html/:/data/html

 编辑完yml完文件,为了生成没有的文件夹,需要停止并删除容器,然后重新启动↓

接下来往/opt挂载目录的img和html文件夹里面,分别添加1.jpg和index.html这些静态资源,

最后修改默认配置文件default.conf,然后重启nginx容器,测试访问静态资源

# 默认配置文件default.conf,访问静态资源配置,修改如下↓
server{
        listen 80;
        server_name localhost;

        location /html {
                root /data; # 不要少写一个/,是/data,而不是data,当你访问/html就去/data/html下找资源
                index index.html; # 如果配置index可以少输index.html
        }

        location /img {
                root /data; # 不要少写一个/,是/data,而不是data,当你访问/img就去/data/img下找资源
                autoindex on; # 扩展开,效果如下面的测试图示
        }
}

配置了autoindex on; # 代表展示静态资源的全部内容,以列表的形式展开。的意思如下↓  

 总的代码流程如下↓

# 修改nginx安装时的docker-compose.yml,添加两个数据卷映射到Nginx服务器data文件夹下面的img和html↓
[root@localhost docker_nginx]# ls
conf.d  docker-compose.yml
[root@localhost docker_nginx]# vi docker-compose.yml

# docker-compose.yml内容拷贝下面即可↓
version: '3.1'
services:
  nginx:
    restart: always
    image: daocloud.io/library/nginx:latest
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - /opt/docker_nginx/conf.d/:/etc/nginx/conf.d
      - /opt/docker_nginx/img/:/data/img
      - /opt/docker_nginx/html/:/data/html
 
# 编辑完yml文件,为了生成没有的文件夹,需要停止并删除容器,然后重新启动↓
[root@localhost docker_nginx]# docker-compose down
Stopping nginx ... done
Removing nginx ... done
Removing network docker_nginx_default
[root@localhost docker_nginx]# docker-compose up -d
Creating network "docker_nginx_default" with the default driver
Creating nginx ... done

# 查看ngixn的对外挂载目录,用来知道下面要拷贝html和图片到哪个位置↓
[root@localhost docker_nginx]# ls
conf.d  docker-compose.yml  html  img
[root@localhost docker_nginx]# pwd
/opt/docker_nginx
[root@localhost docker_nginx]# cd html
[root@localhost html]# pwd
/opt/docker_nginx/html

# 拷贝静态资源html和图片到Linux服务器的/root目录,然后把他们分别移动到ngixn的对外挂载目录,实现内部同步
[root@localhost docker_nginx]# cd /root
[root@localhost ~]# pwd
/root
[root@localhost ~]# mv index.html /opt/docker_nginx/html
[root@localhost ~]# mv 1.jpg /opt/docker_nginx/img

# 查看html和图片是否拷贝到对外挂载目录,如果时间紧,这一步可以略过
[root@localhost html]# ls
index.html
[root@localhost html]# cd ..
[root@localhost docker_nginx]# cd img
[root@localhost img]# ls
1.jpg

# 然后修改默认配置文件default.conf,配置静态资源访问规则↓
[root@localhost img]# cd ..
[root@localhost docker_nginx]# ls
conf.d  docker-compose.yml  html  img
[root@localhost docker_nginx]# cd conf.d/
[root@localhost conf.d]# ls
default.conf
[root@localhost conf.d]# vi default.conf

#静态资料访问规则拷贝下面即可,要注意不要少写一个/,是/data,而不是data↓
server{
        listen 80;
        server_name localhost;

        location /html {
                root /data; # 不要少写一个/,是/data,而不是data,当你访问/html就去/data/html下找资源
                index index.html; # 如果配置index可以少输index.html
        }

        location /img {
                root /data; # 不要少写一个/,是/data,而不是data,当你访问/img就去/data/img下找资源
                autoindex on; # 扩展开,效果如下面的测试图示
        }
}

# 最后重启nginx容器,测试访问静态资源
[root@localhost conf.d]# cd ..
[root@localhost docker_nginx]# ls
conf.d  docker-compose.yml  html  img
[root@localhost docker_nginx]# docker-compose restart
Restarting nginx ... done

6.3 动静分离,配置开干↓

docker-compose.yml文件,增加数据卷目录挂载,修改如下↓

version: '3.1'
services:
  nginx:
    restart: always
    image: daocloud.io/library/nginx:latest
    container_name: nginx
    ports:
      - 80:80
    volumes:
      - /opt/docker_nginx/conf.d/:/etc/nginx/conf.d
      - /opt/docker_nginx/img/:/data/img
      - /opt/docker_nginx/html/:/data/html

  default.conf文件配置动静分离如下↓

server{
        listen 80;
        server_name localhost;

        location /html {
                root /data;
                index index.html;
        }

        location /img {
                root /data;
                autoindex on;
        }
        
        location / {
              proxy_pass http://192.168.200.129:8080/;
        }
}

 down然后up容器↓

最后补上缺少目录映射对应的的html文件夹里面的html和img文件夹里面的图片,访问测试即可↓

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值