美文网首页
网站架构技术笔记

网站架构技术笔记

作者: oo尐爺籽 | 来源:发表于2019-03-29 22:41 被阅读0次

proxy_cache:使用内存给/SSD级代理缓存内容

proxy_cache_lock:使用lock机制,将多个回源合并为一个,以减少回源量,并设置相应的lock超时时间.

shared_dict: nginx+lua 进行cache最大的好处就是白月田reload缓存不会丢失

对于穿透到后端应用的流量可以考虑使用Nginx的limit模块处理.

对于恶意IP可以使用nginx deny 进行屏蔽

Nginx upstream配置

upstream backend{

    server 192.168.61.1:9080 weight=1;

    server 192.168.61.1:9090 weight=2;

}

Ip地址和端口:配置上游戏服务务器的ip地址和端口.

权重:weight用来配置桶重,默认都是1,权重越高分配给这台服务器的请求就越多

proxy_pass来处理用户请求

location /{

    proxy_pass http:// backend;

}

负载均衡算法

round-robin:轮询,默认

ip_hash:根据客户ip进行负载均衡,相同的ip将负载到一个upstream

upstream backend{

    ip_hash;

    server 192.168.61.1:9080 weight=1;

    server 192.168.61.1:9090 weight=2;

}

Hash key [consistent]:对某一个key进行哈希或者使用一致性哈希算法进行负载.使用hash算法存在的问题是,当添加删除一台服务器时,将导致辞很多key被重新新负载(后端可能出现问题),一至性哈希,只有少数key将重新负载

哈希算法:根限请求uir负载

upstream backend{

    hash $uri;

    server 192.168.61.1:9080 weight=1;

    server 192.168.61.1:9090 weight=2;

}

一致性哈希:consisten_key动态指定

upstream backend{

    Hash $consisten_key consistent;

    server 192.168.61.1:9080 weight=1;

    server 192.168.61.1:9090 weight=2;

}

Nginx 商业版 least_time 基于最小平均响应时进行负载

失败重新

配置upstream server 和  proxy_pass

upstream backend{

    server 192.168.61.1:9080 max_fails=2 fail_timeout=10s weight=1;

     server 192.168.61.1:9090 max_fails=2 fail_timeout=10s weight=1;

}

当fail_timeout 时间内失败了 max_fails 次请求,则认为该上游戏服务器不可用/不存活,然后将摘掉该上游戏服务器,fail_timeout时间后会再次将该服务器加入到存活上游戏服务器列表进行重方式

Location /test{

    proxy_connect_time 5s;

    proxy_read_time 5s;    

    proxy_send_time 5s;

   proxy_next_upstream error timeout;

   proxy_next_upstream_timeout 10s;

    proxy_next_upstream_tries 2;

proxy_passhttp://backend;

    add_header upstream_addr $upstream_addr;

}

健康检查

Nginx 默认是惰性策略,商业版提供health_check 主动检查.

可以集成nginx_upstream_check_module 支持TCP HTTP心跳检查

TCP 心跳检查

upstream backend{

    server 192.168.61.1:9080 weight=1;

    server 192.168.61.1:9090 weight=2;

    check interval=3000    rise=1    fall=3    timeout=2000    type=tcp;

}

Interval:检测间隔时间 3秒

Fall:检测失败多少次后标识不存活

Rise:检测成功多少次后标识存活,可以处理请求

Timeout:检测请求超时时间配置

HTTP 心跳检查

upstream backend{

    server 192.168.61.1:9080 weight=1;

    server 192.168.61.1:9090 weight=2;

    check interval=3000    rise=1    fall=3    timeout=2000    type=http;

    check_http_send    “HEAD    /status    HTTP/1.0\r\n\r\n”;

    check_http_expect_alive http_2xx http_3xx;

}

check_http_send:检查时发的HTTP请求内容.

check_http_expect_alive:返回匹配的响应状态码时,则认为服务器存活

检查间隔时间不能太短,否则造成上游戏服务器挂掉

其他配置

域名上游戏务服器

upstream backend{

    server c0.3.cn;

    server c1.3.cn;

}

nginx社区版 nginx解析配置文件的阶段将域名解析成ip地址并记录到upstream上,当这两个域名对应的ip地址发生变化时,该upstream不会更新.

Nginx商业版才支持动态更新.

不过 proxy_passhttp://c0.3.cn是支持动态域名解析

备份上游戏服务器

upstream backend{

    server 192.168.61.1:9080 weight=1;

    server 192.168.61.1:9090 weight=2 backup;

}

9090为备份服务器,当所有主服务器不存活时,请求会转发给备服务器.

不可用上游戏服务器

upstream backend{

    server 192.168.61.1:9080 weight=1;

    server 192.168.61.1:9090 weight=2 down;

}

9090配置为永久不可用

长连接

keepalive配置长边接数量

upstream backend{

    server 192.168.61.1:9080 weight=1;

    server 192.168.61.1:9090 weight=2 backup;

    keepalive 100;

}

