如何在 NGINX Ingress Controller 中添加 X-Original-Forwarded-For 作为请求标头?

问题描述 投票:0回答:1

我正在使用 F5 NGINX 入口控制器。

注意:我没有使用 Kubernetes 社区版(Ingress-nginx)

我已经使用 NGINX Ingress Controller 的虚拟服务器实现了流量拆分。然而,在生产中,一些客户端正在使用 IP 白名单,为了传递请求,我们使用 header

X-Original-Forwarded-For

我想在 NGINX Ingress Controller 中设置

X-Original-Forwarded-For
请求标头

https://docs.nginx.com/nginx-ingress-controller/configuration/global-configuration/configmap-resource/#configmap-and-virtualservervirtualserverroute-resources

根据上述入口级别的链接设置,它也会影响虚拟服务器。

我正在使用 helm 和 env.yaml 来 isntallnginx 入口控制器,但到目前为止我们还没有运气

env.yaml

# Traffic split Nginx Ingress 
controller:
  replicaCount: 2
  config:
    log-format-upstream: '$http_x_forwarded_for - $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_id'
  service:
    loadBalancerIP: "xxx.xxxx.xxxx"
    externalTrafficPolicy: "Local"
    annotations:
      service.beta.kubernetes.io/azure-load-balancer-resource-group: "xxxxxxx"
      nginx.org/location-snippets: |
         more_set_input_headers "X-Original-Forwarded-For: jatin";

安装helm命令

helm upgrade --install nginx-ingress nginx-stable/nginx-ingress \
  --version 1.0.2 -n nginx-ingress --create-namespace \
  --set controller.ingressClass.create=true \
  --set controller.ingressClass.name=nginx-ingress \
  --set controller.ingressClass.setAsDefaultIngress=false \
  -f ./nginx-ingress/env/env.yaml

甚至尝试添加虚拟服务器资源,如下所示这里

apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
  namespace: xxxxxx
  name: nginx-vs-traffic-split
  annotations:
    nginx.ingress.kubernetes.io/ingress.class: nginx-ingress

spec:
  host: xxxxx.com
  upstreams:
  - name: xxxxx-a
    service: xxxx-a
    port: 80
  - name: xxxx-b
    service: xxxx-b
    port: 80
  - name: xxxx-c
    service: xxxx-c
    port: 80
  routes:
  - path: /
    location-snippets: |
      add_header my-test-header test-value;   
    splits:
    # The sum of the weights of all splits must be equal to 100
    - weight: 33
      action:
        pass: xxxxx-a
    - weight: 33
      action:
        pass: xxxx-b
    - weight: 34
      action:
        pass: xxxxx-c 

更新:

https://docs.nginx.com/nginx-ingress-controller/installation/ingress-nginx/#migration-with-kubernetes-ingress-resources

我看到这个链接,其中配置片段在 nginx 中使用位置片段

我什至尝试了这个仍然没有运气

azure kubernetes nginx nginx-ingress ingress-controller
1个回答
0
投票

log-format-upstream
是社区入口控制器配置映射键。当您使用 NGINX Ingress Controller 时,您正在寻找的键是
log-format
。这些与日志记录相关的配置映射键列于here

确保将包含原始 IP 的标头也添加到日志格式中。

config:
    log-format: '$http_x_original_forwarded_for ...'
© www.soinside.com 2019 - 2024. All rights reserved.