Debian Codegraph @

doc/sources/admin/configvhost.rst

raw
1 Manage virtual hosts
2 ====================
3
4 LemonLDAP::NG configuration is built around Apache or Nginx virtual
5 hosts. Each virtual host is a protected resource, with access rules,
6 headers, POST data and options.
7
8 Apache configuration
9 --------------------
10
11 To protect a virtual host in Apache, the LemonLDAP::NG Handler must be
12 activated (see
13 :ref:`Apache global configuration<configlocation-apache>`).
14
15 Then you can take any virtual host, and just add this line to protect it:
16
17 .. code-block:: apache
18
19 PerlHeaderParserHandler Lemonldap::NG::Handler::ApacheMP2
20
21 Hosted application
22 ~~~~~~~~~~~~~~~~~~
23
24 Example of a protected virtual host for a local application:
25
26 .. code-block:: apache
27
28 <VirtualHost *:80>
29 ServerName localsite.example.com
30
31 PerlHeaderParserHandler Lemonldap::NG::Handler::ApacheMP2
32
33 DocumentRoot /var/www/localsite
34
35 ErrorLog /var/log/apache2/localsite_error.log
36 CustomLog /var/log/apache2/localsite_access.log combined
37
38 </VirtualHost>
39
40 .. _vhostapachereverse:
41
42 Reverse-Proxy
43 ~~~~~~~~~~~~~
44
45 Example of a protected virtual host with LemonLDAP::NG as reverse proxy:
46
47 .. code-block:: apache
48
49 <VirtualHost *:80>
50 ServerName application.example.com
51
52 PerlHeaderParserHandler Lemonldap::NG::Handler::ApacheMP2
53
54 # Reverse-Proxy
55 ProxyPass / http://private-name/
56 # Change "Location" header in redirections
57 ProxyPassReverse / http://private-name/
58 # Change domain cookies
59 ProxyPassReverseCookieDomain private-name application.example.com
60
61 ErrorLog /var/log/apache2/proxysite_error.log
62 CustomLog /var/log/apache2/proxysite_access.log combined
63 </VirtualHost>
64
65 Same with remote server configured with the same host name:
66
67 .. code-block:: apache
68
69 <VirtualHost *:80>
70 ServerName application.example.com
71
72 PerlHeaderParserHandler Lemonldap::NG::Handler::ApacheMP2
73
74 # Reverse-Proxy
75 ProxyPass / http://APPLICATION_IP/
76
77 ProxyPreserveHost on
78
79 ErrorLog /var/log/apache2/proxysite_error.log
80 CustomLog /var/log/apache2/proxysite_access.log combined
81 </VirtualHost>
82
83
84 .. note::
85
86 The ``ProxyPreserveHost`` directive will forward the Host header
87 to the protected application. To learn more about using Apache as
88 reverse-proxy, see `Apache
89 documentation <http://httpd.apache.org/docs/current/mod/mod_proxy.html>`__.
90
91
92
93 .. tip::
94
95 Some applications need the ``REMOTE_USER`` environment
96 variable to get the connected user, which is not set in reverse-proxy
97 mode. In this case, see
98 :doc:`how convert header into environment variable<header_remote_user_conversion>`.
99
100 Add a floating menu
101 ~~~~~~~~~~~~~~~~~~~
102
103 A small floating menu can be added to application with this simple Apache configuration:
104
105 .. code-block:: apache
106
107 PerlModule Lemonldap::NG::Handler::ApacheMP2::Menu
108 PerlOutputFilterHandler Lemonldap::NG::Handler::ApacheMP2::Menu->run
109
110 Pages where this menu is displayed can be restricted, for example:
111
112 .. code-block:: apache
113
114 <Location /var/www/html/index.php>
115 PerlOutputFilterHandler Lemonldap::NG::Handler::ApacheMP2::Menu->run
116 </Location>
117
118
119 .. attention::
120
121 You need to disable mod_deflate to use the floating
122 menu
123
124 Nginx configuration
125 -------------------
126
127 .. attention::
128
129 When using Nginx Handler, LUA support is needed to send headers declared in global configuration.
130 As LUA support in Nginx was dropped in many distributions, either use OpenResty, either configure
131 manually headers in Nginx configuration.
132
133 LUA is not needed for Portal and Manager configuration.
134
135 To protect a virtual host in Nginx, the LemonLDAP::NG FastCGI server
136 must be launched (see
137 :doc:`LemonLDAP::NG FastCGI server<fastcgiserver>`).
138
139 Then you can take any virtual host and modify it:
140
141 - Declare the /lmauth endpoint
142
143 .. code-block:: nginx
144
145 location = /lmauth {
146 internal;
147 include /etc/nginx/fastcgi_params;
148 fastcgi_pass unix:/var/run/llng-fastcgi-server/llng-fastcgi.sock;
149
150 # Drop post data
151 fastcgi_pass_request_body off;
152 fastcgi_param CONTENT_LENGTH "";
153
154 # Prevent attacks on older Nginx versions (<1.29.5)
155 fastcgi_param HTTP_HOST $host;
156
157 # Keep original request (LLNG server will receive /lmauth)
158 fastcgi_param X_ORIGINAL_URI $original_uri;
159 }
160
161 - Protect the application (/ or /path/to/protect):
162
163 .. code-block:: nginx
164
165 location /path/to/protect {
166 auth_request /lmauth;
167 set $original_uri $uri$is_args$args;
168 auth_request_set $lmremote_user $upstream_http_lm_remote_user;
169 auth_request_set $lmlocation $upstream_http_location;
170 auth_request_set $cookie_value $upstream_http_set_cookie;
171 add_header Set-Cookie $cookie_value;
172 error_page 401 $lmlocation;
173 try_files $uri $uri/ =404;
174
175 # ...
176 }
177
178 - Use LUA or set manually the headers:
179
180 .. code-block:: nginx
181
182 location /path/to/protect {
183
184 # ...
185
186 # IF LUA IS SUPPORTED
187 #include /etc/lemonldap-ng/nginx-lua-headers.conf;
188
189 # ELSE
190 # Set manually your headers
191 #auth_request_set $authuser $upstream_http_auth_user;
192 #proxy_set_header Auth-User $authuser;
193 # OR
194 #fastcgi_param HTTP_AUTH_USER $authuser;
195
196 # Then (if LUA not supported), change cookie header to hide LLNG cookie
197 #auth_request_set $lmcookie $upstream_http_cookie;
198 #proxy_set_header Cookie: $lmcookie;
199 # OR in the corresponding block
200 #fastcgi_param HTTP_COOKIE $lmcookie;
201
202 # Set REMOTE_USER (for FastCGI apps only)
203 #fastcgi_param REMOTE_USER $lmremote_user;
204 }
205
206 .. _hosted-application-1:
207
208 Hosted application
209 ~~~~~~~~~~~~~~~~~~
210
211 Example of a protected virtual host for a local application:
212
213 .. code-block:: nginx
214
215 # Log format
216 include /path/to/lemonldap-ng/nginx-lmlog.conf;
217 server {
218 listen 80;
219 server_name myserver;
220 root /var/www/html;
221 # Internal authentication request
222 location = /lmauth {
223 internal;
224 include /etc/nginx/fastcgi_params;
225 fastcgi_pass unix:/path/to/llng-fastcgi-server.sock;
226 # Drop post data
227 fastcgi_pass_request_body off;
228 fastcgi_param CONTENT_LENGTH "";
229 # Prevent attacks on older Nginx versions (<1.29.5)
230 fastcgi_param HTTP_HOST $host;
231 # Keep original request (LLNG server will receive /lmauth)
232 fastcgi_param X_ORIGINAL_URI $original_uri;
233 }
234
235 # Client requests
236 location ~ \.php$ {
237 auth_request /lmauth;
238 set $original_uri $uri$is_args$args;
239 auth_request_set $lmremote_user $upstream_http_lm_remote_user;
240 auth_request_set $lmlocation $upstream_http_location;
241 error_page 401 $lmlocation;
242 try_files $uri $uri/ =404;
243 include fastcgi_params;
244 try_files $fastcgi_script_name =404;
245 fastcgi_pass unix:/path/to/php-fpm/socket;
246 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
247 fastcgi_intercept_errors on;
248 fastcgi_split_path_info ^(.+\.php)(/.+)$;
249 fastcgi_hide_header X-Powered-By;
250
251 ##################################
252 # PASSING HEADERS TO APPLICATION #
253 ##################################
254 # IF LUA IS SUPPORTED
255 #include /path/to/nginx-lua-headers.conf
256
257 # ELSE
258 # Set manually your headers
259 #auth_request_set $authuser $upstream_http_auth_user;
260 #fastcgi_param HTTP_AUTH_USER $authuser;
261 }
262 location / {
263 try_files $uri $uri/ =404;
264 }
265 }
266
267 .. _reverse-proxy-1:
268
269 Reverse-Proxy
270 ~~~~~~~~~~~~~
271
272 - Example of a protected reverse-proxy:
273
274 .. code-block:: nginx
275
276 # Log format
277 include /path/to/lemonldap-ng/nginx-lmlog.conf;
278 server {
279 listen 80;
280 server_name myserver;
281 root /var/www/html;
282 # Internal authentication request
283 location = /lmauth {
284 internal;
285 include /etc/nginx/fastcgi_params;
286 fastcgi_pass unix:/path/to/llng-fastcgi-server.sock;
287 # Drop post data
288 fastcgi_pass_request_body off;
289 fastcgi_param CONTENT_LENGTH "";
290 # Prevent attacks on older Nginx versions (<1.29.5)
291 fastcgi_param HTTP_HOST $host;
292 # Keep original request (LLNG server will receive /lmauth)
293 fastcgi_param X_ORIGINAL_URI $original_uri;
294 }
295
296 # Client requests
297 location / {
298 auth_request /lmauth;
299 set $original_uri $uri$is_args$args;
300 auth_request_set $lmremote_user $upstream_http_lm_remote_user;
301 auth_request_set $lmlocation $upstream_http_location;
302 error_page 401 $lmlocation;
303
304 proxy_pass http://remote.server/;
305 include /etc/nginx/proxy_params;
306
307 ##################################
308 # PASSING HEADERS TO APPLICATION #
309 ##################################
310 # IF LUA IS SUPPORTED
311 #include /path/to/nginx-lua-headers.conf;
312
313 # ELSE
314 # Set manually your headers
315 #auth_request_set $authuser $upstream_http_auth_user;
316 #proxy_set_header HTTP_AUTH_USER $authuser;
317 }
318 }
319
320 If /etc/nginx/proxy_params file does not exist, you can create it with this content:
321
322 .. code-block:: nginx
323
324 proxy_set_header Host $host;
325 proxy_set_header X-Real-IP $remote_addr;
326 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
327 proxy_set_header X-Forwarded-Proto $scheme;
328
329 - Example of a Nginx Virtual Host using uWSGI with many URIs protected by different types of handler:
330
331 .. code-block:: nginx
332
333 # Log format
334 include /path/to/lemonldap-ng/nginx-lmlog.conf;
335 server {
336 listen 80;
337 server_name myserver;
338 root /var/www/html;
339
340 # Internal MAIN handler authentication request
341 location = /lmauth {
342 internal;
343 # uWSGI Configuration
344 include /etc/nginx/uwsgi_params;
345 uwsgi_pass 127.0.0.1:5000;
346 uwsgi_pass_request_body off;
347 uwsgi_param CONTENT_LENGTH "";
348 # Do not set this line if your /etc/nginx/uwsgi_params contains already this line
349 uwsgi_param HTTP_HOST $host;
350 uwsgi_param X_ORIGINAL_URI $original_uri;
351 # Improve performances
352 uwsgi_buffer_size 32k;
353 uwsgi_buffers 32 32k;
354 }
355
356 # Internal AUTH_BASIC handler authentication request
357 location = /lmauth-basic {
358 internal;
359 # uWSGI Configuration
360 include /etc/nginx/uwsgi_params;
361 uwsgi_pass 127.0.0.1:5000;
362 uwsgi_pass_request_body off;
363 uwsgi_param CONTENT_LENGTH "";
364 # Do not set this line if your /etc/nginx/uwsgi_params contains already this line
365 uwsgi_param HTTP_HOST $host;
366 uwsgi_param X_ORIGINAL_URI $original_uri;
367 uwsgi_param VHOSTTYPE AuthBasic;
368 # Improve performances
369 uwsgi_buffer_size 32k;
370 uwsgi_buffers 32 32k;
371 }
372
373 # Internal SERVICE_TOKEN handler authentication request
374 location = /lmauth-service {
375 internal;
376 # uWSGI Configuration
377 include /etc/nginx/uwsgi_params;
378 uwsgi_pass 127.0.0.1:5000;
379 uwsgi_pass_request_body off;
380 uwsgi_param CONTENT_LENGTH "";
381 # Do not set this line if your /etc/nginx/uwsgi_params contains already this line
382 uwsgi_param HTTP_HOST $host;
383 uwsgi_param X_ORIGINAL_URI $original_uri;
384 uwsgi_param VHOSTTYPE ServiceToken;
385 # Improve performances
386 uwsgi_buffer_size 32k;
387 uwsgi_buffers 32 32k;
388 }
389
390 # Client requests
391 location / {
392 ##################################
393 # CALLING AUTHENTICATION #
394 ##################################
395 auth_request /lmauth;
396 set $original_uri $uri$is_args$args;
397 auth_request_set $lmremote_user $upstream_http_lm_remote_user;
398 auth_request_set $lmremote_custom $upstream_http_lm_remote_custom;
399 auth_request_set $lmlocation $upstream_http_location;
400 # Remove this for AuthBasic handler
401 error_page 401 $lmlocation;
402
403 ##################################
404 # PASSING HEADERS TO APPLICATION #
405 ##################################
406 # IF LUA IS SUPPORTED
407 include /etc/nginx/nginx-lua-headers.conf;
408 }
409
410 location /AuthBasic/ {
411 ##################################
412 # CALLING AUTHENTICATION #
413 ##################################
414 auth_request /lmauth-basic;
415 set $original_uri $uri$is_args$args;
416 auth_request_set $lmremote_user $upstream_http_lm_remote_user;
417 auth_request_set $lmremote_custom $upstream_http_lm_remote_custom;
418 auth_request_set $lmlocation $upstream_http_location;
419 # Remove this for AuthBasic handler
420 #error_page 401 $lmlocation;
421
422 ##################################
423 # PASSING HEADERS TO APPLICATION #
424 ##################################
425 # IF LUA IS SUPPORTED
426 include /etc/nginx/nginx-lua-headers.conf;
427 }
428
429 location /web-service/ {
430 ##################################
431 # CALLING AUTHENTICATION #
432 ##################################
433 auth_request /lmauth-service;
434 set $original_uri $uri$is_args$args;
435 auth_request_set $lmremote_user $upstream_http_lm_remote_user;
436 auth_request_set $lmlocation $upstream_http_location;
437 # Remove this for AuthBasic handler
438 error_page 401 $lmlocation;
439
440 ##################################
441 # PASSING HEADERS TO APPLICATION #
442 ##################################
443 # IF LUA IS SUPPORTED
444 include /etc/nginx/nginx-lua-headers.conf;
445 }
446 }
447
448 .. _configvhost-lemonldapng-configuration:
449
450 LemonLDAP::NG configuration
451 ---------------------------
452
453 A virtual host protected by LemonLDAP::NG Handler must be registered in
454 LemonLDAP::NG configuration.
455
456 To do this, use the Manager, and go in ``Virtual Hosts`` branch. You can
457 add, delete or modify a virtual host here. Enter the exact virtual host
458 name (for example ``test.example.com``) or use a wildcard (for example
459 ``*.example.com``).
460
461 A virtual host contains:
462
463 - Access rules: check user's right on URL patterns
464 - HTTP headers: forge information sent to protected applications
465 - POST data: use form replay
466 - Options: redirection port, protocol, Handler type, aliases,required authentication level,...
467
468 Wildcards in hostnames
469 ~~~~~~~~~~~~~~~~~~~~~~
470
471 A wildcard can be used in virtualhost name (not in aliases !):
472 ``*.example.com`` matches all hostnames that belong to ``example.com`` domain.
473
474 .. versionchanged:: 2.0.9
475 You can now use wildcards of the form ``test-*.example.com`` or
476 ``test-%.example.com``. The ``%`` wilcard doesn't match subdomains.
477
478 Even if a wildcard exists, if a VirtualHost is explicitly declared, this
479 rule will be applied. Example with precedence order for test.sub.example.com:
480
481 #. test.sub.example.com
482 #. test%.sub.example.com
483 #. test*.sub.example.com
484 #. %.sub.example.com
485 #. \*.sub.example.com
486 #. \*.example.com (``%.example.com`` does not match
487 test.sub.example.com)
488
489 Access rules and HTTP headers
490 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
491
492 See :doc:`Writing rules and headers<writingrulesand_headers>` to
493 learn how to configure access control and HTTP headers sent to
494 application by LL::NG.
495
496
497 .. attention::
498
499 With **Nginx**-based Reverse-Proxy, header directives can
500 be appended by a LUA script.
501
502 To send more than **15** headers to protected applications,
503 you have to edit and modify :
504
505 ``/etc/nginx/nginx-lua-headers.conf``
506
507
508 .. danger::
509
510 \* **Nginx** gets rid of any empty headers. There is no
511 point of passing along empty values to another server; it would only
512 serve to bloat the request. In other words, headers with **empty values
513 are completely removed** from the passed request.
514
515 \* **Nginx**, by default, will consider any header that **contains
516 underscores as invalid**. It will remove these from the proxied request.
517 If you wish to have Nginx interpret these as valid, you can set the
518 ``underscores_in_headers`` directive to “on”, otherwise your headers
519 will never make it to the backend server.
520
521 POST data
522 ~~~~~~~~~
523
524 See :doc:`Form replay<formreplay>` to learn how to configure form
525 replay to POST data on protected applications.
526
527 Options
528 ~~~~~~~
529
530 Some options are available:
531
532 - **Port**: used to build redirection URL *(when user is not logged, or for
533 CDA requests)*, -1 means the handler builds the URL with the incoming port, as seen by the webserver
534 - **HTTPS**: used to build redirection URL
535 - **Maintenance mode**: reject all requests with a maintenance message
536 - **Aliases**: list of aliases for this virtual host *(avoid to rewrite
537 rules,...)*
538 - **Access to trace**: can be used for overwriting REMOTE_CUSTOM with a custom function.
539 Provide a comma separated parameters list with custom function path and args.
540 Args can be vars or session attributes, macros, ...
541 By example: My::accessToTrace, Doctor, Who, _whatToTrace
542 - **Required authentication level**: this option avoids to reject user with
543 a rule based on ``$authenticationLevel``. When user has not got the
544 required level, he is redirected to an upgrade page in the portal.
545 This default level is required for ALL locations relative to this virtual host.
546 It can be overrided for each locations.
547 - **Type**: handler type (:ref:`Main<presentation-kinematics>`,
548 :doc:`AuthBasic<authbasichandler>`,
549 :doc:`ServiceToken<servertoserver>`,
550 :doc:`DevOps<devopshandler>`,
551 :doc:`DevOpsST<devopssthandler>`,
552 :doc:`DevOpsCDA<devopscdahandler>`,
553 :doc:`OAuth2<oauth2handler>`,...)
554 - **DevOps rules file URL**: option to define URL to retrieve DevOps rules file.
555 This option can be overridden with ``uwsgi_param/fastcgi_param RULES_URL`` parameter.
556 - **ServiceToken timeout**: by default, ServiceToken is just valid during 30
557 seconds. This TTL can be customized for each virtual host.
558 - **Comment**: can be used for setting comment.
559
560
561 .. attention::
562
563 A hash reference containing $req, $session, $vhost, $custom and an array reference
564 with provided parameters is passed to the custom function.
565
566 ::
567
568 package My;
569
570 sub accessToTrace {
571 my $hash = shift;
572 my $custom = $hash->{custom};
573 my $req = $hash->{req};
574 my $vhost = $hash->{vhost};
575 my $custom = $hash->{custom};
576 my $params = $hash->{params};
577 my $session = $hash->{session};
578
579 return "$custom alias $params->[0]_$params->[1]:$session->{groups}:$session->{$params->[2]}";
580 }
581
582 1;
583
584
585 .. danger::
586
587 A same virtual host can serve many locations. Each
588 location can be protected by a different type of handler :
589
590 ::
591
592 server test1.example.com 80
593 location ^/AuthBasic => AuthBasic handler
594 location ^/AuthCookie => Main handler
595
596 Keep in mind that AuthBasic handler use "Login/Password" to authenticate
597 users. If you set "Authentication level required" option to "5" by
598 example, AuthBasic requests will be ALWAYS rejected because AuthBasic
599 authentication level is lower than required level.
600
601
602 .. attention::
603
604 A negative or null ServiceToken timeout value will be
605 overloaded by ``handlerServiceTokenTTL`` (30 seconds by default).
606
607
608 "Port" and "HTTPS" options are used to build redirection URL *(when user
609 is not logged, or for CDA requests)*. By default, default values are
610 used. These options are for overriding default values.
611
612 .. |image0| image:: /documentation/new.png
613 :width: 35px