Elasticsearch结合Nginx使用

Elasticsearch是一种先进的,高性能的,可扩展的开源搜索引擎,提供全文搜索和实时分析的结构化和非结构化数据。 它的特点是可以使用RESTful API over HTTP,因此很容易融入现代网络架构。    由于NGINX具有出色的性能非常高并发率,再加上负载平衡的HTTP流量功能,这是非常适合用作为您扩展到多个Elasticsearch服务器的反向代理负载均衡。   

部署NGINX加上与Elasticsearch的优势

Elasticsearch有专为可扩展性的一些功能,但卸载请求开源NGINX产品或商业的负载均衡Nginx Plus产品,这更加具有企业级的功能,节省了资源Elasticsearch服务器上。   把NGINX或NGINX Plus加在一个Elasticsearch服务器的前端提供福利,如请求 日志记录,但真正的价值是当你扩展到多个Elasticsearch服务器。 [attach]578[/attach] 这种架构除了记录每个API请求,NGINX加NGINX Plus还有如下优势:
    []支持大量的客户端连接,不管有没有启用keepalives,比长连接(使用keepalives)到elasticsearch服务器小的多[/][]负载均衡的请求Elasticsearch服务器[/][]缓存数据,减少同一内容再次请求Elasticsearch服务器。NGINX Plus 还提供HTTP API 对缓存数据的清除接口[/][]提供主动健康检测(仅nginx plus),不断检测后端Elasticsearch服务器是否正常,并主动的进行切换。[/][]报告丰富的监控指标(仅nginx plus),提供监控和管理。[/][]支持通过HTTP API动态的配置上游服务器组(仅nginx plus),可以从上游服务器组中添加和删除,上线或下线,改变权重。[/]
下表比较Elasticsearch 、Elasticsearch + nginx F/OSS、Elasticsearch + NGINX Plus 的功能: [attach]579[/attach] [attach]580[/attach] 当使用NGINX Plus高可用性主动或被动配置负载均衡的Elasticsearch 服务器集群时,Elasticsearch 客户端通过nginx plus请求,而不是直接连接到Elasticsearch服务器,这意味着,根据需要,而无需更新客户端,你可以任意的扩展Elasticsearch服务器。

部署NGINX Plus + Elasticsearch

部署NGINX Plus与Elasticsearch是非常简单的。在下面的示例NGINX Plus配置的指令定义负载均衡两台Elasticsearch服务器设置:
    []upstream - 负载均衡请求的是两台Elasticsearch服务器,地址分别是192.168.187.132和192.168.187.133的9200端口。[/][]proxy_cache_valid – 有效缓存有效时间10分钟[/][]health_check - 主动的健康监测、状态收集,把他们从负载平衡轮训的时候,把它们放下来,然后再将它们重新加入到轮训中中,再恢复健康。健康检查资料的匹配块,这不仅可以确保服务器返回200状态码,但检查的响应是JSON,它返回字符串预期Elasticsearch格式化,”状态“:200”。[/][]Final server block - 收集统计并监听8080端口的请求为status.html页面显示统计,或状态,它告诉nginx plus返回JSON格式的原始数据。[/]
 
proxy_cache_path /var/cache/nginx/cache keys_zone=elasticsearch:10m inactive=60m;
upstream elasticsearch_servers {
    zone elasticsearch_servers 64K;
    server 192.168.187.132:9200;
    server 192.168.187.133:9200;
}
match statusok {
    status 200;
    header Content-Type ~ "application/json";
    body ~ '"status" : 200';
}
server {
    listen 9200;
    status_zone elasticsearch;
    location / {
        proxy_pass http://elasticsearch_servers;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_cache elasticsearch;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        proxy_connect_timeout 5s;
        proxy_read_timeout 10s;
        health_check interval=5s fails=1 passes=1 uri=/ match=statusok;
    }
    # redirect server error pages to the static page /50x.html
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    access_log logs/es_access.log combined;
}
server {
    listen 8080;
    root /usr/share/nginx/html;
    location / {
        index status.html;
    }
    location =/status {
        status;
    }
}

总结

Elasticsearch是一个强大而灵活的搜索引擎,和NGINX Plus一个企业级的应用交付平台自然契合在一起,作为一个现代化的可扩展的网络架构的组件。 作为软件产品,他们提供的只要你安装这些相同的特性和功能:在裸机服务器,在虚拟机中,在云中,或在容器中。

进一步阅读

Playing HTTP Tricks with Nginx Securing Elasticsearch with Nginx 分享英文原文:https://www.nginx.com/blog/nginx-elasticsearch-better-together/

0 个评论

要回复文章请先登录注册