1、正向代理与反向代理的区别:
正向代理:
隐藏用户的行为;比如聚合视频会员,vpn翻墙;
反向代理:
隐藏真实应用服务器ip地址,这样就防止直接攻击我们的部署我们应用服务的服务器;
2、配置文件详解
server {# 使用nginx创建了监听端口号码为80listen 80;//访问域名server_name localhost;#location的配置是根据url规则进行匹配 /代表拦截所有location / {#访问我们的根目录的htmlroot html;#默认查找indexindex index.html index.htm;}}
如果是自定义则将root替换为alias
配置反向代理:
server {# 使用nginx创建了监听端口号码为80listen 80;//访问域名server_name localhost;#location的配置是根据url规则进行匹配 /代表拦截所有location / {#配置反向代理proxy_pass http://127.0.0.1.8080;#默认查找indexindex index.html index.htm;}}
负载均衡:
专业术语:“上游服务器”其实就是真实服务器;
#定义上游服务器(需要被nginx真实代理访问的服务器) 默认是轮训机制upstream backServer{server 127.0.0.1:8080;server 127.0.0.1:8081;}server {# 使用nginx创建了监听端口号码为80listen 80;//访问域名server_name www.mayikt.com;#location的配置是根据url规则进行匹配 /代表拦截所有location / {### 指定上游服务器负载均衡服务器proxy_pass http://backServer;#默认查找indexindex index.html index.htm;}}
指定权重
注意:负载均衡效果谷歌浏览器需要直接从浏览器中访问才可以生效,
火狐浏览器没有问题;
upstream backServer{server 127.0.0.1:8080 weight=1;server 127.0.0.1:8081 weight=2;}server {# 使用nginx创建了监听端口号码为80listen 80;//访问域名server_name www.mayikt.com;#location的配置是根据url规则进行匹配 /代表拦截所有location / {### 指定上游服务器负载均衡服务器proxy_pass http://backServer;#默认查找indexindex index.html index.htm;}}
3、五种负载均衡策略
轮询,
指定权重:指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况;
ip_hash:根据来访的ip进行hash值取余来指定访问的机器,这样就可以解决集群环境中session不共享的问题;
fair;
url_hash;
最后两种依赖于第三方;
4、服务器集群带来的问题
分布式session一致性问题,可以使用spring-session或者Token解决,其实底层都是借助了redis做分布式缓存;
分布式调度重复执行,分布式任务调度平台 xxl-job;
分布式日志收集问题,可以使用elk_kafka进行日志收集;
5、待收拾:
根据域名转发到不同的服务;
#配置多个serverserver {listen 80;server_name a.mayikt.com;location / {proxy_pass http://192.168.0.101:8080;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 80;server_name b.mayikt.com;location / {proxy_pass http://192.168.0.101:8081;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
根据项目名转发到不同的服务;(记住proxy_pass端口号后的/),可解决跨域
#使用Nginx搭建API网关保持域名和端口一致location /b {proxy_pass http://192.168.18.190:8081/;index index.html index.htm;}location /a {proxy_pass http://192.168.18.190:8080/;index index.html index.htm;}
