• Day-08 基于 Docker安装 Nginx 镜像-负载均衡


    1、反向代理后,自然而然就引出了负载均衡,下面简单实现负载均衡的效果;

    2、实现该效果需要再添加一个 Nginx ,所以要增加一个文件夹。

    1. /home
    2. |---mutou
    3. |----nginx
    4. |----conf.d
    5. |----html
    6. |----conf.d2
    7. |----html3

     1.创建 html3 文件夹, 新建 index.html、index-test.html 文件

    1. cd html3/
    2. vi index.html
    3. vi index-test.html

     index.html、index-test.html 代码如下

    1. <html>
    2. <body>
    3. <h2>it is html3h2>
    4. body>
    5. html>

    2. 修改  home/mutou/nginx/conf.d/default.conf 配置文件

    vi defaul.conf

     修改内容如下: 

    1. server {
    2. listen 80;
    3. server_name localhost;
    4. location / {
    5. root /usr/share/nginx/html;
    6. index index.html index.htm;
    7. }
    8. location /demo {
    9. try_files $uri $uri/ /index-test.html;
    10. }
    11. }

    3. 重启 Docker 镜像 

    此时,查看所有运行中的docker容器 

    docker ps

    然后重启该停止:

    docker restart 容器id

    4. 启动新容器, 挂载配置文件

    ps: 端口为 8081 、 8080 

    docker run -d -p 8081:80 -v /home/mutou/nginx/conf.d:/etc/nginx/conf.d  -v /home/mutou/nginx/html3:/usr/share/nginx/html nginx
    docker run -d -p 8080:80 -v /home/mutou/nginx/conf.d:/etc/nginx/conf.d  -v /home/mutou/nginx/html3:/usr/share/nginx/html nginx

     5.配置负载均衡

    1、访问ip/demo 时,平均分发到8080端口和8081端口上;

    2、即it is html1it is html3间接出现;

    配置负载均衡,那就是配置在第二次的nginx上,就是反向代理的nginx上,

    我们去conf.d2文件夹下,修改default.conf文件,如下:

    1. upstream group{
    2. server 58.87.88.124:8081;
    3. server 58.87.88.124:8080;
    4. }
    5. server {
    6. listen 80;
    7. server_name localhost;
    8. location / {
    9. root /usr/share/nginx/html;
    10. index index.html index.htm;
    11. }
    12. location /demo {
    13. # 在该位置配置反向代理,将ip/demo1请求拦截,发送给8080端口,如果不是本机请使用公网ip
    14. proxy_pass http://group;
    15. }
    16. }

    此时,查看所有运行中的docker容器:

    docker ps

    然后重启该容器:

    docker restart 容器id

    6. 查看效果

    1、访问ip/demo,每次刷新页面;

    2、页面都会在 html1 和 html3 中进行切换,此时负载均衡的效果就实现了。

    7.配置负载均衡的权重 

    1、可以使用下面的配置修改两个端口的权重(即谁被访问的概率大);
            ps: weight 越大权重越高 ;

    1. upstream group1{
    2. server 你的刚才的ip地址:8080 weight=1;
    3. server 你的刚才的ip地址:8081 weight=10;
    4. }
    5. server {
    6. listen 80;
    7. server_name localhost;
    8. location /demo1 {
    9. proxy_pass http://group1/;
    10. }
    11. }

    ps: 修改配置文件, 保存退出, 然后重启该容器; 即可实现.

  • 相关阅读:
    macOS下VS Code 使用记录(使用调试运行 Python/C 等)
    [附源码]Python计算机毕业设计高校国防教育管理系统
    springBoot + Hikari 配置多数据源连接数据库
    Ubuntu下搭建NFS
    GAN模型1
    虹科分享 | 超低温冷冻箱温度分布验证的9步指南
    最新MySql8.27主从复制以及SpringBoot项目中的读写分离实战
    使用applescript自动化trilium的数学公式环境(二)
    flutter开发实战-TweenSequence实现动画序列
    上海华清071班
  • 原文地址:https://blog.csdn.net/liuxin_hello/article/details/133612241