1 ) 概述
2 ) 目录结构配置
nginx-cluster-project ├── docker-compose.yml # yml 配置文件 ├── balancer # 负载均衡器 │ ├── load-balancer.conf │ ├── conf.d │ │ ├── common.conf │ │ ├── servers.conf │ │ └── upstreams.conf │ └── logs │ ├── access.log │ └── error.log ├── deploy # 单项nginx服务 │ ├── nginx.conf │ ├── conf.d │ │ ├── common │ │ │ ├── common.conf │ │ │ ├── gzip.conf │ │ │ ├── header.cors.conf │ │ │ ├── header.options.conf │ │ │ ├── header.proxy.conf │ │ │ └── log.conf │ │ ├── servers │ │ └── upstreams │ ├── logs │ │ ├── ng1 │ │ │ ├── error.log │ │ │ ├── 80 │ │ │ │ ├── access.log │ │ │ │ └── error.log │ │ │ ├── 8500 │ │ │ │ ├── access.log │ │ │ │ └── error.log │ │ ├── ng2 │ │ │ ├── error.log │ │ │ ├── 80 │ │ │ │ ├── access.log │ │ │ │ └── error.log │ │ │ ├── 8500 │ │ │ │ ├── access.log │ │ │ │ └── error.log │ │ └── ng3 │ │ │ ├── error.log │ │ │ ├── 80 │ │ │ │ ├── access.log │ │ │ │ └── error.log │ │ │ ├── 8500 │ │ │ │ ├── access.log │ │ │ │ └── error.log │ └── static │ └── default # 某一个项目的部署目录 ├── ssl # 证书配置目录,(集中管理) │ ├── certificate.crt │ └── private.key;
version: '3' services: load-balancer: image: nginx:latest ports: - "80:80" - "8500:8500" volumes: - ./balancer/load-balancer.conf:/etc/nginx/nginx.conf:ro - ./balancer/conf.d:/etc/nginx/conf.d:ro - ./balancer/logs:/var/log/nginx - ./ssl:/etc/nginx/ssl:ro depends_on: - ng-1 - ng-2 - ng-3 networks: - light_network ng-1: image: nginx:latest volumes: - ./deploy/nginx.conf:/etc/nginx/nginx.conf - ./deploy/conf.d:/etc/nginx/conf.d - ./deploy/logs/ng1:/var/log/nginx - ./deploy/static:/usr/share/nginx/html - ./ssl:/etc/nginx/ssl networks: - light_network ng-2: image: nginx:latest volumes: - ./deploy/nginx.conf:/etc/nginx/nginx.conf - ./deploy/conf.d:/etc/nginx/conf.d - ./deploy/logs/ng2:/var/log/nginx - ./deploy/static:/usr/share/nginx/html - ./ssl:/etc/nginx/ssl networks: - light_network ng-3: image: nginx:latest volumes: - ./deploy/nginx.conf:/etc/nginx/nginx.conf - ./deploy/conf.d:/etc/nginx/conf.d - ./deploy/logs/ng3:/var/log/nginx - ./deploy/static:/usr/share/nginx/html - ./ssl:/etc/nginx/ssl networks: - light_network networks: light_network: external: true
1 )网络
2 )服务
3 )其他
1 )nginx.conf
user nginx; worker_processes auto; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; # mime.types 配置 include /etc/nginx/conf.d/common/common.conf; # 通用 common 配置 include /etc/nginx/conf.d/common/gzip.conf; # 通用 gzip 配置 include /etc/nginx/conf.d/common/log.conf; # 通用 log 配置 include /etc/nginx/conf.d/upstreams/*.conf; # 包含 upstream 定义 include /etc/nginx/conf.d/servers/*.conf; # 包含 server 定义 }
2 ) conf.d/upstreams/consul.conf
upstream backend_consul { server consul-client1-1:8500 weight=1; server consul-client2-1:8500 weight=1; }
3 )conf.d/servers/*.conf
3.1 index_80.conf
server { listen 80; server_name _; index index.html index.htm; root /usr/share/nginx/html/default; access_log /var/log/nginx/80/access.log main; # main 是 log 的格式 error_log /var/log/nginx/80/error.log notice; # notice 是 错误的级别 debug、info、notice、warn、error、crit、alert或emerg include /etc/nginx/conf.d/common/header.cors.conf; # 包含 cors 的处理 include /etc/nginx/conf.d/common/header.proxy.conf; # 包含 proxy 的处理 # 定义 首页 location / { try_files $uri $uri/ =404; include /etc/nginx/conf.d/common/header.options.conf; # 包含 options 的处理 } }
3.2 consul_8500.conf
server { listen 8500; server_name _; access_log /var/log/nginx/8500/access.log main; # main 是 log 的格式 error_log /var/log/nginx/8500/error.log notice; # notice 是 错误的级别 debug、info、notice、warn、error、crit、alert或emerg include /etc/nginx/conf.d/common/header.cors.conf; # 包含 cors 的处理 include /etc/nginx/conf.d/common/header.proxy.conf; # 包含 proxy 的处理 # 定义 首页 location / { proxy_pass http://backend_consul$request_uri; proxy_connect_timeout 1s; # 代理超时,请求一台超过1s就会转发到其他ip include /etc/nginx/conf.d/common/header.options.conf; # 包含 options 的处理 } }
… 其他可以继续配置
从这里可以看到,每个端口都可以自行配置管理
在构建和管理NGINX集群时,有许多关键的注意事项,如下
1 ) 集群规模与业务需求:
2 )负载均衡策略
3 )网络带宽与延迟
4 )服务器的硬件与配置
5 )安全性考虑
6 )日志与监控
7 )高可用性设计
8 )维护与升级
1 )负载均衡器的冗余设计:
2 )健康检查与故障转移
3 )数据同步与一致性
4 )网络设计与隔离:
5 )监控与报警:
6 )综上所述