Skip to content

Commit 1d79724

Browse files
authored
Update apache.md
1 parent f18fdcc commit 1d79724

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

best_practices/webserver/apache.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@ order: 100
44

55
# Apache
66

7-
==- <span style="color:#5283ff; font-size:24px;">**Grant or deny access to server for specific IP's while using basic auth**</span>
7+
## Grant or deny access to server for specific IP's while using basic auth
88

9-
In this article, we'll tackle the problem how we can decide if a visitor should or should not login on a server with basic auht enabled, based on it's IP-adress.
9+
In this article, we'll tackle the problem how we can decide if a visitor should or should not login on a server with basic auth enabled, based on it's IP-address.
1010

1111
So what is the result we want to achieve? We want to implement an .htpasswd so visitors need to have a valid login, except when the request came from a whitelisted IP adress.
1212
In that case, no login is asked and you'll be redirected to the site. Like a VIP that would skip a waiting queue for a club.
1313

14-
##### Method 2: Server with varnish enabled
14+
This method is used for Apache2
1515

16+
#### Method 1: Server without varnish enabled
17+
There is a difference when a server has or doesn't have Varnish enabled.
18+
For now we'll make it simple assuming there is no interruption with any service like Varnish. In that case we'll use the next setup:
19+
20+
_For best practice, we put this code at the top of our .htaccess file_
21+
22+
```AuthType Basic AuthName "Restricted Content" AuthUserFile /var/www/user/apache2/.htpasswd # Required IP's will be granted access without login require ip # Only a person with valid credentials will be redirected require valid-user```
23+
24+
#### Method 2: Server with varnish enabled
1625
For a server with varnish enabled, is a different approach needed. All requests that go through varnish will pass the header (X-Forwarded-For), but it may contain some tempered information about the visitors IP.
1726
Because of this modification, the request for immediate access will be denied and the visitor will be asked to login. To make sure this won't happen, we'll add a variable for the header that contains the whitelisted IP-adress.
1827
The code below will do the trick:
@@ -33,10 +42,8 @@ SetEnvIF X-Forwarded-For AllowIP
3342
# Include the env variable
3443
Require env AllowIP
3544
```
36-
===
37-
3845

39-
==- <span style="color:#5283ff; font-size:24px;">**Block infamous bytespider bot**</span>
46+
## Block infamous bytespider bot**</span>
4047

4148
Sometimes a server can go high in load due to the infamous bytespider bot. This one can be excluded by implementing this piece of code inside the .htaccess:
4249

@@ -47,4 +54,3 @@ Sometimes a server can go high in load due to the infamous bytespider bot. This
4754
RewriteRule .* - [F]
4855
</IfModule>
4956
```
50-
===

0 commit comments

Comments
 (0)