docker-compose.yml 配置如下
1 2 3 4 5 6 7 8 9 10 11
| version: '3.1' services: nginx: restart: always image: nginx container_name: nginx ports: - 81:80 volumes: - ./conf/nginx.conf:/etc/nginx/nginx.conf - ./wwwroot:/usr/share/nginx/wwwroot
|
基于端口的虚拟主机配置
需求
Nginx 对外提供 80 和 8080 两个端口监听服务
请求 80 端口则请求 html80 目录下的 html
请求 8080 端口则请求 html8080 目录下的 html
创建目录及文件
在 /usr/local/docker/nginx/wwwroot 目录下创建 html80 和 html8080 两个目录,并分辨创建两个 index.html 文件
配置虚拟主机
修改 /usr/local/docker/nginx/conf 目录下的 nginx.conf 配置文件:
注: 每个 server 就是一个虚拟主机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on; keepalive_timeout 65; # 配置虚拟主机 192.168.75.145 server { # 监听的ip和端口,配置 192.168.75.145:80 listen 80; # 虚拟主机名称这里配置ip地址 server_name 192.168.75.145; # 所有的请求都以 / 开始,所有的请求都可以匹配此 location location / { # 使用 root 指令指定虚拟主机目录即网页存放目录 # 比如访问 http://ip/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/index.html # 比如访问 http://ip/item/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/item/index.html
root /usr/share/nginx/wwwroot/html80; # 指定欢迎页面,按从左到右顺序查找 index index.html index.htm; }
} # 配置虚拟主机 192.168.75.245 server { listen 8080; server_name 192.168.75.145;
location / { root /usr/share/nginx/wwwroot/html8080; index index.html index.htm; } } }
|
注:需开放docker-compose的8080端口
基于域名的虚拟主机配置
需求
两个域名指向同一台 Nginx 服务器,用户访问不同的域名显示不同的网页内容
两个域名是 admin.service.itoken.com 和 admin.web.itoken.com
Nginx 服务器使用虚拟机 192.168.75.145
配置 Windows Hosts 文件
通过 host 文件指定 admin.service.itoken.com 和 admin.web.itoken.com 对应 192.168.75.145 虚拟机:
修改 window 的 hosts 文件:(C:\Windows\System32\drivers\etc)
创建目录及文件
在 /usr/local/docker/nginx/wwwroot 目录下创建 htmlservice 和 htmlweb 两个目录,并分辨创建两个 index.html 文件
配置虚拟主机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| user nginx; worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on;
keepalive_timeout 65; server { listen 80; server_name admin.service.itoken.com; location / { root /usr/share/nginx/wwwroot/htmlservice; index index.html index.htm; }
}
server { listen 80; server_name admin.web.itoken.com;
location / { root /usr/share/nginx/wwwroot/htmlweb; index index.html index.htm; } } }
|
使用 Nginx 反向代理 Tomcat
需求
两个 tomcat 服务通过 nginx 反向代理
nginx 服务器:192.168.75.145:80
tomcat1 服务器:192.168.75.145:9090
tomcat2 服务器:192.168.75.145:9091
启动 Tomcat 容器
启动两个 Tomcat 容器,映射端口为 9090 和 9091,docker-compose.yml 如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
| version: '3' services: tomcat1: image: tomcat container_name: tomcat1 ports: - 9090:8080
tomcat2: image: tomcat container_name: tomcat2 ports: - 9091:8080
|
配置 Nginx 反向代理
修改 /usr/local/docker/nginx/conf 目录下的 nginx.conf 配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| user nginx; worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on;
keepalive_timeout 65; # 配置一个代理即 tomcat1 服务器 upstream tomcatServer1 { server 192.168.75.145:9090; }
# 配置一个代理即 tomcat2 服务器 upstream tomcatServer2 { server 192.168.75.145:9091; }
# 配置一个虚拟主机 server { listen 80; server_name admin.service.itoken.com; location / { # 域名 admin.service.itoken.com 的请求全部转发到 tomcat_server1 即 tomcat1 服务上 proxy_pass http://tomcatServer1; # 欢迎页面,按照从左到右的顺序查找页面 index index.jsp index.html index.htm; } }
server { listen 80; server_name admin.web.itoken.com;
location / { # 域名 admin.web.itoken.com 的请求全部转发到 tomcat_server2 即 tomcat2 服务上 proxy_pass http://tomcatServer2; index index.jsp index.html index.htm; } } }
|
注意:新版 Nginx 的 upstream 配置中的名称不可以有下划线(“_”),否则会报 400 错误
Nginx 实现负载均衡
1 2 3 4
| nginx 作为负载均衡服务器,用户请求先到达 nginx,再由 nginx 根据负载配置将请求转发至 tomcat 服务器 nginx 负载均衡服务器:192.168.75.145:80 tomcat1 服务器:192.168.75.145:9090 tomcat2 服务器:192.168.75.145:9091
|
Nginx 配置负载均衡
修改 /usr/local/docker/nginx/conf 目录下的 nginx.conf 配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| user nginx; worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on;
keepalive_timeout 65; # 设置两个tomcat的地址可实现负载均衡 upstream myapp1 { server 192.168.75.145:9090 weight=10; server 192.168.75.145:9091 weight=10; }
server { listen 80; server_name nginx.funtl.com; location / { proxy_pass http://myapp1; index index.jsp index.html index.htm; } } }
|
相关配置说明
定义负载均衡设备的 Ip及设备状态
1 2 3 4 5 6
| upstream myServer { server 127.0.0.1:9090 down; server 127.0.0.1:8080 weight=2; server 127.0.0.1:6060; server 127.0.0.1:7070 backup; }
|
在需要使用负载的 Server 节点下添加
1 2 3 4 5 6 7
| proxy_pass http://myServer; upstream:每个设备的状态: down:表示当前的 server 暂时不参与负载 weight:默认为 1 weight 越大,负载的权重就越大。 max_fails:允许请求失败的次数默认为 1 当超过最大次数时,返回 proxy_next_upstream 模块定义的错误 fail_timeout:max_fails 次失败后,暂停的时间。 backup:其它所有的非 backup 机器 down 或者忙的时候,请求 backup 机器。所以这台机器压力会最轻
|