티스토리 뷰

Programming/Others

X-Forwarded-For

Albothyl 2019. 8. 30. 13:24
  1. X-FORWARDED-FOR 란?

    1. X-Forwarded-For (XFF) 헤더는 HTTP 프록시나 로드 밸런서를 통해 웹 서버에 접속하는 클라이언트의 원 IP 주소를 식별하는 사실상의 표준 헤더다.

    2. 클라이언트와 서버 중간에서 트래픽이 프록시나 로드 밸런서를 거치면, 서버 접근 로그에는 프록시나 로드 밸런서의 IP 주소만을 담고 있다.

    3. 클라이언트의 원래 IP 주소를 보기위해 X-Forwarded-For 요청 헤더가 사용된다.

  2. Nginx

    1. nginx.conf

      server {
      ...
      set $xff $http_x_forwarded_for;
      if ($http_x_forwarded_for ~ "^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})") {
          set $xff $1;
      }
      
      if ($xff = "") {
          set $xff $remote_addr;
      }
      proxy_set_header X-Forwarded-For $xff;
      ...
      }
      
      //acces.log foramt
      log\_format timed\_combined '$remote\_addr - $remote\_user \[$time\_local\] '
                          '"$request" $status $body_bytes_sent '
                          '"$http_referer" "$http_user_agent" $request_time '
                          '$upstream_response_time "$upstream_addr" "$xff"';
    2. AWS 환경에서 웹서버 앞단의 ELB, ALB 를 사용하는 경우 nginx 의 access.log 에 실제 Client IP 가 아닌 Proxy 의 IP 가 기록된다. 이 문제를 해결하기 위해서 Request Header 중 x-forwarded-for값에서 실제 Client IP를 추출하여 사용하도록 Nginx 설정을 변경하였다.

    3. reference :

      1. https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/X-Forwarded-For
  3. Tomcat

    1. server.xml

      <Valve className="org.apache.catalina.valves.RemoteIpValve"
            remoteIpHeader="X-Forwarded-For"
            proxiesHeader="x-forwarded-by"
            internalProxies="10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|192\\.168\\.\\d{1,3}\\.\\d{1,3}|169\\.254\\.\\d{1,3}\\.\\d{1,3}|127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|172\.\d{1,3}\.\d{1,3}\.\d{1,3}"
            protocolHeader="X-Forwarded-Proto"
            protocolHeaderHttpsValue="https"/>
    2. tomcat의 server.xml 중 아래 remoteIpHeader="X-Forwarded-For" 설정은 아래와 같이 동작한다.

      1. x-forwarded-for의 값중 첫 번째 값을 Request.RemoteAddr에 저장한다

      2. x-forwarded-for을 null로 설정한다.

    3. reference :

      1. https://tomcat.apache.org/tomcat-8.5-doc/api/org/apache/catalina/valves/RemoteIpValve.html
  4. Application

    1. Application에서 모든 Request Header를 확인해보면 아래와 같다.

      1. HTTP Request Header

        host: jjhwqqq.practice.com

        x-real-ip: 10.213.147.234

        accept-encoding: gzip, deflate

        connection: close

        x-forwarded-proto: https

        x-forwarded-port: 443

        x-amzn-trace-id: Root=1-5d660d93-7ef238aaad1d8ad2b0fab598

        user-agent: PostmanRuntime/7.15.2

        accept: */*

        cache-control: no-cache

        postman-token: 5cbd1a46-7e91-4b07-8ca0-e7c4baa7d8fe

        cookie: age=31;

Conclusion

  • Application에서 Client IP가 필요할 경우 request.getRemoteAddr()을 사용한다.
  • 다른 Application으로 x-forwarded-for header를 추가하려면 request.getRemoteAddr() + host IP를 추가하거나 x-forwarded-by 설정 후 조합한다.

'Programming > Others' 카테고리의 다른 글

[대규모 시스템 설계 기초] 6장  (0) 2022.07.16
[대규모 시스템 설계 기초] 1장  (0) 2022.06.05
UTC, GMT 차이  (1) 2022.02.04
정규식  (0) 2017.05.16
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함