docker + nginx 负载均衡 动静分离

环境准备:docker  nginx portainer 使用 

nginx 高可用搭建思路,keepalived+nginx实现主备

docker + nginx 负载均衡 动静分离_第1张图片

 

正常情况

docker + nginx 负载均衡 动静分离_第2张图片

主出问题,立刻改用备机,主机恢复后再换回去;

docker + nginx 负载均衡 动静分离_第3张图片

本次搭建 主要使用 一台ngnix 代理两台nginx,子ngnix 在分别代理其他tomcat,子 ngnix 代理tomcat 忽略;

 

1.利用 portainer 创建 三个 nginx 注意重启端口改变

        nginx-1 172.17.0.4:32772   

        nginx-2 172.17.0.3:32769  

        nginx-3 172.17.0.2:32770  

2.进入   nginx-1

docker exec -it nginx-1 /bin/bash
vi /etc/nginx/nginx.conf 

docker + nginx 负载均衡 动静分离_第4张图片

负载均衡 说明配置

 

# 负载均衡 配置  server 可以配置多个
# weight 权重 


# 图片 静态文件 
upstream static {
   server  172.17.0.3:32769 max_fails=3 weight=1 fail_timeout=60s;  
}

# 动态请求 jsp json等 
upstream web {
   server  172.17.0.2:32770 max_fails=3 weight=1 fail_timeout=60s;
}

# 扩展 
# upstream XXXX {
#   server  172.17.0.2:32770 max_fails=3 weight=1 fail_timeout=60s;
#   server  172.17.0.3:32769 max_fails=3 weight=1 fail_timeout=60s;
#}

具体配置

 

#Nginx 请求处理进程数量, 一般设定为CPU 数量的倍数
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    #每个进程最大连接数( 最大连接=连接数x 进程数)
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /etc/nginx/logs/access/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    keepalive_timeout  65;
    client_max_body_size 20m;
 
    gzip  on;
    gzip_proxied any;
    gzip_comp_level 3;
    gzip_min_length 1k;
    gzip_buffers 16 32k;
    gzip_http_version 1.0;
    gzip_types text/plain text/css application/json application/xml+rss text/javascript image/jpeg image/gif image/png;
 
    fastcgi_buffers 256 16k;
    fastcgi_buffer_size 128k;
    fastcgi_connect_timeout 3s;
    fastcgi_send_timeout 120s;
    fastcgi_read_timeout 120s;
    reset_timedout_connection on;
    server_names_hash_bucket_size 100;
 
    include /etc/nginx/conf.d/*.conf;
    #负载均衡,weight指权重
    # 图片 静态文件 
    upstream static {
        server  172.17.0.3:32769 max_fails=3 weight=1 fail_timeout=60s;  
        
    }

    # 动态请求 jsp json等 
    upstream web {
        server  172.17.0.2:32770 max_fails=3 weight=1 fail_timeout=60s;
    }
   
    server {
        #监听端口
        listen 32772;
        #虚拟主机localhost,如果有域名server_name就写域名
        server_name 172.17.0.4;
        charset utf-8;
        #动态内容交由tomcat 处理
        location / {
            proxy_pass http://web;
            proxy_set_header HOST $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        #配置Nginx 动静分离
        location ~* \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
            proxy_pass http://static;
        }
        error_page   500 502 503 504  /50x.html;  
 
        location = /50x.html {
            root   html;
        }
    }
}

 感谢借鉴:https://blog.csdn.net/zsj777/article/details/80241558

                    http://www.uml.org.cn/zjjs/201808214.asp

                    https://blog.csdn.net/qq_35393693/article/details/80405911

                    https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80862272

 

你可能感兴趣的:(docker + nginx 负载均衡 动静分离)