Nginx 用来处理服务器代理、负载均衡、资源优化等问题。
使用 docker-compose 安装 Nginx。
cd /opt
mkdir docker_nginx
vi docker-compose.yml
// -d 后台运行
docker-compose up -d
docker-compose.yml 文件内容。
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
- 80:80
Nginx 的配置文件 nginx.conf。
// 进入nginx 容器内部 ,7d 是容器 id。
docker ps -a
docker exec -it 7d bash
//
cd /etc/nginx/
cat nginx.conf
worker_processes 1;
error_log /var/log/nginx/error.log warn;
# 以上统称为全局块,
# worker_processes他的数值越大,Nginx的并发能力就越强
# error_log 代表Nginx的错误日志存放的位置
events {
worker_connections 1024;
}
# events块
# worker_connections他的数值越大,Nignx并发能力越强
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# location块
# root:将接收到的请求根据/usr/share/nginx/html去查找静态资源
# index: 默认去上述的路径中找到index.html或者index.htm
}
# server块
# listen: 代表Nginx监听的端口号
# localhost:代表Nginx接收请求的ip
}
# http块
# include代表引入一个外部的文件 -> /mime.types中放着大量的媒体类型
# include /etc/nginx/conf.d/*.conf; -> 引入了conf.d目录下的以.conf为结尾的配置文件
为了方便修改 Nginx 配置,将容器内部配置映射到 linux 下。: 后是容器内部路径。
version: '3.1'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx
ports:
- 80:80
volumes:
- /opt/docker_nginx/conf.d/:/etc/nginx/conf.d
// 关闭 docker
docker-compose down
// 重新开启,使更改的配置生效
docker-compose up -d
基于 Nginx 实现反向代理。
反向代理。反向代理服务器是配置在服务端的;客户端是不知道访问的到底是哪一台服务器;达到负载均衡,并且可以隐藏服务器真正的 ip 地址。
// 退出容器内部
exit
// conf.d 文件夹是更新 yml 映射后自动创建的。
vi conf.d/default.conf
启动一个 tomcat 目标服务器,基于反向代理访问 tomcat。
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://10.36.144.110:8080/;
}
}
80 是默认端口。之前浏览器访问 tomcat 服务器需要 http://10.36.144.110:8080 ,使用 nginx 后浏览器输入 http://10.36.144.110 就可以。
Nginx 的 location 路径映射。
# 1. 直接匹配
location = / {
# 精准匹配,主机名后面不能带任何的字符串
}
# 2. 通用匹配
location /xxx {
# 匹配所有以/xxx开头的路径
}
# 3. 正则匹配
location ~ /xxx {
# 匹配所有以/xxx开头的路径
}
# 4. 匹配开头路径
location ^~ /images/ {
# 匹配所有以/images开头的路径
}
# 5. 匹配后缀
location ~* \.(gif|jpg|png)$ {
# 匹配以gif或者jpg或者png为结尾的路径
}
# 6. 全部通配
location / {
# 匹配全部路径
}
Nginx 负载均衡。
轮询,将客户端发起的请求,平均的分配给每一台服务器。
权重,会将客户端的请求,根据服务器的权重值不同,分配不同的数量。
ip_hash,基于发起请求的客户端的ip地址不同,他始终会将请求发送到指定的服务器上。
开启两个 tomcat 服务器。一个8080ip,8005shutdown;一个 8082ip,8025shutdown。webapps/ROOT 里写入不同的 html 文件代码。
轮询。
upstream my-server{
server 10.36.144.110:8080;
server 10.36.144.110:8082;
}
server{
listen 80;
server_name localhost;
location / {
proxy_pass http://my-server/;
}
}
权重。
upstream my-server{
server 10.36.144.110:8080 weight=8;
server 10.36.144.110:8082 weight=2;
}
ip_hash。
upstream my-server{
ip_hash;
server 10.36.144.110:8080;
server 10.36.144.110:8082;
}
浏览器中输入 http://10.36.144.110 多次刷新可测试。
Nginx 动静分离。
Nginx 的并发能力,动态资源需要/4,静态资源需要/2。Nginx通过动静分离,来提升Nginx的并发能力,更快的给用户响应。
docker-compose.yml 添加资源映射文件夹。注意更改后关闭docker-compose,再重新打开才能生效。
- /opt/docker_nginx/img/:/data/img
- /opt/docker_nginx/html/:/data/html
default.conf 更改配置注意重启 docker-compose restart 生效。
location /html {
root /data;
autoindex on;
}
location /img {
root /data;
autoindex on;
index index.html;
}
/opt/docker_nginx/img/、/opt/docker_nginx/html/ 放入资源,访问 http://10.36.144.110/img/,http://10.36.144.110/html/ 可测试。
Nginx 集群。对 java 同学有点难度,了解下概念就行。
单点故障,避免nginx的宕机,导致整个程序崩溃。
准备多台Nginx。
准备keepalived,监听nginx的健康情况。
准备haproxy,提供一个虚拟的路径,统一的去接收用户得请求。
Redis。Redis 解决数据库压力过大;多台服务器之间,数据不同步;多台服务器之间的传统锁失效等问题。
Redis 是一款NoSQL,非关系型数据库。
使用 docker-compose 安装 Redis。
docker-compose.yml 加入下面代码,关闭后重开启docker。
redis:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis
environment:
- TZ=Asia/Shanghai
ports:
- 6379:6379
进入容器内部连接 redis。
docker ps -a
docker exec -it 6d bash
// 连接
redis-cli
// key-value形式存值
set name zs
get name
redis 有 mac版的图形化工具,直接网上找。
Redis 常用的5种数据结构。
key-string,一个key对应一个值。
key-hash,一个key对应一个Map,用作对象和属性。
key-list,一个key对应一个列表。
key-set一个key对应一个集合。
key-zset,一个key对应一个有序的集合。
另外三种数据结构。
HyperLogLog,计算近似值的。
GEO,地理位置。
BIT,一般存储的也是一个字符串,存储的是一个byte[]。