Answer the question
In order to leave comments, you need to log in
Apache Nginx Cloudflare does not work .htaccess as it should by IP, what should I do?
Good time!
Task: allow some subnets to access the site, do it through modrewrite, and deny the rest.
Given: Server, it has a VestaCP panel, a domain on Cloudflare and an A record on the server, the domain works through Nginx> Apache, everything is OK, the site is working. So far, it is not possible to correctly configure blocking / allowing certain IPs.
IP rules in htaccess do not work at all, any of the RewriteCond was tested one by one. I also used this guide from Cloud where he writes IP https://support.cloudflare.com/hc/en-us/articles/2... ( CF-Connecting-IP + X-Real-IP )
1. Why not works and what's wrong?
2. What is the order in which headers are passed? Why does getallheaders() see one thing but $_SERVER shows another?
The code:
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} ^XXX.YY.*
RewriteCond %{HTTP_X_REAL_IP} ^XXX.YY.*
RewriteCond %{CF-Connecting-IP} ^XXX.YY.*
RewriteCond %{HTTP_CF_CONNECTING_IP} ^XXX.YY.*
RewriteCond %{X-Real-IP} ^XXX.YY.*
RewriteRule (.*) - [F]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.*) print_headers.php [L]
</IfModule>
<?php
echo '<pre>', var_dump(getallheaders()). '</pre>';
echo '<pre>', print_r($_SERVER, 1), '</pre>';
?>
Answer the question
In order to leave comments, you need to log in
RewriteCond %{CF-Connecting-IP} ^XXX.YY.*
RewriteCond %{X-Real-IP} ^XXX.YY.*
RewriteCond %{HTTP:CF-Connecting-IP} !^XXX\.YY\.
RewriteCond %{HTTP:X-Real-IP} !^XXX\.YY\.
RewriteEngine On
RewriteCond %{HTTP:X-Real-IP} !^XXX\.YY\.
RewriteRule (.*) - [F]
Here's another option. (works for me on domains that are through the cloud).
#if the domain is via cloudflare, the rules for banning access to the site for everyone except your own ip
Order Deny,Allow
SetEnvIf X-Forwarded-For ^100\.50\.30\.61 AllowIP
SetEnvIf X-Forwarded-For ^90\.100\.100\.62 AllowIP
SetEnvIf X-Forwarded-For ^100\.50\.30\.63 AllowIP
Deny from all
Allow from env=AllowIP
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question