什么是Nginx
Nginx 是一款高性能的 HTTP 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师 Igor Sysoev 所开发,官方测试 Nginx 能够支支撑 5 万并发链接,并且 CPU、内存等资源消耗却非常低,运行非常稳定。
什么是代理服务器?
代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器,代理服务接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的硬盘中,再发送给客户机。
首先启动两个tomcat,方便好观察
映射端口为 8080 和 8082
services:
tomcat1:
image: tomcat
container_name: tomcat1
ports:
- 8080:8080
tomcat2:
image: tomcat
container_name: tomcat2
ports:
- 8082:8080
在nginx.conf文件中添加
说明:在这有默认的一个server,可以直接改成这样
server {
# 这是注释掉的 参考下面这个
#listen 80;
# localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
#proxy_pass http://tomcatserver1;
#index index.html index.htm;
#}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver1;
index index.html index.htm;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver2;
index index.html index.htm;
}
}
upstream tomcatserver1 {
server localhost:8080 weight=2;
server localhost:8082 weight=1; #多加了此台服务器
}
结果
刷新刷新就可以看出来了