I use .htaccess to only allow traffic from Cloudflare to my Cloudflare enabled domains.
It works, yay.
But now my logs are showing the proxied IP.
Un-yay.
We thought the mod_remoteip mod would help. But it literally changes how centos see’s the ip, and now access to my sites, with this .htaccess mod is blocked. Because now the ips are unhidden, and .htaccess says ONLY allow Cloudflare ip’s.
These sites are getting ddosed, through Cloudflare, and I can’t see their IP, to try and block them. I only see the Cloudflare ip.
This sounds like you did not update your LogFormat Directive to use the Client IP address of the request and are still logging the Remote hostname instead. When HostnameLookups is set to Off, which is the default, Remote hostname logs the IP address, which will be the Cloudflare proxy IP.
But I don’t think that changes the fact that this mod is changing how apache interprets the IP entirely. Or am I mistaken? Where as I don’t want apache to ONLY see the origin IP, I ONLY want apache to just log the origin ip. But this mod is not making that possible from what I can tell.
That’s not been my experience. I have access restrictions that allow HTTP basic authentication to be bypassed from specific locations, defined by IP addresses, on sites that are behind Cloudflare. I have mod_remoteip configured to recognize Clouflare IPs as the trusted proxy addresses. The apache server correctly distinguishes the client location and only prompts for authentication when accessed from outside of the trusted IPs. When I view apache access logs on my proxied sites, I see the visitor’s actual IP.
It doesn’t just work on its own. It requires changes to your server logging directives in addition to configuring mod_remoteip. My previous reply included links to the relevant documentation so you can check all of your sever configs to make sure all of the required changes have been completed. Verifying those settings is where I would start if I was trying to solve this on a client’s system.
For anyone having this issue, I found out you can update the apache logging format, or use this custom php script to see the origin IP, without having to use the mod_remoteip mod.
I created this PHP script to log the origin IP, and request URL to a non public log. For use within wordpress, or extract the code for general php use.
This logs get and post requests.
Put this in your function.php within your child theme
Turn on “Authenticated Origin Pulls” for all your domains in the dashboard (safe to turn on even if you haven’t set it up server-side yet), in SSL/TLS → Origin Server
Download Cloudflare’s authenticated_origin_pull_ca.pem and put it on your server
Add to global Apache config: SSLCACertificateFile /etc/apache2/authenticated_origin_pull_ca.pem
Add to all your Cloudflare-only vhosts: SSLVerifyClient require
I use iptables to restrict port 80/443 connections to Cloudflare IPs on hosts that only serve content through the Cloudflare proxy. I do like the AOP strategy, though, especially since you can configure it in a vhost directive instead of globally if you have some vhosts on the server that are not using Cloudflare.
Requiring a client certificate will shutdown non-Cloudflare connections immediately during the SSL handshake phase, with no need to read your htaccess file from disk. htaccess is notorious for increasing disk activity and thus lowering server performance. Blocking through htaccess is still letting the traffic in and answering it, just answering it with an error page instead of the requested content, so it doesn’t necessarily reduce load on your server. You also won’t have to worry about blocking legitimate traffic every time Cloudflare adds a new IP range.
Possible cons? Since the bad connections attempts are killed in OpenSSL before reaching “proper” Apache, they don’t show up in Apache logs. This can be good or bad, since logging does consume resources. It might be possible to log them somehow but I haven’t bothered trying.
Even in the vhost config, you can still use the same path to a single certificate file. One important thing to take into consideration is that @user4358 and I are both likely operating under the assumption that you control the entire server and are not in a shared hosting scenario.
If you all sites on the server behind Cloudflare, updating the global config is definitely the superior choice.