这个指令配置每个worker进行程与上游服务器可缓存的空闲边接最大数,当超出时,最近最少使用的边接将被关闭.keepalive指令不限制Worker进行与上游戏服务器的总边接

建立长边接

location /{

    #支持keep-alive

    proxy_http_version 1.1;

    proxy_set_header    Connection    “”;

    proxy_pass    http://backend;

}

如果是http/1.0 需要配置发送 “Connection:Keep-Alive” 请求头.

上游戏服务器不要忘记开启长边接支持

建议只对小报文开启长连接

HTTP反向代理示例

反向代理除了实现负载之外,不提供如缓顾来减少上游戏服务器的压力

全局配置

proxy_buffering    on;

proxy_buffer_size    4k;

proxy_buffers    512    4k;

proxy_busy_buffers_size    64;

proxy_temp_file_write_size    256k;

proxy_cache_lock    on;

proxy_cache_lock_timeout    200ms;

proxy_temp_path    /tmpfs/proxy_temp;

proxy_cache_path    /tmpfs/proxy_cache    levels=1:2    keys_zone=cache:512m    inactive=5m    max_size=8g;

proxy_connect_timeout    3s;

proxy_read_timeout    5s;

proxy_send_timeout    5s;

开启proxy buffer 缓存内容放到tmpfs(内存文件系统)提长性能,设置超时时间

location 配置

location    ~^/backend/(.*)${

    #设置一致性哈希希载key

    set_by_lua_file $consistent_key “backend.lua”;

    #失败重试配置

    proxy_next_upstream    error    timeout    http_500    http_502    http_504;

    proxy_next_upstream_timeout    2s;

    proxy_next_upstream_tries    2;

    #请求上游戏服务器使用GET (不管请求是什么方法)

    proxy_method GET;

    #不给上游戏服务器传递请求体

    proxy_pass_request_body    off;

    #不给上游戏服务器传递请求头

    proxy_pass_request_headers    off;

    #设置上游戏服各器的哪些响应头不发送给客户端

    proxy_hide_header Vary;

    #给上游戏服务器传递Referer Cookie Host(按需传递)

    proxy_set_header    Referer    $http_referer;

    proxy_set_header    Cookie    $http_cookie;

    proxy_set_header    Host    web.c.3.local;

   proxy_passhttp://backend/$1$is_args$args;

}

开启proxy_pass_request_body proxy_pass_request_headers 禁止向上游戏服务器传递请求头和内容体,使得上游戏不受请求头攻击,也不需要解析;使用proxy_set_header按需传递即可

开启gzip支持

gzip    on;

gzip_min_length    1k;

gzip_buffers    16    16k;

gzip_http_version    1.0;

gzip_proxied    any;

gzip_comp_level    2;

gzip_types    text/plain application/x-javascript    text/css    application/xml;

gzip_vary    on;

对于内容型响应建议开启gzip压缩 gzip_comp_level压缩级别根据实际压测来决定

HTTP动态负载均衡

每次upstream列表有变更,需要服务器进行修改,upstream服务上线无法自动注册到nginx upstream列表,因此我们需一种服务注册将upsteam动态注册到nginx上,实现upstream服务自动发现.

Consul是一款开源的分布式服务注册与发现系统,通过HTTP API可以使得服务注册,发现实现起来非常简单,

服务注册:服务实现者可以通过HTTP API或DNS方式,将服务注册到Consul

服务发现:服务消费者可以通过HTTP API或DNS方式,从Consul获取服务的IP和PORT

故障检测:支持如TCP HTTP等方式的健康检查,有故障时自动摘除

K/V存储:使用K/V存储实现动态配置,其使用HTTP长轮询实现变更触发和配置更改

多数据中心:支持多数据中心,可以按照数据中心注册和发现服务

Raft算法:Consul使用Raft算法实现集群数据一致性.

Nginx四层负载均衡

Nginx1.9.0支持四层负载均衡

静态负载均衡

ngx_stream_cor_module 默认没有启用 安装nginx时 添加—with-stream 

./configure —prefix=/usr/servers/    —with-stream

stream指令

stream{

    upstream mysql_backend{

    …...

    }

    server{

    …...

    }

}

upstream配置

upstream mysql_backend{

    server 192.168.61.1:3306 max_fails=2 fail_timeout=10s weight=1;

    server 192.168.61.2:3306 max_fails=2 fail_timeout=10s weight=1;

    least_conn;

}

server配置

server{

    listen 3308;

    proxy_next_upstream on;

    proxy_next_upstream_timeout    0;

    proxy_next_upstream_tries    0;

    #超时配置

    proxy_connect_timeout    1s;

    proxy_timeout    1m;

    #限速配置 每少字节数 默认为0 不限速

    proxy_upload_rate   0;

    proxy_download_rate    0;

    #上游戏服务器

    proxy_pass    mysql_backend;

}

listen指令指定临界听的端品,默认TCP UDP配置 "listen3308    udp;"

相关文章

网友评论

      本文标题:网站架构技术笔记

      本文链接:https://www.haomeiwen.com/subject/fbldbqtx.html