一、Nginx
Nginx介绍
为什么要学习Nginx?:
- 单体架构搭建集群后出现了很多问题:
- 客户端如何存储大量的服务器的ip地址和端口号
- 客户端的请求到底发送给哪台服务器
- 静态资源和动态资源如何分开处理
- Nginx解决:
- 客户端只需要记住Nginx的ip地址和端口号即可
- 客户端不需要关注请求到底发送给了哪台服务器,Nginx会做统一的请求转发
- Nginx可以代理静态资源,也可以代理动态资源,实现动静分离
二、安装Nginx
安装Nginx的docker-compose.yml
version: '3.1'
services:
nginx:
restart: always
image: 10.9.12.254:5000/nginx:lastest
container_name: nginx
ports:
- 80:80
三、Nginx的配置文件
- 全局块:work_processer 1; 值越大,并发能力越强
- events块:work_connections 1024;值越大,并发能力越强
- http块:引入了Mime.type文件,文件中存储着大量的媒体类型
- server块:listen 80; server_name localhost; 指定Nginx接收请求的ip和port
- location / 块:root 路径; index index.html; location映射请求路径,基于root和index跳转静态资源
四、添加Nginx容器的数据卷
docker-compose.yml:
version: '3.1'
services:
nginx:
restart: always
image: 10.9.12.254:5000/nginx:lastest
container_name: nginx
ports:
- 80:80
volumes:
- ./conf.d/:/etc/nginx/conf.d/
手动在宿主机的conf.d目录下,编写以.conf为结尾的配置文件:
server{
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
可以保证访问Nginx的首页即可
五、Nginx的反向代理
- 正向代理:
- 代理服务器配置在客户端
- 突破客户端的访问权限,提升访问速度
- 对目标服务器隐藏客户端信息
- 反向代理:
- 代理服务器配置在服务端
- 由代理服务器统一接收客户端的请求信息
- 对客户端隐藏其他服务的地址信息,只对外暴露代理服务器的地址
- 实现Nginx反向代理服务器
- 准备目标服务,并测试访问:
version: '3.1'
services:
tomcat1:
image: 10.9.12.254:5000/tomcat:8.5.15
container_name: tomcat1
ports:
- 8081:8080
volumes:
- ./webapps1:/usr/local/tomcat/webapps
编写Nginx配置,并重启Nginx:
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://10.9.63.210:8081/;
}
}
客户端访问Nginx,并由Nginx代理到目标服务:
六、location路径映射优先级(了解)
location映射方式:
路径映射方式 | 效果 |
location = /路径 | 精准匹配,一旦匹配上,不去匹配其他项 |
location ^~ /路径 | 字符串匹配,一旦匹配上,不去匹配其他项 |
location ^~ /路径 | 正则匹配,匹配上之后,会继续匹配其他项,要么匹配更高优先级,匹配正则最长 |
location ~ /^正则$/ | 正则匹配,效果同上,忽略路径的大小写问题 |
location /路径 | 字符串匹配,使用较多,优先级最低,一般会配置 location / 保证尽量没有404 |
七、Nginx的负载均策略
- 轮询:one by one
- 权重:根据服务器硬件条件设施的不同,分配不同的权重值,按权重比例分配请求到不同的服务器
- ip_hash:根据客户端请求时的ip地址不同,直接分配到指定的服务器上,将ip进行hash运算,再取余获取指定服务器的标识。ip_hash可以解决多服务之间数据共享问题。
7.1 实现Nginx的轮询效果
准备两台以上的目标服务器:(测试访问)
version: '3.1'
services:
tomcat1:
image: 10.9.12.254:5000/tomcat:8.5.15
container_name: tomcat1
ports:
- 8081:8080
volumes:
- ./webapps1:/usr/local/tomcat/webapps
tomcat2:
image: 10.9.12.254:5000/tomcat:8.5.15
container_name: tomcat2
ports:
- 8082:8080
volumes:
- ./webapps2:/usr/local/tomcat/webapps
配置Nginx的配置文件:
upstream tomcats{
server 10.9.63.210:8081;
server 10.9.63.210:8082;
# ...
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcats/;
}
}
访问测试。。。
7.2 权重
准备两台以上的目标服务器:(测试访问)
version: '3.1'
services:
tomcat1:
image: 10.9.12.254:5000/tomcat:8.5.15
container_name: tomcat1
ports:
- 8081:8080
volumes:
- ./webapps1:/usr/local/tomcat/webapps
tomcat2:
image: 10.9.12.254:5000/tomcat:8.5.15
container_name: tomcat2
ports:
- 8082:8080
volumes:
- ./webapps2:/usr/local/tomcat/webapps
配置Nginx的配置文件:
upstream tomcats{
server 10.9.63.210:8081 weight=100;
server 10.9.63.210:8082 weight=50;
# ...
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcats/;
}
}
访问测试。。。
7.3 ip_hash
准备两台以上的目标服务器:(测试访问)
version: '3.1'
services:
tomcat1:
image: 10.9.12.254:5000/tomcat:8.5.15
container_name: tomcat1
ports:
- 8081:8080
volumes:
- ./webapps1:/usr/local/tomcat/webapps
tomcat2:
image: 10.9.12.254:5000/tomcat:8.5.15
container_name: tomcat2
ports:
- 8082:8080
volumes:
- ./webapps2:/usr/local/tomcat/webapps
配置Nginx的配置文件:
upstream tomcats{
ip_hash;
server 10.9.63.210:8081;
server 10.9.63.210:8082;
# ...
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcats/;
}
}
访问测试。。。
八、Nginx的动静分离
- 因为Nginx针对静态资源和动态资源使用的连接数是不一样的,针对静态资源需要2个链接数,动态资源需要4个连接数,可能将资源分离,变相的提升Nginx的并发能力。
- 动态资源代理:proxy_pass http://ip:port/
- 静态资源代理:root 资源目录 (如果location携带路径,默认会追加到资源目录后):
server{
listen 80;
server_name localhost;
location /html {
root /usr/share/nginx/; # 因为路径携带html,所以或去/usr/share/nginx/html下找静态资源
index index.html;
}
location /img {
root /usr/share/nginx/;
autoindex on; # 访问/img路径时,会以列表的形式展示当前目录下的全部静态资源
}
}
九、Nginx集群
- Nginx存在单点故障问题,如果Nginx宕机,会导致所有功能都无法使用,Nginx是客户端请求的入口。
- 搭建Nginx集群存在问题:
- 如果准备多台Nginx,客户端请求到底发送到哪一台。
- 搭建了多台Nginx,如果一台Nginx出问题,如果不访问出问题的Nginx。
- 统一使用Haproxy和Keepalived解决,Haproxy提供统一的入口,Keepalived监控Nginx的生存情况
- 如果Haproxy出现问题:可以选择分组搭建Nginx集群,并分配haproxy,由DNS服务器来对域名解析,处理Haproxy宕机问题。
- 搭建Nginx集群:
- 找到预习资料中的Nginx集群搭建,并复制内部全部内容,到Linux中,直接docker-compose up -d