当前位置: 首页 > 图灵资讯 > 技术篇> Keepalived 提高吞吐量、负载均衡 ip_hash、负载均衡 url_hash 与 least_conn、Nginx的缓存

Keepalived 提高吞吐量、负载均衡 ip_hash、负载均衡 url_hash 与 least_conn、Nginx的缓存

来源:图灵教育
时间:2023-11-09 17:22:39

Keepalived 提高吞吐量

keepalived : 设置长连接处理的数量

proxy_http_version :设置长连接http的版本是1.1

proxy_set_header :清除connection header 信息

upstream tomcats { # server 192.168.1.173:8080 max_fails=2 fail_timeout=1s; server 192.168.1.190:8080; # server 192.168.1.174:8080 weight=1; # server 192.168.1.175:8080 weight=1; keepalive 32; }server {listen 80; server_name www.tomcats.com; location / { proxy_pass http://tomcats; proxy_http_version 1.1; proxy_set_header Connection ""; } }
负载均衡 ip_hash

ip_hash 如果用户ip没有更改,用户访问可以要求上游服务中的固定服务器。

使用ip_hash的注意事项:后台服务器不能直接移除,只能标记 down .

upstream tomcats { ip_hash; server 192.168.1.173:8080; server 192.168.1.174:8080 down; server 192.168.1.175:8080; } 
负载均衡 url_hash 与 least_conn

hash后访问固定的服务器节点,根据每次请求的url地址。

upstream tomcats { # url hash hash $request_uri; # 最少连接数 # least_conn server 192.168.1.173:8080; server 192.168.1.174:8080; server 192.168.1.175:8080; }server { listen 80; server_name www.tomcats.com; location / { proxy_pass http://tomcats; } }
Nginx缓存

1. 浏览器缓存:

  • 加快用户访问,提升单个用户(浏览器访问者)体验,缓存在本地

2. Nginx缓存:

  • 在nginx端缓存,提升所有访问nginx端的用户
  • 提高上游访问量(upstream)服务器的速度
  • 用户访问仍然会产生请求流量
location /files { alias /home/imooc; # expires 10s; # expires @22h30m; # expires -1h; # expires epoch; # expires off; expires max; } 
<html> <body>Hello, Nginx ~ !~  </body> </html>

属性说明:

# proxy_cache_path 设置缓存目录 # keys_zone 设置共享内存,占用空间大小 # max_size 设置缓存大小 # inactive 超过这个时间就被清理干净了 # use_temp_path 临时目录,使用后会影响nginx的性能 proxy_cache_path /usr/local/nginx/upstream_cache keys_zone=mycache:5m max_size=1g inactive=1m use_temp_path= 
location / { proxy_pass http://tomcats; # 启用缓存,与keys_zone一致 proxy_cache mycache; # 200和304状态码缓存时间为8小时 proxy_cache_valid 200 304 8h; }