负载均衡Nginx | 青训营笔记

101 阅读2分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 11 天

Nginx

一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。

特点:占用内存少,并发能力强

作用:反向代理

正向代理

代理客户端如VPN

image-20230212213050247.png

反向代理

放在服务器上的

image-20230212213305706.png

负载均衡

轮询(每台服务器不论性能请求一次)

加权轮询(如消息队列的公平分发:能者多劳)

iphash:对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户端ip的请求分发给同一台服务器处理,可以解决session不共享的问题

【补充知识点】

  1. 什么是session?

http是无状态协议,服务器需要记录客户状态就需要用到一种机制。不同于cookie存放在客户端,session是存放在服务器上

  1. 用外置的redis来存储session数据解决session共享问题

实现:

  1. 引入spring-session-data-redis依赖
  2. 添加配置文件
  3. session配置,添加注解EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)

Nginx安装

windows安装

  1. 下载Nginx并安装到对应文件夹

网址:nginx: download

  1. 打开ngnix路径的cmd
  2. 输入命令
nginx.exe #最好用start nginx

报错:

nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: The system cannot find the path specified)
​
nginx: [emerg] CreateDirectory() "D:\Environment\nginx-1.18.0/temp/client_body_temp" failed (3: The system cannot find the path specified)

解决:在文件目录下新建logs文件夹和temp文件夹

image-20230212234711548.png

  1. 访问网址
http://127.0.0.1/

出现下图成功!

image-20230212234834013.png

  1. 退出
nginx.exe -s stop

常用命令

start nginx #启动
nginx.exe -s stop #停止
nginx.exe -s quit #安全退出
nginx -s reload #重新加载配置文件
ps aux|grep nginx #查看nginx进程
nginx -c conf\my.conf #指定自己配置的文件