
我剛剛讀過這個數位海洋文章想知道是否拓樸結構最後的例子可以用nginx來實現。
我對......有興趣負載平衡器如何處理不在應用程式伺服器前面的兩個專用快取伺服器。他們這樣描述這個過程:
- 負載平衡器檢查快取後端以查看請求的內容是否已快取(快取命中)或未快取(快取未命中)
- 如果cache-hit:將請求的內容傳回給負載平衡器,並跳到步驟7。
- 負載平衡器將請求轉發到應用程式後端
- 應用程式後端從資料庫讀取然後將請求的內容傳回負載平衡器
- 負載平衡器將回應轉發到快取後端
- 快取後端快取內容然後將其返回到負載平衡器
- 負載平衡器將請求的資料傳回給用戶
我想負載平衡器應該在上游指令中有兩組:
upstream cachebackend {
server cache-1.example.com;
server cache-2.example.com;
}
upstream appbackend {
server app-1.example.com;
server app-2.example.com;
}
然後從伺服器指令內部:
location / {
proxy_pass http://cachebackend;
# if that one is a MISS, request this one:
# proxy_pass http://appbackend;
# and then save the response on the cachebackend
# before returning it to the client
}
我想知道如何告訴 nginx 遵循上述步驟,或者是否可能。
謝謝 :)
答案1
Nginx 可以同時進行負載平衡和緩存,您只需使用 usingproxy_cache_path
指令配置緩存區域並將其分配給特定的server {}
或location {}
using proxy_cache
.因此,結論是,如果使用 nginx 作為負載平衡器和緩存,Digital Ocean 架構看起來是多餘的。