為您的搜尋引擎設定Nginx

Elasticsearch本主題說明如何結合使用傳輸層安全性(TLS)加密與 HTTP基本驗證. 您也可以選擇設定其他型別的驗證;我們提供該資訊的參考。

(舊稱Secure Sockets Layer (SSL),經常與TLS互換使用。 在本主題中,我們稱為 TLS.)

WARNING
除非另有說明,否則本主題中的所有命令都必須以使用者身分輸入, root 許可權。

Recommendations

我們建議以下事項:

  • 您的網頁伺服器使用TLS。

    TLS不在本主題的討論範圍內;不過,我們強烈建議您在生產中使用真正的憑證,而不是自我簽署憑證。

  • 您的搜尋引擎會在與網頁伺服器相同的主機上執行。 在不同主機上執行搜尋引擎和網頁伺服器不在本主題的討論範圍內。

    將搜尋引擎和網頁伺服器放在同一部主機上的優點是,它無法攔截加密的通訊。 搜尋引擎網頁伺服器不必與Adobe Commerce網頁伺服器相同;例如,Adobe Commerce可以執行Apache,而Elasticsearch/OpenSearch可以執行nginx。

    如果搜尋引擎公開於公用網站,您應該設定驗證。 如果您的搜尋引擎執行個體在網路上受到保護,則可能沒有必要。 請與您的託管提供者合作,決定您應實作哪些安全性措施來保護您的執行個體。

更多有關TLS的資訊

請參閱下列資源之一:

設定proxy

NOTE
2.4.4版本新增OpenSearch支援。OpenSearch是相容的Elasticsearch復本。 另請參閱 將Elasticsearch移轉至OpenSearch 以取得詳細資訊。

本節探討如何將nginx設定為 不安全 Proxy,讓Adobe Commerce能夠使用在此伺服器上執行的搜尋引擎。 本節不討論設定HTTP基本驗證;這將在中討論 與nginx的安全通訊.

NOTE
在此範例中,Proxy不受保護的原因是它更容易設定和驗證。 您可以視需要搭配此Proxy使用TLS;若要這樣做,請確定將Proxy資訊新增至安全伺服器區塊設定。

指定全域組態中的其他組態檔

確定您的全域 /etc/nginx/nginx.conf 包含下列行,因此會載入下列各節中討論的其他組態檔案:

