<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cloudflare Archives - Rhein-Ruhr-Informatik</title>
	<atom:link href="https://rhein-ruhr-informatik.de/en/tag/cloudflare/feed/" rel="self" type="application/rss+xml" />
	<link>https://rhein-ruhr-informatik.de/en/tag/cloudflare/</link>
	<description></description>
	<lastbuilddate>Mon, 03 Mar 2025 09:55:38 +0000</lastbuilddate>
	<language>en-US</language>
	<sy:updateperiod>
	hourly	</sy:updateperiod>
	<sy:updatefrequency>
	1	</sy:updatefrequency>
	

<image>
	<url>https://rhein-ruhr-informatik.de/wp-content/uploads/2025/03/cropped-favicon-32x32.png</url>
	<title>cloudflare Archives - Rhein-Ruhr-Informatik</title>
	<link>https://rhein-ruhr-informatik.de/en/tag/cloudflare/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Wie man eine echte IP über Cloudflare erhält &#8211; Nginx Reverse Proxy &#8211; Docker</title>
		<link>https://rhein-ruhr-informatik.de/en/2025/01/29/echte-ip-ueber-cloudflare-erhaelt-nginx-reverse-proxy-docker/</link>
		
		<dc:creator><![CDATA[Rhein-Ruhr-Informatik]]></dc:creator>
		<pubdate>Wed, 29 Jan 2025 17:58:22 +0000</pubdate>
				<category><![CDATA[Artikel]]></category>
		<category><![CDATA[Cloudflare]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[cloudflare]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[nginx]]></category>
		<guid ispermalink="false">https://rhein-ruhr-informatik.de/?p=359</guid>

					<description><![CDATA[<p>When using Cloudflare as a CDN and DDoS protection service, it acts as an intermediary between your website and its visitors. This means that by default, the IP address visible to your web server is the one belonging to Cloudflare, not the original visitor. This can create issues for logging, analytics, and security policies based on IP addresses. In this article, we will walk through the steps necessary to configure your Nginx server to properly log and use the real IP addresses of visitors when using Cloudflare, even within a Dockerized environment.<a class="moretag" href="https://rhein-ruhr-informatik.de/en/2025/01/29/echte-ip-ueber-cloudflare-erhaelt-nginx-reverse-proxy-docker/"> Read more&#8230;</a></p>
<p>The post <a href="https://rhein-ruhr-informatik.de/en/2025/01/29/echte-ip-ueber-cloudflare-erhaelt-nginx-reverse-proxy-docker/">Wie man eine echte IP über Cloudflare erhält &#8211; Nginx Reverse Proxy &#8211; Docker</a> appeared first on <a href="https://rhein-ruhr-informatik.de/en">Rhein-Ruhr-Informatik</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>Introduction</strong></p>



<p>When using Cloudflare as a CDN and DDoS protection service, it acts as an intermediary between your website and its visitors. This means that by default, the IP address visible to your web server is the one belonging to Cloudflare, not the original visitor. This can create issues for logging, analytics, and security policies based on IP addresses. In this article, we will walk through the steps necessary to configure your Nginx server to properly log and use the real IP addresses of visitors when using Cloudflare, even within a Dockerized environment.</p>



<p><strong>Understanding the Problem</strong></p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="945" height="124" src="https://rhein-ruhr-informatik.de/wp-content/uploads/2024/10/image-10.png" alt="" class="wp-image-362" srcset="https://rhein-ruhr-informatik.de/wp-content/uploads/2024/10/image-10.png 945w, https://rhein-ruhr-informatik.de/wp-content/uploads/2024/10/image-10-300x39.png 300w, https://rhein-ruhr-informatik.de/wp-content/uploads/2024/10/image-10-768x101.png 768w, https://rhein-ruhr-informatik.de/wp-content/uploads/2024/10/image-10-18x2.png 18w" sizes="(max-width: 945px) 100vw, 945px" /></figure>



<p>When a visitor connects to your website through Cloudflare, the original IP address is masked by Cloudflare’s own IP. This helps in protecting your server from direct attacks, but it makes it difficult to identify the true visitor IP. To solve this issue, Cloudflare includes headers like <code>X-Forwarded-For</code> and <code>CF-Connecting-IP</code> , which contain the original IP of the client. Nginx, by default, does not use these headers, so additional configuration is needed.</p>



<p><strong>Prerequisites</strong></p>



<p>Before starting, ensure you have:</p>



<ol start="1" class="wp-block-list">
<li>On Nginx server installed with the <code>ngx_http_realip_module </code>module enabled. This module is necessary to correctly handle the real IP headers.</li>



<li>A basic understanding of Nginx configuration and Docker.</li>



<li>Access to your server’s configuration files.</li>
</ol>



<p><strong>Step 1: Configuring Nginx to Use Real IP Headers</strong></p>



<p>To make Nginx recognize the real IP from the headers provided by Cloudflare, you need to modify the configuration. First, ensure that the <code>ngx_http_realip_module </code>module is installed. This module is usually included in most Nginx installations by default.</p>



<p>Next, create a file named <code>Cloudflare </code>in the <code>/etc/nginx/</code> directory with the following content:</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p></p>



<pre class="wp-block-code"><code>set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
...
real_ip_header CF-Verbindungs-IP;
</code></pre>



<p>This file lists all Cloudflare IP ranges and instructs Nginx to trust these IPs and use the CF-Connecting-IP header for the real IP. For future updates, it’s important to regularly check Cloudflare’s IP ranges, as they might change over time.</p>



<p><strong>Step 2: Including the Cloudflare Configuration in Nginx</strong></p>



<p>Edit the main Nginx configuration file, usually located at <code>/etc/nginx/nginx.conf</code> In the <code>http {}</code> section, include the newly created cloudflare configuration file:</p>
</div></div>



<pre class="wp-block-code"><code>http {
     include /etc/nginx/cloudflare;
     ...
}</code></pre>



<p>This ensures that the real IP configuration is applied globally across all your server blocks.</p>



<p><strong>Step 3: Logging Real IP Addresses</strong></p>



<p>To include the original visitor IP in your logs, modify the log_format directive in your <code>nginx.conf</code>to use the variables <code>$http_cf_connecting_ip</code> and <code>$http_x_forwarded_for</code> . For example:</p>



<pre class="wp-block-code"><code>log_format main '$remote_addr - $http_cf_connecting_ip &#91;$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';</code></pre>



<p>This allows you to see both the Cloudflare IP and the original visitor IP in your logs.</p>



<p><strong>Step 4: Applying the Changes</strong></p>



<p>To apply the changes, test the Nginx configuration:</p>



<ul class="wp-block-list">
<li>For CentOS/RHEL:</li>
</ul>



<p><code>nginx -t</code></p>



<ul class="wp-block-list">
<li>For Ubuntu:</li>
</ul>



<p><code>service nginx restart</code></p>



<p><strong>Step 5: Verifying the Configuration</strong></p>



<p>Check your website and inspect the Nginx access and error logs (e.g., <code>/var/log/nginx/access.log</code>) to verify that the real visitor IPs are being logged correctly. This step is crucial to ensure that your configuration is working as expected.</p>



<p><strong>Optional: Handling Real IP in a Dockerized Environment</strong></p>



<p>If your web application runs inside a Docker container, you need to pass the real IP to the containerized application. Add the following line in your Nginx configuration, right after the <code>fastcgi_pass </code>directive:</p>



<p><code>fastcgi_param REMOTE_ADDR $http_x_real_ip;</code></p>



<p>This sets the <code>$_SERVER['REMOTE_ADDR']</code> variable to the original IP in PHP-based applications.</p>



<p><strong>Common Issues and Troubleshooting</strong></p>



<ol start="1" class="wp-block-list">
<li><strong>Incorrect IP in Logs: </strong>Double-check that the Cloudflare IP ranges are up-to-date and that the <code>real_ip_header </code>directive is correctly set.</li>



<li><strong>Nginx Configuration Errors: </strong>Use <code>nginx -t</code>to check for syntax errors in the configuration files before restarting Nginx.</li>



<li><strong>Docker Networking Issues:</strong> Ensure that Docker’s network mode allows for correct IP forwarding.</li>
</ol>



<p><strong>Conclusion</strong></p>



<p>By following the steps outlined in this article, you can ensure that your Nginx server, even when running inside Docker, correctly logs and uses the original visitor IP addresses provided by Cloudflare. This is essential for accurate logging, security configurations, and analytics. With proper configuration, you can leverage the benefits of Cloudflare’s protection without sacrificing the visibility of real user data.</p><p>The post <a href="https://rhein-ruhr-informatik.de/en/2025/01/29/echte-ip-ueber-cloudflare-erhaelt-nginx-reverse-proxy-docker/">Wie man eine echte IP über Cloudflare erhält &#8211; Nginx Reverse Proxy &#8211; Docker</a> appeared first on <a href="https://rhein-ruhr-informatik.de/en">Rhein-Ruhr-Informatik</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>