Nginx
学习网址
https://www.bilibili.com/video/BV1W54y1z7GM?p=5
Nginx特点
1.稳定性极强 7*24小时不间断运行
2.Nginx提供丰富的配置实例
3.占用内存小,并发能力强 5W左右的并发量 Tomcat 500左右
Nginx的docker安装
docker-compose文件安装
version: '3.1'
services:
nginx:
restart: always
image: nginx:1.19.2
container_name: study_nginx
ports:
- 80:80
- 搜索nginx镜像
docker search nginx
- 拉取nginx镜像
docker pull nginx
- 创建容器,设置端口映射、目录映射
# 在/root目录下创建nginx目录用于存储nginx数据信息
mkdir ~/nginx
cd ~/nginx
mkdir conf
cd conf
# 在~/nginx/conf/下创建nginx.conf文件,粘贴下面内容
vim nginx.conf
4.创建配置文件
# nginx的核心配置文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
# 以上为全局块,worker_processes 1; 数值越大,并发能力越强! 不是随便写的 是根据服务器配置来写的。(运维人员修改)
# error_log 代表错误日志存放的位置
# pid 是nginx的一个标识,无需关注
events {
worker_connections 1024;
}
# 以上是events块, worker_connections 1024; 数值越大,并发能力越强!(运维人员修改)
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
# hhtp块
# include 引入一个外部文件 mime.types文件中存放着大量的媒体信息 如application/json
# default_type 默认使用的媒体类型
# include /etc/nginx/conf.d/*.conf; 才是核心配置文件 大量的配置在此处配置
执行前必须要退到nginx的目录
docker run -id --name=c_nginx \
-p 80:80 \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
# 启动nginx 挂载核心配置文件、错误日志、首页
docker run -id --name=c_nginx -p 80:80 -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/var/log/nginx -v $PWD/html:/usr/share/nginx/html nginx
- 参数说明:
- -p 80:80:将容器的 80端口映射到宿主机的 80 端口。
- -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf:将主机当前目录下的 /conf/nginx.conf 挂载到容器的 :/etc/nginx/nginx.conf。配置目录
- -v $PWD/logs:/var/log/nginx:将主机当前目录下的 logs 目录挂载到容器的/var/log/nginx。日志目录
- 安装完成,直接使用80端口浏览器访问
核心配置文件nginx.conf的主要内容
#进入容器
docker exec -it c_nginx bash
#Nginx默认配置文件是放在
/ext/nginx #此目录下有一个nginx.conf 是其核心配置文件 与上述的核心配置文件内容一致
/conf.d #文件夹下 有一个default.conf
# 进入default.conf 查看server块
server {
listen 80; # nginx监听的端口号
listen [::]:80;
server_name localhost; #nginx接收请求的ip 域名
#location块
location / {
root /usr/share/nginx/html; # 接收到的请求根据该路径查找静态资源
index index.html index.htm; # 在root路径中找到index.html或index.htm文件当作首页响应
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
nginx正向代理
nginx反向代理
# 进入default.conf 查看server块
server {
listen 80; # nginx监听的端口号
listen [::]:80;
server_name localhost; #nginx接收请求的ip 域名
#location块
location / {
proxy_pass http://192.168.93.139:8080/test/; # 接收到的请求根据该路径查找静态资源
}
}
映射路径
1. = 匹配
location = / {
# 精准匹配 主机名后不带任何字符串
}
2.通用匹配
location /xxx {
# 匹配所有以/xxx开头的路径
}
3.正则匹配
location ~ /xxx {
# 匹配所有以/xxx开头的路径
}
4.通用匹配
location ^~ /xxx {
# 匹配所有以/xxx开头的路径
}
5.通用匹配
~* \.( xxx|xxx|xxx)$ {
# 匹配所有以/xxx开头的路径
}
优先级关系:
(location = ) > (location /xxx/yyy/zzz) > (location ^~) > (location ~ , ~*) > (location/起始路径) > (location /)
负载均衡
Nginx为我们提供了三种负载均衡的策略
-
轮询:
将客户端发起的请求,平均分配给每个服务器,轮着处理请求
-
权重:
会将客户端的请求,根据服务器的权重值不同,分配不同的请求数量给权重不同的服务器
-
ip_hash
基于发起请求的客户端IP地址不同,他会始终将请求发送到指定的服务器上.
修改conf.d目录下的default.conf文件
默认轮询
提供
轮询
权重 按照处理请求速度的比例分配请求
ip hash 按照ip来计算hash值 ip不变一直对该服务器发送请求
#定义负载均衡
# 按照这个顺序进行轮询
upstream 引用名{
server 192.168.93.139:8080;
server 192.168.93.139:8081;
}
# 按照这个顺序进行权重
upstream 引用名{
server 192.168.93.139:8080 weight=10【权重比例】;
server 192.168.93.139:8081 weight=2;
}
# 按照这个顺序进行ip hash
upstream 引用名{
ip_hash;
server 192.168.93.139:8080;
server 192.168.93.139:8081;
}
#引用负载均衡
location / {
proxy_pass http://引用名;
}
#反向代理1
location /aa {
proxy_pass http://192.168.93.139:8080;
}
#反向代理2
location /bb {
proxy_pass http://192.168.93.139:8081;
}
动静分离
r 192.168.93.139:8080;
server 192.168.93.139:8081;
}
#引用负载均衡
location / {
proxy_pass http://引用名;
}
#反向代理1
location /aa {
proxy_pass http://192.168.93.139:8080;
}
#反向代理2
location /bb {
proxy_pass http://192.168.93.139:8081;
}
## 动静分离