You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: best_practices/webserver/apache.md
+13-7Lines changed: 13 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -4,15 +4,24 @@ order: 100
4
4
5
5
# Apache
6
6
7
-
==- <spanstyle="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
8
8
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.
10
10
11
11
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.
12
12
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.
13
13
14
-
##### Method 2: Server with varnish enabled
14
+
This method is used for Apache2
15
15
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
16
25
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.
17
26
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.
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:
42
49
@@ -47,4 +54,3 @@ Sometimes a server can go high in load due to the infamous bytespider bot. This
0 commit comments