새소식

반응형
Nginx

[NGINX] client ip설정 (proxy_set_header, remote_addr, http_x_forwarded_for, proxy_add_x_forwarded_for)

  • -
반응형

location 하위 블록에 proxy_set_header를 통해서

X-Forwarded-For, X-Real_IP와 같은 header를 세팅한다.

 

그러면 Tomcat과 같은 WAS Layer에서 해당 설정을 읽어서 IP 정보를 사용하게 된다. 

user nginx; worker_processes 2; ... http { include mime.types; upstream back-server { server 127.0.0.1:3000; keepalive 32; } ... server { ... location / { proxy_pass http://back-server; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }

 

구글링을 해보면 X-Forwarded-For에 다양한 설정 값들이 들어가게 된다. 

각각의 값들이 어떠한 의미가 있는지 궁금했는데 

찾아서 정리해 보는 시간을 가져 보자 

 

proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $http_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 

각각의 설정들은 모두 nginx의 설정값들이기 때문에 nginx doc에서 찾으면 어떤 의미인지 알 수 있다. 

 

$remote_addr - client address 

(https://nginx.org/en/docs/http/ngx_http_core_module.html#var_remote_addr)

nginx로 요청을 보낸 client의 address 정보이다.

 

$http_x_forwarded_for - nginx로 들어왔을 때 존재 하던 X-Forwarded-For 설정을 그대로 넘겨준다.

 

$proxy_add_x_forwarded_for - nginx로 들어왔을 때 존재 하던 X-Forwarded-For 설정에 $remote_addr 값 추가 

(https://nginx.org/en/docs/http/ngx_http_proxy_module.html#var_proxy_add_x_forwarded_for)

 

Original request

  • Client IP: 128.128.128.128
  • Request X-Forwarded-For header: 203.0.113.195, 70.41.3.18, 150.172.238.178

$remote_addr value

  • 128.128.128.128

$http_x_forwarded_for value

  • 203.0.113.195, 70.41.3.18, 150.172.238.178

$proxy_add_x_forwarded_for value

  • 128.128.128.128, 203.0.113.195, 70.41.3.18, 150.172.238.178

이외에도 Forwarded라는 헤더 정보가 추가되었다고 한다.

(https://www.nginx.com/resources/wiki/start/topics/examples/forwarded/)

 

nginx에서 X-Forwarded-For 헤더를 이용하기 위해서,

--with-http_realip_module 이 함께 컴파일된 바이너리가 필요

(https://meetup.nhncloud.com/posts/163

 

 

 

예시 출처 - https://www.reddit.com/r/nginx/comments/cqmhga/what_is_the_difference_between_http_x_forwarded/

 

From the nginx community on Reddit: What is the difference between http_x_forwarded_for and proxy_add_x_forwarded_for

Explore this post and more from the nginx community

www.reddit.com

 

반응형

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.