简体   繁体   中英

Google Cloud TCP Load Balancer forward ip

I use Google Cloud TCP load balancer forwarding requests to a Kubernetes NGINX service. As expected the logs on the NGINX show the Load Balancer IP. How can I retrieve the actual IP

使用$http_x_forwarded_for变量记录用户的原始IP。

The forwarded headers from the Load Balancer are

X-Forwarded-For
X-Forwarded-Proto

Thus on nginx "$http_x_forwarded_for" shall be used

location / {
  ...
  proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
  ...
}

If you are using Kubernetes and services you need to set the extrernal traffic policy to local

kind: Service
apiVersion: v1
metadata:
  name: proxy-service
spec:
  selector:
    app: the-application
  type: LoadBalancer
  externalTrafficPolicy: Local
  ports:
  - protocol: TCP
    port: 443
    targetPort: 443
    name: https

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM