介绍
本文来说一下OpenResty配置反向代理和负载均衡,其实也就是对nginx.conf的相关配置。我们来看下
配置
我们来看一个配置的例子: nginx.conf
#user nobody;
worker_processes 2;
# 优化配置Nginx worker进程最大打开文件数
worker_rlimit_nofile 65535;
events {
# 单个进程允许的客户端最大连接数
worker_connections 65535;
# epoll模式 Epoll模式将提高并发连接到100K级别
#用于linux2.6以上内核,可以大大提高nginx的性能,mac用不了
# use epoll;
}
http {
# #文件扩展名与文件类型映射表
include /usr/local/etc/openresty/mime.types;
#默认文件类型,默认为text/plain
default_type application/octet-stream;
#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块
sendfile on;
#连接超时时间,默认为75s,可以在http,server,location块
keepalive_timeout 65;
#是否开启gzip压缩
gzip on;
#压缩在内存中缓冲几块,每块多大
gzip_buffers 32 4K;
#[1-9] 推荐6 压缩级别,级别越高,压的越小,越浪费CPU计算资源
gzip_comp_level 6;
#配置禁用gzip条件,支持正则(此处表示ie6及以下不启用gzip,ie 低版本不支持)
gzip_disable MSIE [1-6].;
#开始压缩的长度,太小也没必要压缩
gzip_min_length 200;
#开始压缩的http协议版本
gzip_http_version 1.0;
#对哪些类型文件进行压缩
gzip_types text/plain application/xml;
# 是否传输gzip压缩标志
gzip_vary on;
#缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户
client_max_body_size 50m;
#
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
##nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 5s;
#连接成功后,后端服务器响应时间(代理接收超时)
#nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒
proxy_read_timeout 5s;
#nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒
proxy_send_timeout 5s;
#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 64k;
#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_buffers 4 32k;
#高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 64k;
#设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘
proxy_temp_file_write_size 64k;
#不允许代理端主动关闭连接
proxy_ignore_client_abort on;
#Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本
proxy_http_version 1.0 ;
#支持客户端的请求方法。post/get;
#proxy_method get;
#如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off
proxy_intercept_errors on;
#存放http报文头的哈希表容量上限,默认为512个字符
proxy_headers_hash_max_size 1024;
#nginx服务器申请存放http报文头的哈希表容量大小。默认为64个字符。
proxy_headers_hash_bucket_size 128;
#反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
proxy_next_upstream timeout;
#默认为on,如果我们在错误日志中发现“SSL3_GET_FINSHED:digest check failed”的情况时,可以将该指令设置为off
#proxy_ssl_session_reuse on;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include proxy.conf;
}
复制代码
上面引用了proxy.conf配置,配置了相关server,我们看下配置: proxy.conf
upstream payproxy {
server 192.168.1.16:8080;
server 192.168.1.18:8080;
#backup为热备,当其它服务都出现故障的时候使用
server 192.168.1.19:8080 backup;
}
server {
listen 80;
# 线上设置域名如pay.wk.com,当前本地测试
server_name pay.wk.com;
access_log logs/access.log;
error_log logs/error.log;
location /pay {
# 重定向url关闭
proxy_redirect off;
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
#请求转向payproxy的服务器列表
proxy_pass http://payproxy;
}
}
upstream adminproxy {
### 请求服务器的策略
# 1.服务器列表默认轮询权重都为1(依次轮训).9091->9092->9091->9092...
# 2.可以通过weight设置权重(加权轮询),如果设置,请求顺序则为9091->9092->9092->9091...
# 3.也可以设置ip_hash,会让相同的服务端请求相同的服务器
# ip_hash
### max_fails和fail_timeout设置的含义
# max_fails: 允许请求失败次数,默认为1.当超过最大次数时会返回proxy_next_upstream 模块定义的错误
# ail_timeout:max_fails失败次数以后,暂停server的时间,默认为10s
server 192.168.1.19:9091 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.20:9092 weight=2 max_fails=2 fail_timeout=2;
#backup为热备,当其它服务都出现故障的时候使用
#server 192.168.1.21:9093 backup;
# 表示当前server暂时不参与负载均衡
#server 192.168.1.22:9094 down;
}
server {
listen 80;
# 线上设置域名如admin.wk.com,当前本地测试
server_name 127.0.0.1;
access_log logs/access.log;
error_log logs/error.log;
location /admin {
# 重定向url关闭
proxy_redirect off;
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
#请求转向payproxy的服务器列表
proxy_pass http://adminproxy;
}
}
复制代码
上面我们对每个配置都进行了解释,如果对一些配置还是不要理解,可以自行查询下。
另外我们来说一下proxy.conf中upstream配置server列表的max_fails和fail_timeout再进行说明下。nginx基于连接测试,后端请求异常不可用,在单位周期fail_timeout内达到了max_fails失败次数的话,那么该server会被暂时标记为不可用。等待下一个fail_timeout单位周期,进入下个周期请求成功则按照之前轮询策略进行执行。如果还是请求失败则再等待下个周期再次重试。以此类推。
总结
简单梳理了一下反向代理和负载均衡的相关配置,上面配置也已上传到github