Получить IP-адрес источника запроса - PullRequest
0 голосов
/ 08 октября 2018

Проблема:

  • x-forwarded-for Заголовок http просто показывает 127.0.0.1 вместо исходного ip

Настройка

  • GKE
  • входной контроллер gitlab

Подробности

Я пытался адаптировать правило входа с nginx CORS enable но безуспешно.

Здесь мой вход Аннотация к услуге:

nginx.ingress.kubernetes.io/cors-allow-headers: X-Forwarded-For
nginx.ingress.kubernetes.io/cors-allow-methods: PUT, GET, POST, OPTIONS

А вот вывод через приложение echoheaders:

Hostname: backend-78dd9d4ffd-cwkvv

Pod Information:
    -no pod information available-

Server values:
    server_version=nginx: 1.13.3 - lua: 10008

Request Information:
    client_address=10.60.8.16
    method=GET
    real path=/
    query=
    request_version=1.1
    request_scheme=http
    request_uri=[REDACTED]

Request Headers:
    accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    accept-encoding=gzip, deflate, br
    accept-language=en-GB,en-US;q=0.9,en;q=0.8
    cache-control=max-age=0
    connection=close
    cookie=_ga=[REDACTED]
    host=[REDACTED]
    upgrade-insecure-requests=1
    user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
    x-forwarded-for=127.0.0.1 <--- why doesn't it show the source IP?
    x-forwarded-host=[REDACTED]
    x-forwarded-port=443
    x-forwarded-proto=https
    x-original-uri=/
    x-real-ip=127.0.0.1 <--- why doesn't it show the source IP?
    x-scheme=https

Request Body:
    -no body in request-

1 Ответ

0 голосов
/ 09 октября 2018

X-forwarded-for должно работать из коробки с входным контроллером nginx.Это работает для меня:

$ curl -H 'Host: foo.bar'  aws-load-balancer.us-west-2.elb.amazonaws.com/first


Hostname: http-svc-xxxxxxxxxx-xxxxx

Pod Information:
    node name:  ip-172-x-x-x.us-west-2.compute.internal
    pod name:   http-svc-xxxxxxxxx-xxxxx
    pod namespace:  default
    pod IP: 192.168.x.x

Server values:
    server_version=nginx: 1.13.3 - lua: 10008

Request Information:
    client_address=192.168.x.x
    method=GET
    real path=/first
    query=
    request_version=1.1
    request_uri=http://foo.bar:8080/first

Request Headers:
    accept=*/*
    connection=close
    host=foo.bar
    user-agent=curl/7.58.0
    x-forwarded-for=x.x.x.x <- public IP address
    x-forwarded-host=foo.bar
    x-forwarded-port=80
    x-forwarded-proto=http
    x-original-uri=/first
    x-real-ip=x.x.x.x < - same IP as x-forwarded-for
    x-request-id=xxxxxxxxxxxxxxxxxx
    x-scheme=http

Request Body:
    -no body in request-

Есть несколько вещей, которые вы можете попробовать:

  • Если вы включаете CORS, вам также нужна аннотация включения:

    nginx.ingress.kubernetes.io/enable-cors: "true"
    
  • На вашем входном контроллере есть опция конфигурации use-forwarded-headers для nginx, которую, возможно, потребуется включить.Это включится на ConfigMap, используемом вашим входным контроллером nginx.

...