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
nginx does not offer HTTP authentication using LDAP (or any other type of databases) as backend out of the box. Fortunately nginx features a module named ngx_http_auth_request_module which enables client authorization based on the result of an HTTP/HTTPS subrequest. Using this module in combination with the nginx HTTP proxy module it is possible to authenticate against any web service returning either an HTTP 200 code on authentication success or HTTP 401 code on authentication failure.
4
+
5
+
This simple Ruby script implements a WEBrick HTTPS servlet listening by default on port 8888 in order to authenticate against an LDAP server using STARTTLS and thus enabling you to provide LDAP authentication for your nginx website.
6
+
7
+
## Installation
8
+
9
+
You will need Ruby and the bundler gem in order to install and run this script. Read below for the installation instructions.
10
+
11
+
### Install bunlder
12
+
13
+
$ gem install bundler
14
+
15
+
### Install dependencies
16
+
Currently the only required gem is net-ldap.
17
+
18
+
$ bundle
19
+
20
+
### Configure the script
21
+
22
+
Copy the sample `config.sample.yaml` file as `config.yaml` and adapt it for your LDAP environment.
23
+
24
+
### Configure nginx
25
+
26
+
1. Add to your nginx http configuration (e.g. `/etc/nginx/conf.d/auth_cache.conf`):
27
+
28
+
```
29
+
proxy_cache_path cache/ keys_zone=auth_cache:5m;
30
+
```
31
+
32
+
The credentials are cached for 5 minutes, feel free to increase or decrease. If you change this parameter do not forget to also adapt `proxy_cache_valid` under point 2. below.
33
+
34
+
2. Add to your nginx server configuration (e.g. `/etc/nginx/conf.d/mywebsite.ch`):
35
+
36
+
```
37
+
satisfy any;
38
+
auth_basic "Ruby LDAP authentication servlet";
39
+
auth_basic_user_file "/etc/nginx/empty.htpasswd";
40
+
auth_request /auth;
41
+
42
+
location = /auth {
43
+
proxy_pass https://localhost:8888;
44
+
proxy_cache auth_cache;
45
+
proxy_cache_valid 200 5m;
46
+
proxy_pass_request_body off;
47
+
proxy_set_header Content-Length "";
48
+
proxy_set_header X-Original-URI $request_uri;
49
+
}
50
+
```
51
+
52
+
This will protect the whole website. It is also possible to protect parts of it by including the first block in an nginx specific location such as `/private`:
53
+
54
+
```
55
+
location /private {
56
+
satisfy any;
57
+
auth_basic "Ruby LDAP authentication servlet";
58
+
auth_basic_user_file "/etc/nginx/empty.htpasswd";
59
+
auth_request /auth;
60
+
}
61
+
```
62
+
63
+
3. Create an empty htpasswd file
64
+
65
+
This is required and I did not find any way around it.
66
+
67
+
$ touch /etc/nginx/empty.htpasswd
68
+
69
+
4. Reload nginx
70
+
71
+
$ systemctl reload nginx
72
+
73
+
### Start the script
74
+
75
+
$ ./ldap-auth-servlet.rb
76
+
77
+
Once you have tested that everything works well it is recommended to run the script in background in daemon mode by changing the `daemonize` parameter in the `config.yaml` file to `true`.
78
+
79
+
## Tested with
80
+
81
+
This script has been tested with the following setup:
# Purpose: This simple Ruby script implements a WEBrick HTTPS servlet listening by default on port 8888 in order to authenticate against an LDAP server using STARTTLS and thus enabling you to provide LDAP authentication for your nginx website.
6
+
7
+
require'webrick'
8
+
require'webrick/https'
9
+
require'base64'
10
+
require'net/ldap'
11
+
require'yaml'
12
+
13
+
# Path to config file and default configuration parameters
0 commit comments