include /etc/nginx/conf.d/*.conf;

將nginx設為Proxy

本節討論如何指定誰可以存取nginx伺服器。

  1. 使用文字編輯器建立檔案 /etc/nginx/conf.d/magento_es_auth.conf 包含下列內容:

    code language-conf
    server {
       listen 8080;
       location /_cluster/health {
          proxy_pass http://localhost:9200/_cluster/health;
       }
    }
    
  2. 重新啟動nginx:

    code language-bash
    service nginx restart
    
  3. 輸入下列命令來驗證Proxy是否有效:

    code language-bash
    curl -i http://localhost:<proxy port>/_cluster/health
    

    例如,如果您的Proxy使用連線埠8080:

    code language-bash
    curl -i http://localhost:8080/_cluster/health
    

    類似下列顯示以指示成功的訊息:

    code language-terminal
    HTTP/1.1 200 OK
    Date: Tue, 23 Feb 2019 20:38:03 GMT
    Content-Type: application/json; charset=UTF-8
    Content-Length: 389
    Connection: keep-alive
    
    {"cluster_name":"elasticsearch","status":"yellow","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":5,"active_shards":5,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":5,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":50.0}
    

與nginx的安全通訊

本節將討論如何設定 HTTP基本驗證 使用您的安全Proxy。 同時使用TLS和HTTP Basic驗證可防止任何人攔截與Elasticsearch、OpenSearch或您的應用程式伺服器的通訊。

由於nginx原生支援HTTP基本驗證,因此建議您不要使用,例如 摘要式驗證,在生產環境中不建議使用。

其他資源:

如需詳細資訊,請參閱下列章節:

建立密碼

建議您使用Apache htpasswd 為可存取Elasticsearch或OpenSearch的使用者編碼密碼的命令(已命名) magento_elasticsearch 在此範例中)。

若要建立密碼:

  1. 輸入以下命令以確定 htpasswd 已安裝:

    code language-bash
    which htpasswd
    

    如果顯示路徑,則安裝路徑;如果命令未傳回任何輸出, htpasswd 未安裝。

  2. 如有必要,請安裝 htpasswd

    • Ubuntu: apt-get -y install apache2-utils
    • CentOS: yum -y install httpd-tools
  3. 建立 /etc/nginx/passwd 儲存密碼的目錄:

    code language-bash
    mkdir -p /etc/nginx/passwd
    
    code language-bash
    htpasswd -c /etc/nginx/passwd/.<filename> <username>
    
    note warning
    WARNING
    基於安全考量, <filename> 應隱藏;也就是說,必須以句點開頭。
  4. (選擇性)。 若要將其他使用者新增至您的密碼檔案,請輸入相同的命令,而不使用 -c (建立)選項:

    code language-bash
    htpasswd /etc/nginx/passwd/.<filename> <username>
    
  5. 確認的內容 /etc/nginx/passwd 是正確的。

設定nginx的存取權

本節討論如何指定誰可以存取nginx伺服器。

WARNING
範例適用於 不安全 proxy。 若要使用安全Proxy,請將下列內容(監聽連線埠除外)新增至您的安全伺服器區塊。

使用文字編輯器修改 /etc/nginx/conf.d/magento_es_auth.conf (不安全)或您的安全伺服器區塊,其內容如下:

server {
  listen 8080;
  server_name 127.0.0.1;

  location / {
   limit_except HEAD {
      auth_basic "Restricted";
      auth_basic_user_file  /etc/nginx/passwd/.htpasswd_magento_elasticsearch;
   }
   proxy_pass http://127.0.0.1:9200;
   proxy_redirect off;
   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location /_aliases {
   auth_basic "Restricted";
   auth_basic_user_file  /etc/nginx/passwd/.htpasswd_magento_elasticsearch;
   proxy_pass http://127.0.0.1:9200;
   proxy_redirect off;
   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  include /etc/nginx/auth/*.conf;
}
NOTE
上述範例中顯示的搜尋引擎監聽連線埠只是範例。 基於安全考量,建議您使用非預設監聽連線埠。

為搜尋引擎設定受限制的內容

本節探討如何指定誰可以存取搜尋引擎伺服器。

  1. 輸入以下命令來建立儲存驗證組態的目錄:

    code language-bash
    mkdir /etc/nginx/auth/
    
  2. 使用文字編輯器建立檔案 /etc/nginx/auth/magento_elasticsearch.conf 包含下列內容:

    code language-conf
    location /elasticsearch {
    auth_basic "Restricted - elasticsearch";
    auth_basic_user_file /etc/nginx/passwd/.htpasswd_magento_elasticsearch;
    
    proxy_pass http://127.0.0.1:9200;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    
  3. 如果您設定安全Proxy,請刪除 /etc/nginx/conf.d/magento_es_auth.conf.

  4. 重新啟動nginx並繼續下一節:

    code language-bash
    service nginx restart
    

驗證

本節將討論驗證HTTP基本驗證是否正常運作的兩種方式:

  • 使用 curl 命令驗證您必須輸入使用者名稱和密碼才能取得叢集狀態
  • 在Admin中設定HTTP基本驗證

使用 curl 驗證叢集狀態的命令

輸入下列命令:

curl -i http://<hostname, ip, or localhost>:<proxy port>/_cluster/health

例如,如果您在搜尋引擎伺服器上輸入命令,且您的Proxy使用連線埠8080:

curl -i http://localhost:8080/_cluster/health

下列訊息會顯示以指出驗證失敗:

HTTP/1.1 401 Unauthorized
Date: Tue, 23 Feb 2016 20:35:29 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Restricted"
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
  <center><h1>401 Authorization Required</h1></center>
</body>
</html>

現在嘗試下列命令:

curl -i -u <username>:<password> http://<hostname, ip, or localhost>:<proxy port>/_cluster/health

例如:

curl -i -u magento_elasticsearch:mypassword http://localhost:8080/_cluster/health

這次命令會成功,並出現類似下列的訊息:

HTTP/1.1 200 OK
Date: Tue, 23 Feb 2016 20:38:03 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 389
Connection: keep-alive
{"cluster_name":"elasticsearch","status":"yellow","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":5,"active_shards":5,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":5,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":50.0}

在Admin中設定HTTP基本驗證

執行中討論的相同工作 搜尋引擎設定 除了 按一下 YesEnable HTTP Auth 清單並在提供的欄位中輸入您的使用者名稱和密碼。

按一下 Test Connection 以確定其運作正常,然後按一下 Save Config.

您必須先清除快取並重新索引,才能繼續。

recommendation-more-help
0f8e7db5-0e9c-4002-a5b8-a0088077d995