Apache Log에 실제 IP 주소가 나오도록하기

일단 cloudflare 의 글을 참조 하여서 작성되었습니다.
참조 주소는 “https://developers.cloudflare.com/support/troubleshooting/restoring-visitor-ips/restoring-original-visitor-ips/” 입니다.

아파치 기능을 활성화 합니다.

sudo a2enmod remoteip

비활성화시는 다음과 같은 명령어를 사용 하시면 됩니다.

sudo a2dismod remoteip

단일 서버이면 apache 환경설정에 설정하시면 되고, 아닐 경우 각각의 virtualhost에 설정을 하시면 됩니다.
아래 내용이 설정할 내용입니다.

RemoteIPHeader CF-Connecting-IP

아래는 위 구분을 추가 할 경우에 예시일뿐이니 각자 설정에 맞춰서 설정해 주시기 바랍니다.

RemoteIPHeader CF-Connecting-IP
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

이후에 아파치 환경설정도 변경을 해주시면 됩니다.
/etc/apache2/apache2.conf 파일에서 LogFormat에서 “%h” 부분을 “%a”로 변경합니다.

아래는 변경하고 난 후의 예시 입니다.

LogFormat "%v:%p %a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%a %l %u %t \"%r\" %>s %O" common

그리고 나서 이제 cloudflar의 IP를 허용을 해주어야 하기 때문에 아래의 파일을 수정 합니다.
해당 파일이 있을수도 없을수 있으니 확인후 없으면 생성 하시면 됩니다.

/etc/apache2/conf-available/remoteip.conf

remoteip.conf에 들어갈 내용이며 아래 내용을 추가 하실때는 “https://www.cloudflare.com/ko-kr/ips/” 를 참조 하시면 됩니다.

RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 104.16.0.0/13
RemoteIPTrustedProxy 104.24.0.0/14
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 131.0.72.0/22

이제 설정을 모두 하셨으면, 기능을 활성화를 진행합니다.

sudo a2enconf remoteip

기능을 해제 하고 싶으시면 아래의 명령어로 처리 하면 됩니다.

sudo a2disconf remoteip

문법체크를 진행하고시고 난 다음에 apache를 재시작 해주시면 됩니다.

sudo apache2ctl configtest
sudo systemctl restart apache2

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다