변군이글루 블로그(Development)
[draft] Cloudflare 프록시 환경에서 Apache의 액세스 로그에 클라이언트의 실제 IP 주소를 남기는 방법 본문
728x90
Cloudflare 프록시 환경에서 Apache의 액세스 로그에 클라이언트의 실제 IP 주소를 남기는 방법(Ubuntu 시스템)
Apache HTTP 서버를 APT 패키지로 설치한 경우
Apache 버전 확인
$ apachectl -v
Server version: Apache/2.4.52 (Ubuntu)
Server built: 2024-04-10T17:45:18
mod_remoteip 모듈 활성화
- a2enmod 명령은 Apache 모듈을 활성화합니다.
sudo a2enmod remoteip
apache2.conf 설정 파일 수정
- LogFormat 수정 : %a는 클라이언트의 실제 IP 주소를 기록합니다.
vim /etc/apache2/apache2.conf
#LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
remoteip 모듈 설정 파일 생성 및 수정
- Cloudflare IP 대역대 등록
vim /etc/apache2/conf-available/remoteip.conf
# CF-IPs
RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 104.16.0.0/13
RemoteIPTrustedProxy 104.24.0.0/14
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 131.0.72.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 2400:cb00::/32
RemoteIPTrustedProxy 2606:4700::/32
RemoteIPTrustedProxy 2803:f800::/32
RemoteIPTrustedProxy 2405:b500::/32
RemoteIPTrustedProxy 2405:8100::/32
RemoteIPTrustedProxy 2a06:98c0::/29
RemoteIPTrustedProxy 2c0f:f248::/32
가상 호스트 설정 파일 수정
- RemoteIPHeader CF-Connecting-IP : Cloudflare가 전달하는 실제 클라이언트 IP를 포함한 헤더를 지정합니다.
vim /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName localhost
ServerAlias localhost.localhost
RemoteIPHeader CF-Connecting-IP
CustomLog "${APACHE_LOG_DIR}/localhost-access.log" combined
ErrorLog "${APACHE_LOG_DIR}/localhost-error.log"
</VirtualHost>
Apache 설정 파일 테스트
- 구성 파일에 오류가 없는지 확인합니다.
sudo apachectl -t
Apache 재시작
sudo systemctl restart apache2
728x90
Apache HTTP 서버를 소스에서 컴파일하여 설치한 경우
Apache 버전 확인
/usr/local/apache2/bin/httpd -v
Server version: Apache/2.4.29 (Unix)
Server built: Mar 23 2020 17:10:43
httpd.conf 설정 파일 수정
vim /usr/local/apache2/conf/httpd.conf
1. mod_remoteip 모듈 로드
LoadModule remoteip_module modules/mod_remoteip.so
2. LogFormat 수정
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
3. Cloudflare IP 범위 설정
# CF-IPs
<IfModule remoteip_module>
RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 104.16.0.0/13
RemoteIPTrustedProxy 104.24.0.0/14
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 131.0.72.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 2400:cb00::/32
RemoteIPTrustedProxy 2606:4700::/32
RemoteIPTrustedProxy 2803:f800::/32
RemoteIPTrustedProxy 2405:b500::/32
RemoteIPTrustedProxy 2405:8100::/32
RemoteIPTrustedProxy 2a06:98c0::/29
RemoteIPTrustedProxy 2c0f:f248::/32
</IfModule>
가상 호스트 설정 파일 수정
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName localhost
ServerAlias localhost.localhost
RemoteIPHeader CF-Connecting-IP
CustomLog "${APACHE_LOG_DIR}/localhost-access.log" combined
ErrorLog "${APACHE_LOG_DIR}/localhost-error.log"
</VirtualHost>
Apache 설정 파일 테스트
/usr/local/apache2/bin/httpd -t
Apache 재시작
/usr/local/apache2/bin/apachectl restart
이 설정을 적용하면 Apache는 Cloudflare 프록시를 통해 전달된 실제 클라이언트의 IP 주소를 액세스 로그에 기록할 수 있습니다.
참고URL
- Cloudflare : IP 범위
728x90
'리눅스' 카테고리의 다른 글
[draft] 도커 컨테이너와 도커 컴포즈에서 타임존을 설정하는 방법 (0) | 2024.07.05 |
---|---|
[draft] Debian 계열의 코드이름 체계 (0) | 2024.07.04 |
[draft] CentOS 7에서 Grafana Loki를 설치하는 방법 (0) | 2024.07.03 |
[draft] Docker에서 Loki 드라이버를 설치하고 구성하는 방법 (0) | 2024.07.02 |
[draft] Promtail을 Docker Compose 파일을 사용하여 설정하는 방법 (0) | 2024.07.01 |