nginx與Elasticsearch結合使用

cmb2 9年前發布 | 29K 次閱讀 ElasticSearch 搜索引擎

Elasticsearch是一種先進的,高性能的,可擴展的開源搜索引擎,提供全文搜索和實時分析的結構化和非結構化的數據。

它的特定是可以通過HTTP使用 RESTful API,很容易的融入現有的web架構。因此在高并發的情況下,采用nginx反向代理負載均衡到多臺Elasticsearch 服務器上。

架構圖:

nginx與Elasticsearch結合使用

這種架構的優點是:

  1. 記錄每個API請求的日志
  2. 支持大量的客戶端連接,不管有沒有啟用keepalives,比長連接(使用keepalives)到elasticsearch服務器小的多
  3. 負載均衡的請求Elasticsearch服務器
  4. 緩存數據,減少同一內容再次請求Elasticsearch服務器。NGINX Plus 還提供HTTP API 對緩存數據的清除接口
  5. 提供主動健康檢測(僅nginx plus),不斷檢測后端Elasticsearch服務器是否正常,并主動的進行切換。
  6. 報告豐富的監控指標(僅nginx plus),提供監控和管理。
  7. 支持通過HTTP API動態的配置上游服務器組(僅nginx plus),可以從上游服務器組中添加和刪除,上線或下線,改變權重。
  8. </ol>

    nginx與Elasticsearch結合使用

    下表比較Elasticsearch 、Elasticsearch + nginx F/OSS、Elasticsearch + NGINX Plus 的功能:

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr>

    </tr> </tbody> </table>

    當使用NGINX Plus高可用性主動或被動配置負載均衡的Elasticsearch 服務器集群時,Elasticsearch 客戶端通過nginx plus請求,而不是直接連接到Elasticsearch服務器,可以根據實際情況任意的擴展Elasticsearch服務器不用更新客戶端。

    部署NGINX Plus + Elasticsearch

    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; } }</pre></div> </div> </div>

    負載均衡、對有效的請求緩存10分鐘、主動的健康監測、狀態收集。

    Elasticsearch是一個強大而靈活的搜索引擎,與nginx完美構建成一個可擴展高性能高可用性架構。

    更多關于安全性的問題可以參見:

    Securing Elasticsearch with Nginx
    Playing HTTP Tricks with Nginx

    本文地址: http://www.ttlsa.com/nginx/nginx-elasticsearch/

     本文由用戶 cmb2 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
     轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
     本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!

    Elasticsearch client nginx F/OSS NGINX Plus
    Horizontal scalability Y Y Y
    Keepalive optimization
    Y Y
    Centralized HTTP access logs
    Y Y
    Queuing and concurrency control

    Y
    Response caching
    Y Y
    Failover of failed connections Y Y Y
    Active monitoring of Elasticsearch nodes Y (some clients)
    Y
    Advanced load balancing methods
    Y Y
    Weighted load balancing
    Y Y
    Dynamic reconfiguration

    Y
    Status monitoring

    Y
    General-purpose HTTP load balancing
    Y Y
sesese色