Google Cloud TCP负载均衡器转发IP

时间:2019-08-06 10:06:32

标签: nginx google-cloud-platform google-cloud-load-balancer

我使用Google Cloud TCP负载平衡器将请求转发到Kubernetes NGINX服务。如预期的那样,NGINX上的日志显示了负载均衡器IP。如何获取实际IP

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

Load Balancer中转发的标头是

X-Forwarded-For
X-Forwarded-Proto

因此应在nginx上使用“ $ http_x_forwarded_for”

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

如果您正在使用Kubernetes和服务,则需要将外部流量策略设置为本地

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