[英]Adding Headers to NGINX Ingress using ConfigMap not working
我正在嘗試使用 Azure 上的 Kubernetes 通過我的 NGINX Ingress 傳遞我的客戶端 IP 地址
我已經為 NGINX 配置創建了這個配置映射來添加標頭:
apiVersion: v1
data:
X-Real-IP: $remote_addr;
X-Forwarded-For: $proxy_add_x_forwarded_for;
X-Forwarded-Proto: $proxy_x_forwarded_proto;
use-forwarded-headers: "true"
use-proxy-protocol: "true"
real-ip-header: "proxy_protocol"
kind: ConfigMap
metadata:
name: custom-headers
namespace: default
然后添加此配置以引用上一個文件:
apiVersion: v1
data:
proxy-set-headers: "custom-headers"
externalTrafficPolicy: Local
kind: ConfigMap
metadata:
name: ingress-nginx-controller
namespace: default
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
如果我描述我的 nginx 控制器:
kubectl describe deploy ingress-nginx-controller
我可以看到這一行:
--configmap=$(POD_NAMESPACE)/ingress-nginx-controller
如果我描述 ingress-nginx-controller configmap
kubectl describe configmap ingress-nginx-controller
我可以在數據部分看到以下內容:
proxy-set-headers:
----
custom-headers
但是,如果我注銷控制器中的 nginx.conf 文件,則看不到更改的值。 例如,這仍然是默認值:
proxy_set_header X-Forwarded-For $remote_addr;
您缺少 proxy-set-headers 值的命名空間前綴。 因為您已將custom-headers
configmap 部署到默認命名空間,所以它應該是
data:
proxy-set-headers: default/custom-headers
我已經運行了一些測試,用戶Yadhu提供的答案應該是正確的。
問題解釋:您正在使用ConfigMaps 的proxy-set-headers
配置選項。 這里有很好的描述:
在將流量發送到后端之前,從命名的 configmap 設置自定義標頭。 值格式為命名空間/名稱。 查看示例
在此示例中,您可以通過 ConfigMap 找到 nginx 入口控制器的配置,以將自定義的標頭列表傳遞給上游服務器。
看例子yaml:
apiVersion: v1
data:
proxy-set-headers: "ingress-nginx/custom-headers"
kind: ConfigMap
metadata:
name: ingress-nginx-controller
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
問題解答:您需要在此選項中使用正確的格式:
proxy-set-headers: "<your namespace>/custom-headers"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.