簡體   English   中英

Nginx:響應處理和帶緩存的反向代理

[英]Nginx: Response handling and Reverse Proxy with Cache

我正在運行Nginx作為一個簡單的反向代理和緩存,現在我想根據收到的響應(正文不是標題)設置一些標題。

獲得location_capture_by Lua看起來很簡單,但似乎我無法讓緩存正常工作。

初步設置是:

location / {
..
try_files $uri @upstream_server
}

location @upstream_server {
      proxy_pass "http://web_lb";
      proxy_cache small;
      proxy_cache_methods POST;
      proxy_cache_key "$request_uri|$request_body";
      client_max_body_size 500M;
      add_header X-Cached $upstream_cache_status;
      set_real_ip_from   10.86.102.0/24;
      real_ip_header      X-Forwarded-For;
      proxy_ignore_headers Set-Cookie;
      proxy_ignore_headers Cache-Control;
}

改為:


location /{
 content_by_lua '
     local res = ngx.location.capture(
                "/new-location",
                { method = ngx.HTTP_POST, body = ngx.var.request_body})
     #update response body here and header etc based on content
      ngx.say(res.body)
      '; }

location new-location{
try_files $uri @upstream_server
}

location @upstream_server {
      proxy_pass "http://web_lb;"
      proxy_cache small;
      proxy_cache_methods POST;
      proxy_cache_key "$request_uri|$request_body";
      client_max_body_size 500M;
      add_header X-Cached $upstream_cache_status;
      set_real_ip_from   10.86.102.0/24;
      real_ip_header      X-Forwarded-For;
      proxy_ignore_headers Set-Cookie;
      proxy_ignore_headers Cache-Control;
}

======

我發現我丟失了所有原始標題,作為Proxy_Header處理的一部分添加了一個標題,包括upstream_cache_status標題。 但是我發現Nginx仍然提供來自緩存本身的重復請求。

有什么理由會這樣嗎? 我也是這里的早期首發,這是一些基本陷阱的借口。

實際上,location.capture並不是為了做你正在做的事情而設計的,但是,如果我告訴你(你希望發送瀏覽器發送給子請求的標題),你可能會使用ngx.ctx + set;來破解它。)

但我會說這是一種非常骯臟的克拉迪方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM