大纲
- 介绍nginx
- 下载使用
- nginx功能(核心)
1. 正向代理,反向代理区别
nginx 介绍
- 服务器
* http
* 反向代理 服务器
* IMAP/POP3/SMTP 邮件服务器
* 负载均衡服务器 - 优点
* 高性能
* 轻量级
* 内存少,并发能力强 - 其他
* Nginx是Apache服务器不错的替代品
* 中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
下载使用
-
下载
- 官网:
* stable 稳定的 tags
* mainline 主线 trunk
* beta alph 测试版本 branches
- 官网:
-
使用
* 解压(不要中文)
* No mapping for the Unicode character exists in the target multi-byte code page
* 文件夹
* conf
* nginx.conf 核心配置文件
* html 界面
* logs 日志
* nginx.exe 执行文件
* 开启 双击
* master
* worker
* 关闭
* 任务管理器 -
nginx.conf
* http
server 服务器节点(可以多个)
listen 监听端口
server_name 主机名
location
root
index
* 另外
* 端口 主机名 ip 区分
* another virtual host using mix of IP-, name-, and port-based configuration
nginx功能
- 静态服务器
- 反向代理服务器
- 负载均衡服务器
静态服务器
server {
listen 80;
server_name localhost;
location / {
root C:\Users\Administrator\Desktop\soft\img;
index index.html index.htm;
}
反向代理
1. 正向代理
* 客户端 主动权
* ip 端口 在 客户端
GFW : great fire Wall of China
[外链图片转存失败(img-iIFonewW-1564544285785)(./002_proxy.png)]
2. 反向代理
* 主动权在 服务器端
[外链图片转存失败(img-iMhIqp20-1564544285795)(./003_proxy.png)]
[外链图片转存失败(img-PPtQE2qH-1564544285802)(./004_proxy.png)]
3. nginx反向代理
* 配置
location / {
root C:\Users\Administrator\Desktop\soft\img;
index index.html index.htm;
proxy_pass http://localhost:8080;
}
发展史(代理多台服务器)
-
通过 端口区分
-
域名 ip 区分
* 域名 ip关系
* 域名好记,但是最终访问电脑的时候是ip
* 域转ip过程
* hosts 本机 C:\Windows\System32\drivers\etc
* dns 域名服务器
* 14.215.177.38
* 一个域名对应一个ip
* 一个ip对应多个域名
* 域名
* 购买:万网
* 注册域名卖钱
* 域名区分
* wanwang.aliyun.com
* 顶级域名
* com / org / cn /top
* 一级
* aliyun.com
* 二级
* wanwang.aliyun.com
* …
* 区分
* 第一台
* www.myjd.com/XXX/X.txt
* 第二台
* www.mytmall.com/XXX/X.txtserver { listen 80; server_name www.myjd.com; location / { root C:\Users\Administrator\Desktop\soft\img; index index.html index.htm; proxy_pass http://localhost:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.mytmall.com; location / { root C:\Users\Administrator\Desktop\soft\img; index index.html index.htm; proxy_pass http://localhost:8081; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
负载均衡
[外链图片转存失败(img-4mRD3ONm-1564544285804)(./cluster.png)]
1. 问题
* 超市收钱,如果只有一个收银口,大力比较大
* 一台tomcat,最大并发访问量500
* 一台nginx,最大并发访问量5w,优化一下10w
2. 概念
* 负载均衡,英文名称为Load Balance
* 种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性
* 原理就是数据流量分摊到多个服务器上执行,减轻每台服务器的压力,多台服务器共同完成工作任务,从而提高了数据的吞吐量
3. 搭建多态服务器一起工作(集群)
* 代理多台
upstream xxx{
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
listen 80;
server_name localhost;
location / {
root C:\Users\Administrator\Desktop\soft\img;
index index.html index.htm;
proxy_pass http://xxx;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
* 权值越大,访问几率越大
upstream xxx{
server 127.0.0.1:8080 weight=3;
server 127.0.0.1:8081 weight=1;
}
[外链图片转存失败(img-4pYWvdtV-1564544285805)(./005_balance.png)]
session共享
-
问题
* 用户在tomcat1登入后,seesion中存了用户信息
* 假设用户刷新,nginx分配给了tomcat2
* session丢失,被拦,有需要登入 -
演示
* 两次登入
* jsp页面中获取sessionId
* 如果两次 sessionId不同,换了服务器->必须重新登入 -
方案
session共享
-
问题
* 用户在tomcat1登入后,seesion中存了用户信息
* 假设用户刷新,nginx分配给了tomcat2
* session丢失,被拦,有需要登入 -
演示
* 两次登入
* jsp页面中获取sessionId
* 如果两次 sessionId不同,换了服务器->必须重新登入 -
方案