
我正在使用 Nginx 來快取一些回應。產生這些回應的後端Cache-control
為所有回應設定一個公共標頭。但是,我需要將某些響應快取的時間比其他回應的快取時間更長。也就是說,我需要cache-control
在指令考慮標頭之前修改標頭proxy_pass
。
我正在使用ngx_lua_module
並希望使用指令修改位置區塊Cache-Control
中的標頭。預期的配置如下所示:internal
header_filter_by_lua_block
location / {
proxy_pass /actual;
proxy_cache something;
}
location = /actual {
internal;
proxy_pass https://backend;
proxy_cache off;
header_filter_by_lua_block {
-- modify cache-control header based on request/response parameters
}
}
但是我無法找到一種方法來通過proxy_pass
.我將不勝感激您對這項工作的任何見解。
答案1
您不能 proxy_pass
存取某個位置,只能proxy_pass
存取上游或 URL(基本上是未聲明的上游)。因此,正式回答你的問題,你proxy_pass
應該本機將 Host 標頭設定為目前server_name
;但這可能會使事情變得過於複雜。
反而- 看起來你需要做的就是刪除location / {}
你不需要的,然後重命名location = /actual
為location / {}
.
我還想說你根本不需要 lua - 只需刪除從代理網絡獲取的標頭proxy_hide_header
並添加你自己的標頭即可add_header
。
答案2
一般來說,要將控制權傳遞給另一個位置區塊,您應該使用內部重定向(改寫), 不是proxy_pass
:
location / {
rewrite ^.*$ /actual;
}
若要修改上游標頭,您可以使用代理設定頭:
location /actual {
proxy_set_header Cache-Control '<your value>';
}
若要修改下游標頭,您可以使用更多設定標題。它需要使用附加模組進行自訂 Nginx 構建,但它在您的情況下非常強大:
location /actual {
more_set_headers 'Cache-Control: <your value>';
}
考慮到問題的標題,您還可以做出一些硬核的事情,例如切換伺服器來處理客戶端流量。我不會推薦它來完成如此瑣碎的任務,但在極少數情況下它會有所幫助:
http {
upstream internal_http_routing {
server unix:var/internal.sock;
}
server {
# Internal interface
listen unix:var/internal.sock;
location / {
return 200;
}
}
server {
# Client-facing interface
listen 443 ssl;
location / {
proxy_pass http://internal_http_routing;
}
}
}
tcp {
upstream internal_tcp_routing {
server unix:var/internal.sock;
}
server {
# Client-facing interface
listen 8443 ssl;
proxy_pass internal_tcp_routing;
}
}