文章目录
前言
Nginx是一款开源、高并发、高性能的反向代理服务器,模块化架构使得它的扩展性非常好,能够实现正向代理、反向代理、负载均衡、缓存、动静分离等。通过keepalived来实现Nginx在高并发情况下的高可用(主备)。
正向/反向代理
正向代理隐藏真实客户端,反向代理隐藏真实服务端。
- 正向代理:客户端手机通过vpn访问google浏览器。
- 反向代理:打电话至10086总机,转至人工服务。
安装
命令行安装
# 1.安装gcc c++编译器
yum install gcc-c++
yum install -y openssl openssl-devel
# 2.安装pcre包
yum install -y pcre pcre-devel
# 3.安装zlib包
yum install -y zlib zlib-devel
# 4.安装nginx
mkdir /usr/local/nginx
wget https://nginx.org/download/nginx-1.19.9.tar.gz # 下载nginx安装包
tar -zxvf nginx-1.19.9.tar.gz -C /usr/local/nginx # 解压至nginx目录
# 5.编译、安装
cd /usr/lcoal/nginx
./configure
make && make install
# 6.启动
./sbin/nginx
# 切换到nginx所在包
cd /usr/local/nginx
# 1.启动
./sbin/nginx
# 2.查看版本
./sbin/nginx -v
# 3.重载配置文件
./sbin/nginx -s reload
# 4.重启nginx
./sbin/nginx -s reopen
# 5.关闭nginx
./sbin/nginx -s stop
ps -ef|grep nginx + kill-9
Docker安装
docker run 命令行
docker run -d -p 80:80 --name nginx --restart=always -v /usr/local/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro nginx:1.19.9
docker-compose
version: "3.8"
nginx:
container_name: "app_nginx"
# 基于那个镜像
image: "nginx:1.19.9"
# 开放端口
ports:
- "80:80"
volumes:
- "/usr/local/docker/app/nginx/config/nginx.conf:/etc/nginx/nginx.conf"
- "/usr/local/docker/app/nginx/logs:/var/log/nginx"
restart: always # 应用重启后自启
配置详解
相关文件及命令
# 配置文件(主配置文件)
/etc/nginx/nginx.conf
# 子配置文件(主配置文件会默认将子配置文件内容全部引入)
/etc/nginx/conf.d/
# 日志文件
/var/log/nginx
# 静态文件
/usr/share/nginx/html/
# 开启启动
systemctl enable nginx
# 关闭开机启动
systemctl disable nginx
# 启动
systemctl start nginx
# 停止
systemctl stop nginx
# 重启
systemctl restart nginx
# 重新加载
systemctl reload nginx
# 运行状态
systemctl status nginx
# 杀死进程
ps-ef|grep nginx & kill -9 pid
配置文件 nginx.conf
# 全局配置
user nginx; # 运行用户
worker_processes 1; # nginx进程数,一般设置与cpu核数一样
error_log /var/log/nginx/error.log warn; # 错误日志存放
pid /var/run/nginx.pid; # nginx服务启动时候pid存放
# 配置影响Nginx服务器与用户的网络连接
events {
# use epoll; nginx轮询方式 select、poll、kqueue、epoll、/dev/poll、eventport、推荐不配置让nginx自动选择
worker_connections 1024; # 子进程能够处理的最大并发连接数
accept_mutex on; # 负载均衡互斥锁 默认off关闭
}
# 配置代理.缓存.日志定义等绝大多数功能和第三方模块的配置
http {
# 日志
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; # 减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 65; # 保持连接的时间,也叫超时时间,单位秒
types_hash_max_size 2048;
include /etc/nginx/mime.types; # 文件扩展名与类型映射表
default_type application/octet-stream; # 默认文件类型
# include /etc/nginx/conf.d/*.conf; # 加载子配置项
server {
listen 80;
# server_name www.snowl.socpe snowl.socpe; 域名匹配
server_name localhost;
error_page 500 502 503 504 /50x.html;
error_page 400 404 error.html;
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm; # 默认首页文件
allow 183.129.134.242; # allow 允许访问的ip地址 可以all
# deny 进制访问的ip地址 可以all
}
# rabbitmq控制界面
location /rabbitmq/ {
proxy_pass http://localhost:15672/;
}
# nacos控制界面
location /nacos/ {
proxy_pass http://localhost:8848;
}
}
}
路径匹配原则 location
localtion
=
精确匹配~
区分大小写~*
不区分大小写^~
匹配到停止搜索
~ \.(jpeg|jpg|png|svg)$
以…结尾,区分大小写
^~ /api/
以api为前缀请求,匹配到就停止搜索
= /match_all/
精确匹配,等号也可以忽略
location /bbs/{
proxy_pass http://127.0.0.1:8080;
}
- 用户请求:
/bbs/api/test
- 到达nginx:
/bbs/api/test
- 到达上游应用服务器:
/bbs/api/test
location /bbs/{
proxy_pass http://127.0.0.1:8080/;
}
- 用户请求:
/bbs/api/test
- 到达nginx:
/bbs/api/test
- 到达上游应用服务器:
/api/test
反向代理
轮训策略
server {
listen 80;
server_name snowl.space;
location / {
proxy_pass http://polling_strategy
}
}
upstream polling_strategy {
server snowl.space:8080; # 应用服务器1
server snowl.space:8081; # 应用服务器2
}
权重策略
upstream polling_strategy {
server snowl.space:8080 [option]; # 应用服务器1
server snowl.space:8081 [option]; # 应用服务器2
}
- weight=number # 权重值
- max_conns=number # 上游服务器最大的并发连接数
- fail_timeout=time # 服务器不可用的判定时间
- max_fails=number # 服务器不可用的检查次数
- backup # 备份服务器,仅当其他服务器都不可用时才会启用
- down # 标记服务器长期不可用,离线维护
hash算法
hash $request_uri
表示使用reqeust_uri
变量作为hash
的key
值,只要访问的uri保持不变,就会一直分发给同一个服务器
upstream polling_strategy {
hash $request_uri;
server snowl.space:8080; # 应用服务器1
server snowl.space:8081; # 应用服务器2
}
ip_hash策略
ip_hash
表示使用ip地址变量作为hash
的key
值,只要访问的ip不变,就会一直分发给同一个服务器。
upstream polling_strategy {
ip_hash;
server snowl.space:8080; # 应用服务器1
server snowl.space:8081; # 应用服务器2
}