Skip to content

Instantly share code, notes, and snippets.

@nczz
Created February 20, 2020 14:06
Show Gist options
  • Save nczz/8b7205a43c92f6fb8256ded5e3cecbee to your computer and use it in GitHub Desktop.
Save nczz/8b7205a43c92f6fb8256ded5e3cecbee to your computer and use it in GitHub Desktop.
Nginx 取回 Cloudflare 代理後的瀏覽者真實 IP(搭配 http_realip_module 模組) Ref: https://virtubox.github.io/nginx-cloudflare-real-ip/
#!/bin/bash
if [ -z "$(command -v curl)" ]; then
echo "####################################"
echo "Installing CURL"
echo "####################################"
apt-get update
apt-get install curl -y
fi
CURL_BIN=$(command -v curl)
CF_IPV4=$($CURL_BIN -sL https://www.cloudflare.com/ips-v4)
CF_IPV6=$($CURL_BIN -sL https://www.cloudflare.com/ips-v6)
echo '' > cloudflare.conf
echo "####################################"
echo "Adding Cloudflare IPv4"
echo "####################################"
for cf_ip4 in $CF_IPV4; do
echo "set_real_ip_from $cf_ip4;" >> cloudflare.conf
done
echo "####################################"
echo "Adding Cloudflare IPv6"
echo "####################################"
for cf_ip6 in $CF_IPV6; do
echo "set_real_ip_from $cf_ip6;" >> cloudflare.conf
done
echo 'real_ip_header CF-Connecting-IP;' >> cloudflare.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment