OS/Windows

[windows nginx + tomcat] remote_addr (client IP)를 얻는 방법

노루아부지 2019. 7. 28. 19:01

XFF는 HTTP Header 중 하나로 HTTP Server에 요청한 client의 IP를 식별하기 위한 사실상의 표준이다.

 

웹 서버나 WAS 앞에 L4같은 Load balancers나 Proxy Server, caching server, HTTP 서버용 WAS Connector 등이 있을 경우

이런 제품들은 웹서버/WAS에 HTTP나 전용 프로토콜(AJP)로 요청을 보낸 후에 받은 결과를 가공하여 클라이언트에 재전송하게 된다.

 

이로 인해 처리한 웹 서버나 WAS에서 request.getRemoteAddr(); 등으로 클라이언트 IP를 얻을 경우 

L4나 Proxy의 IP를 얻게 되는데 이는 원하는 결과가 아니다.

 

X-Forwarded-For는 이 문제를 해결하기 위해 사용하는 HTTP Header로 squid caching server에서 처음 사용되었다.

다음과 같이 콤마를 구분자로 client와 proxy IP가 들어가게 되므로 첫번째 IP를 가져오면 클라이언트를 식별할 수 있다.

 

[WAS 에서 사용]

 

여기까지 읽고 웹 어플리케이션을 개발할 경우 client ip 를 식별할 필요가 있다면 먼저 헤더가 있는지 확인한 후에 없으면 getRemoteAddr() 로 IP 를 얻으면 되겠지라고 생각할 수도 있겠지만 이게 끝은 아니다.

XFF 는 사실상의 표준이지 정식 RFC 에 포함된게 아니므로 대개는 착실하게 저 헤더를 사용하지만 엉뚱한 헤더를 사용하는 제품들이 있다.

그중에 하나인 WebLogic Connector(mod_wl) 는 저 헤더를 사용하지 않고 WL-Proxy-Client-IP 나 Proxy-Client-IP  같은 전혀 엉뚱한 헤더를 사용하므로 만약 만드는 웹 어플리케이션이 WebServer, WAS, L4, proxy 종류에 상관없이 client IP 를 잘 가져오기를 바란다면 다음과 같은 순서로 IP 를 얻어내야 한다.

 

 String ip = request.getHeader("X-Forwarded-For");

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 

     ip = request.getHeader("Proxy-Client-IP"); 

 } 

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 

     ip = request.getHeader("WL-Proxy-Client-IP"); 

 } 

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 

     ip = request.getHeader("HTTP_CLIENT_IP"); 

 } 

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 

     ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 

 } 

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 

     ip = request.getHeader("X-Real-IP"); 

 } 

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 

     ip = request.getHeader("X-RealIP"); 

 } 

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 

     ip = request.getRemoteAddr(); 

 }

 

 

* 헤더에 IP가 없는 경우에는 패킷의 Client정보를 HTTP Header에 추가하는 작업을 해야 한다.

 

 

[ windows nginx + tomcat 연동 ]

 

Step 1. nginx download

 

nginx의 설치는 다운로드 후 압축만 풀면 끝.

 

Download URL : http://nginx.org/en/download.html

 

 

 

 

 

Step 2. nginx 실행

 

다운받은 nginx의 압축을 풀고 nginx를 실행

 

 

 

 

Step 3. Tomcat 설치

 

Step 4. nginx + tomcat  연동

 

reverse proxy 방식의 Tomcat 연동 설정 ( JSP로 오는 요청은 tomcat으로 proxy )

 

conf/nginx.conf 의 내용 수정

 

http {

    [중간생략]

    server {

        # nginx의 port

        listen 8980;

        [중간생략]

        location ~ \.jsp$ {

            # proxy 할 IP와 port ( 여기서는 tomcat의 port )

            proxy_pass   http://127.0.0.1:8080;

            proxy_set_header Host $http_host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }

    }

}

 

Step 5.JSP 파일 작성

 

<%@page import="java.util.Enumeration"%>

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR" %>

 

<%

Enumeration headerNames = request.getHeaderNames();

while(headerNames.hasMoreElements()){

    String name = (String)headerNames.nextElement();

    String value = request.getHeader(name);

    out.println(name + " : " + value + "<br>");

}

%>

 

Step 6. 브라우저에서 접속하여 확인

 

* 반드시 nginx의 port로 접속해야 함

 

http://192.168.0.83:8980/jsp/ApproveForPrint/aaa.jsp

 

 

 

출처 : https://www.lesstif.com/pages/viewpage.action?pageId=20775886

http://rocksea.tistory.com/68

 

 

 

728x90
loading