nginx,gateway配置
一 反向代理:
监听192.168.10.129:9001 如果url中有 edu就把请求转发给http://ip1:port1,如果url中有vod就把请求转发给http://ip2:port2
nginx配置:
server{
listen 9001;
server_name 192.168.10.129;
location ~ /edu/ {
proxy_pass http://ip1:port1
}
location ~ /vod/ {
proxy_pass http://ip2:port2
}
}
}
二 先反向代理,再对代理路径进行负载均衡: 192.168.10.11:80请求进来,负载均衡到 ip:port1和ip2:port2两个节点
server{
listen 80;
server_name 192.168.10.11;
location / {
proxy_pass http://myserver;
root html;
index index.html index.htm;
}
}
http{
upstream myserver{ //upstream是配置负载均衡模块的地方,myserver是模块名,不加weight就是默认轮询方式负载均衡,加了weight后面的值越大负载的概率越大
server ip1:port1 weight=10;
server ip2:port2 weight=1;
}
三 动静分离配置: 浏览器输入 192.168.10.129:9001/image/01.jpg 会转发到 /date/image/01.jpg
server{
listen 9001;
server_name 192.168.10.129;
location /www/ {
root /date/;
index index.html index.htm;
}
location /image/ {
root /date/;
autoindex on; //表示列出当前文件夹下的目录
}
}
}
四 location中的url匹配规则:
4.1-location中root和alias的区别
如果是root,会把请求uri中的ip/域名+port替换成root指定的目录
如果是alias,会把请求uri中的ip/域名+port+匹配到的路径替换为alias指定的目录
以请求http://example.com/foo/bar/hello.html 为例
location /foo{
root /home/hyt
}
匹配到/foo后,url的ip/域名+port替换成root指定的目录,这里port为默认值80,就是将example.com替换成
/home/hyt,所以实际访问地址为 /home/hyt/foo/bar/hello.html
location /foo/bar{ root /home/hyt }
匹配到/foo/bar后,将/home/bar替换请求uri中的ip/域名+port,所以实际访问地址 /home/hyt/foo/bar/hello.html
location /foo{ alias /home/hyt/ }
匹配到/foo, /home/hyt替换example.com/foo,所以实际访问地址为/home/hyt/bar/hello.html
location /foo/bar{ alias /home/hyt/ }
实际访问地址为 /home/hyt/hello.html
更详细使用案例:https://www.jb51.net/server/305770q8w.htm
nginx负载均衡和高可用集群使用情况:
例如前端保证高可用部署两台服务器,后端也部署两台服务器,数据库也是两个,前端两台服务器安装nginx,并用keepalived组成高可用集群,对外提供一个虚拟ip,域名对应这个虚拟ip,页面根据域名解析ip地址,虚拟ip的请求会分给组成集群的服务器,前端根据url调后端接口,需要在前端nginx配置负载均衡把请求分摊给两个后端服务器,后端接口如果需要调用minio集群,就在两个后端服务器上部署nginx配置minio的负载均衡,比如java配置文件里minio的ip地址配置为http://localhost,那么后端nginx的server就监听localhost的80端口,负载到minio集群(后端java服务和minio服务不在同一个服务器上)数据库Mysql本身是主备集群保证高可用
nginx主从配置,一台主一台从,组成高可用: 主服务器是ip1,从服务器是ip2
1 在两个服务器分别下载keepalived软件: yum -y install keepalived,下载完成后会有/etc/keepalived/keepalived.conf配置文件
2 两台服务器分别修改keepalive.conf这个配置文件:
主要的配置: virtual_ipaddress: 集群对外的虚拟ip地址,vrrp_script chk_http_port:检测集群里的服务器是否宕机的脚本文件
这样就搭建好了主从模式,请求虚拟ip,会先转发到主nginx,如果发现主nginx宕机,会转发到从nginx.
gateway配置:
spring:
cloud:
gateway:
routes:
- id: auth #中央授权服 ,符合predicates断言条件的请求将会交给uri中的服务处理,lb是负载均衡,后面是微服务名
uri: lb://yy-auth
predicates:
- Path=/auth/**
- id: user #APP端用户服务
uri: lb://yy-user
predicates:
- Path=/user/**
- id: admin #管理后台服务
uri: lb://yy-admin
predicates:
- Path=/admin/**
- id: test #测试服
uri: lb://yy-test
predicates:
- Path=/test/**
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理