这是我参与「第五届青训营 」伴学笔记创作活动的第 11 天
Nginx
一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。
特点:占用内存少,并发能力强
作用:反向代理
正向代理
代理客户端如VPN
反向代理
放在服务器上的
负载均衡
轮询(每台服务器不论性能请求一次)
加权轮询(如消息队列的公平分发:能者多劳)
iphash:对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户端ip的请求分发给同一台服务器处理,可以解决session不共享的问题
【补充知识点】
- 什么是session?
http是无状态协议,服务器需要记录客户状态就需要用到一种机制。不同于cookie存放在客户端,session是存放在服务器上
- 用外置的redis来存储session数据解决session共享问题
实现:
- 引入
spring-session-data-redis依赖 - 添加配置文件
- session配置,添加注解
EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)
Nginx安装
windows安装
- 下载Nginx并安装到对应文件夹
- 打开ngnix路径的cmd
- 输入命令
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文件夹
- 访问网址
http://127.0.0.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 #指定自己配置的文件