首页 > 系统相关 >Keepalived 提高吞吐量、负载均衡 ip_hash、负载均衡 url_hash 与 least_conn、Nginx的缓存

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

时间:2023-11-09 14:33:47浏览次数:28  
标签:负载 缓存 hash 192.168 server proxy 均衡 8080

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

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

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; 
}

标签:负载,缓存,hash,192.168,server,proxy,均衡,8080
From: https://blog.51cto.com/u_13771490/8278789

相关文章

  • 开发时推荐使用Map map = new HashMap()
    Mapmap=newHashMap();Map是一个接口,HashMap是具体的实现类。由于接口就是多个类的共有规范(里面的抽象方法),是一种引用数据类型,一个抽象的概念,不能被实例化,因此接口需要由具体的类来实现。这条代码指明:由HashMap类来实现接口Map中描述的方法。HashMapmap=newHashMap(......
  • Load Test Statistics 负载测试统计(负载测试计数器)
    LoadTestStatisticsLoadTestercollectsanextensivemeasurementsduringaloadtest.Thisdataiscollectedinsamples.Eachsamplehasmultiplemeasurementsassociatedwithit.Dependingonthemeasurementtype,somemeasurementsmaynotbaapplicable......
  • redis 类型Hash 中value字符串存储空间大小
    在Redis中,Hash数据类型中的value是字符串,存储空间大小取决于存储在Hash中的每个value字符串的长度。Redis内部并不会额外存储每个value的元信息,因此存储空间大小主要由存储的字符串长度决定。每个字符串值的存储空间大小取决于以下因素:字符串长度:字符串的长度是主要的决定因素。较......
  • Set---HashSet-LinkedHashSet
    概述Hashtableandlinkedlistimplementationofthe<tt>Set</tt>interface,withpredictableiterationorder.Thisimplementationdiffersfrom<tt>HashSet</tt>inthatitmaintainsadoubly-linkedlistrunningthroughallofitsen......
  • 2023-11-08:用go语言,字符串哈希原理和实现 比如p = 233, 也就是课上说的选择的质数进制
    2023-11-08:用go语言,字符串哈希原理和实现比如p=233,也就是课上说的选择的质数进制"31256..."01234hash[0]=3*p的0次方hash[1]=3*p的1次方+1*p的0次方hash[2]=3*p的2次方+1*p的1次方+2*p的0次方hash[3]=3*p的3次方+1*p的2次方+2*p......
  • HashMap、TreeMap、Hashtable、HashSet和ConcurrentHashMap区别
    一、HashMap和TreeMap区别1、HashMap是基于散列表实现的,时间复杂度平均能达到O(1)。   TreeMap基于红黑树(一种自平衡二叉查找树)实现的,时间复杂度平均能达到O(logn)。2、HashMap、TreeMap都继承AbstractMap抽象类;TreeMap实现SortedMap接口,所以TreeMap是有序的!HashMap是无序的......
  • HashMap---jdk8
    概述HashtablebasedimplementationoftheMapinterface.Thisimplementationprovidesalloftheoptionalmapoperations,andpermits<tt>null</tt>valuesandthe<tt>null</tt>key.(The<tt>HashMap</tt>classis......
  • pb_ds库中hash_table的使用方法
    头文件:#include<ext/pb_ds/hash_policy.hpp>命名空间:usingnamespace__gnu_pbds;两种hash_table:gp_hash_table:探测法,理论可以卡到O(size),但很难,推荐使用。cc_hash_table:拉链法,复杂度可以保持O(1),但常数较大。使用方法类似unordered_map参考博客:浅谈pb_ds库及其在OI......
  • hashmap的小应用---投票去旅游
    在学习了map之后,使用简单的hashmap进行简单的全班同学投票旅游地点packagecom.itheima.myMap;importjava.util.*;importjava.util.function.BiConsumer;publicclassText2{publicstaticvoidmain(String[]args){//模拟投票Randomra=newRandom......
  • LinkedHashMap
    概述Hashtableandlinkedlistimplementationofthe<tt>Map</tt>interface,withpredictableiterationorder.Thisimplementationdiffersfrom<tt>HashMap</tt>inthatitmaintainsadoubly-linkedlistrunningthroughallofitsen......