I would like to allow only one country access, but exclude proxies within this country.
This is what I have (shortened version for convenience)
<Limit GET POST>
order deny,allow
deny from all
allow from 139.82.0.0/16
allow from 143.54.0.0/16
allow from 186.192.0.0/11
allow from 186.224.0.0/11
.
deny from 186.201.27.66
deny from 186.201.196.1
deny from 186.214.51.231
deny from 186.237.225.26
</Limit>
But I know this wont work. How do I go about doing this?
Update : for the new apache 2.4 jump directly to the end.
The Order keyword and its relation with
DenyandAllowDirectives is a real nightmare. It would be quite interesting to understand how we ended up with such solution, a non-intuitive one to say the least.Orderkeyword will have a big impact on howAllowandDenydirectives are used.DenyandAllowdirectives are not applied in the order they are written, they must be seen as two distinct blocks (one the forDenydirectives, one forAllow).You have two main modes:
The Order-Deny-Allow-mode, or Allow-anyone-except-this-list-or-maybe-not
Denyrules.Denyrules reject some requests.Allow.I would rephrase it as:
The Order-Allow-Deny-mode, or Reject-everyone-except-this-list-or-maybe-not
Allowrules.Allowrule.Allow, you can still reject them with aDeny.In the simplified form:
Back to your case
You need to allow a list of networks which are the country networks. And in this country you want to exclude some proxies’ IP addresses.
You have taken the allow-anyone-except-this-list-or-maybe-not mode, so by default anyone can access your server, except proxies’ IPs listed in the
Denylist, but if they get rejected you still allow the country networks. That’s too broad. Not good.By inverting to
order allow,denyyou will be in the reject-everyone-except-this-list-or-maybe-not mode.So you will reject access to everyone but allow the country networks and then you will reject the proxies. And of course you must remove the
Deny from allas stated by @Gerben and @Michael Slade (this answer only explains what they wrote).The
Deny from allis usually seen withorder deny,allowto remove the allow by default access and make a simple, readable configuration. For example, specify a list of allowed IPs after that. You don’t need that rule and your question is a perfect case of a 3-way access mode (default policy, exceptions, exceptions to exceptions).But the guys who designed these settings are certainly insane.
All this is deprecated with Apache 2.4
The whole authorization scheme has been refactored in Apache 2.4 with RequireAll, RequireAny and RequireNone directives. See for example this complex logic example.
So the old strange
Orderlogic becomes a relic, and to quote the new documentation: