Nginx

keepalive 的相反行為(ElasticSearch 上的 nginx 反向代理)

  • February 19, 2016

如本文所述,我正在為 ElasticSearch(使用 HTTP Basic Auth)設置 nginx 反向代理。

這是我的 nginx 配置文件:

events {
       worker_connections  1024;
}


http {
       upstream elasticsearch {
               server elasticsearch.example.org:9200;
               keepalive 64;
       }

       server {
               listen 8080;

               location / {
                       auth_basic "ElasticSearch";
                       auth_basic_user_file /var/www/.htpasswd;

                       proxy_pass http://elasticsearch.example.org:9200;
                       proxy_http_version 1.1;
                       proxy_set_header Connection "Keep-Alive";
                       proxy_set_header Proxy-Connection "Keep-Alive";
               }
       }
}

代理正確地將埠 8080 轉發到 9200,並且應該保持與 Elasticsearch 的持久連接(keepalive)。

這是訪問 URL http://elasticsearch.example.org:9200/_nodes/stats/http?pretty>或<http://elasticsearch.example.org:8080/_nodes/stats/http?pretty (HTTP身份驗證已經完成)在瀏覽器中:

{
 "cluster_name" : "elasticsearch",
 "nodes" : {
   "rIFmzNwsRvGp8kipbcwajw" : {
     "timestamp" : 1455899085319,
     "name" : "Kid Colt",
     "transport_address" : "elasticsearch.example.org/10.3.3.3:9300",
     "host" : "10.3.3.3",
     "ip" : [ "elasticsearch.example.org/10.3.3.3:9300", "NONE" ],
     "http" : {
       "current_open" : 3,
       "total_opened" : 28
     }
   }
 }
}

在 9200 埠訪問頁面(直接連接 Elasticsearch)並重新載入時,該欄位total_opened應該增加,而在 8080 埠(通過 nginx 代理)訪問並重新載入時,該欄位不應更改。

事實上,情況恰恰相反。這種奇怪行為的原因是什麼?

您已經定義了一個upstream名為elasticsearch. 但是你不呼叫它。嘗試將您的proxy_pass指令替換為:

proxy_pass http://elasticsearch;

有關詳細資訊,請參閱此文件

引用自:https://unix.stackexchange.com/questions/264403