由于上游服务(不提供公网访问)要处理非常复杂的业务逻辑,而且强调开发效率,所以其性能都比较弱,我们使用nginx作为反向代理以后,可以由一台nginx把请求按照负载均衡算法代理给多台上游服务器工作,这样就可以实现水平扩展,在用户无感知的情况下,我们添加更多的上游服务器来提升服务的处理性能,如果上游服务器常出现问题时,nginx可以自动把请求转发到正常服务器上面。
上游服务器列表
使用负载均衡器转发到真实业务逻辑访问的服务器
upstream service_group{
server 127.0.0.1:8888 max_fails=3 fail_timeout=10s;
server 127.0.0.1:8887 max_fails=3 fail_timeout=10s;
server 127.0.0.1:8886 max_fails=3 fail_timeout=10s;
}
反向代理
location / {
limit_conn addr 25;
limit_req zone=one burst=100 nodelay;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_set_header Host $host; #远端地址信息
proxy_set_header X-Real-IP $remote_addr;#远端地址信息
proxy_intercept_errors on;
proxy_set_header Connection "";
proxy_http_version 1.1;
client_max_body_size 2m;
proxy_pass http://service_group;
}
配置缓存服务器
用于资源信息内容在一段时间不会发生变化,为了减轻上游服务器压力,会让nginx把上游服务器返回的内容缓存一段时间,即使这段内容在这一段时间发生变化,也不做处理,我们只会把缓存中的数据给浏览器。
# 设置缓存文件路径 文件命名方式 共享内容10M 命名为my_cache
proxy_cache_path /tmp/nginx/cache levels=1:2 keys_zone=my_cache:10m;
proxy_cache my_cache;
proxy_cache_key $scheme$proxy_host$request_uri;
proxy_cache_valid 200 304 302 1d;
本文暂时没有评论,来添加一个吧(●'◡'●)