Cloudflare를 사용할 때 접속자 IP가 실제 사용자 IP가 아니라 Cloudflare 프록시 IP로 보이는 문제는 보통 다음 두 가지를 설정하면 해결됩니다.

핵심은 Cloudflare가 전달하는 CF-Connecting-IP 헤더를 nginx가 실제 클라이언트 IP로 인식하게 하는 것입니다.

1️⃣ nginx 설정 (Cloudflare 실제 IP 적용)

# Cloudflare IP ranges
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;

# 실제 사용자 IP 헤더
real_ip_header CF-Connecting-IP;
real_ip_recursive on;

2️⃣ 로그 포맷 수정 (선택)

로그에 실제 IP가 남도록 log_format을 설정합니다.

log_format main '$remote_addr - $remote_user [$time_local] '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';

access_log /var/log/nginx/access.log main;

$remote_addr가 이제 실제 사용자 IP가 됩니다.

3️⃣ nginx 재시작

sudo nginx -t
sudo systemctl reload nginx

4️⃣ 정상 작동 확인

tail -f /var/log/nginx/access.log

또는

tail -f /var/log/nginx/error.log

이전에 출력되던 IP는 172.68.x.x  (Cloudflare IP) 로 출력 되었지만, 

이후 123.45.67.89 (실제 사용자 IP)로 출력되는 것을 확인하실 수 있습니다.