services: nginx: Virtual hosts are first-class configuration items.
* gnu/services/web.scm (<nginx-vhost-configuration>): New record type. (config-domain-strings): New procedure. (config-index-strings): New procedure. (default-nginx-vhost-config): New procedure. (default-nginx-config): Add vhost support and temporary directories (nginx-activation): Create temporary directories (nginx-service): Add vhost-list key. * doc/guix.texi (Web Services): Document 'nginx-vhost-configuration'. Signed-off-by: Ludovic Courtès <ludo@gnu.org>
This commit is contained in:
		
							parent
							
								
									405be8dceb
								
							
						
					
					
						commit
						8c00b83818
					
				
					 2 changed files with 137 additions and 5 deletions
				
			
		| 
						 | 
					@ -27,7 +27,8 @@ Copyright @copyright{} 2016 Chris Marusich@*
 | 
				
			||||||
Copyright @copyright{} 2016 Efraim Flashner@*
 | 
					Copyright @copyright{} 2016 Efraim Flashner@*
 | 
				
			||||||
Copyright @copyright{} 2016 John Darrington@*
 | 
					Copyright @copyright{} 2016 John Darrington@*
 | 
				
			||||||
Copyright @copyright{} 2016 ng0@*
 | 
					Copyright @copyright{} 2016 ng0@*
 | 
				
			||||||
Copyright @copyright{} 2016 Jan Nieuwenhuizen
 | 
					Copyright @copyright{} 2016 Jan Nieuwenhuizen@*
 | 
				
			||||||
 | 
					Copyright @copyright{} 2016 Julien Lepiller
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Permission is granted to copy, distribute and/or modify this document
 | 
					Permission is granted to copy, distribute and/or modify this document
 | 
				
			||||||
under the terms of the GNU Free Documentation License, Version 1.3 or
 | 
					under the terms of the GNU Free Documentation License, Version 1.3 or
 | 
				
			||||||
| 
						 | 
					@ -10397,6 +10398,7 @@ The @code{(gnu services web)} module provides the following service:
 | 
				
			||||||
@deffn {Scheme Procedure} nginx-service [#:nginx nginx] @
 | 
					@deffn {Scheme Procedure} nginx-service [#:nginx nginx] @
 | 
				
			||||||
       [#:log-directory ``/var/log/nginx''] @
 | 
					       [#:log-directory ``/var/log/nginx''] @
 | 
				
			||||||
       [#:run-directory ``/var/run/nginx''] @
 | 
					       [#:run-directory ``/var/run/nginx''] @
 | 
				
			||||||
 | 
					       [#:vhost-list (list (nginx-vhost-configuration))] @
 | 
				
			||||||
       [#:config-file]
 | 
					       [#:config-file]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Return a service that runs @var{nginx}, the nginx web server.
 | 
					Return a service that runs @var{nginx}, the nginx web server.
 | 
				
			||||||
| 
						 | 
					@ -10407,8 +10409,54 @@ files are written to @var{run-directory}.  For proper operation, these
 | 
				
			||||||
arguments should match what is in @var{config-file} to ensure that the
 | 
					arguments should match what is in @var{config-file} to ensure that the
 | 
				
			||||||
directories are created when the service is activated.
 | 
					directories are created when the service is activated.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					As an alternative to using a @var{config-file}, @var{vhost-list} can be
 | 
				
			||||||
 | 
					used to specify the list of @dfn{virtual hosts} required on the host.  For
 | 
				
			||||||
 | 
					this to work, use the default value for @var{config-file}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@end deffn
 | 
					@end deffn
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@deftp {Data Type} nginx-vhost-configuration
 | 
				
			||||||
 | 
					Data type representing the configuration of an nginx virtual host.
 | 
				
			||||||
 | 
					This type has the following parameters:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@table @asis
 | 
				
			||||||
 | 
					@item @code{http-port} (default: @code{80})
 | 
				
			||||||
 | 
					Nginx will listen for HTTP connection on this port.  Set it at @code{#f} if
 | 
				
			||||||
 | 
					nginx should not listen for HTTP (non secure) connection for this
 | 
				
			||||||
 | 
					@dfn{virtual host}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@item @code{https-port} (default: @code{443})
 | 
				
			||||||
 | 
					Nginx will listen for HTTPS connection on this port.  Set it at @code{#f} if
 | 
				
			||||||
 | 
					nginx should not listen for HTTPS (secure) connection for this @dfn{virtual host}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Note that nginx can listen for HTTP and HTTPS connections in the same
 | 
				
			||||||
 | 
					@dfn{virtual host}.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@item @code{server-name} (default: @code{(list 'default)})
 | 
				
			||||||
 | 
					A list of server names this vhost represents. @code{'default} represents the
 | 
				
			||||||
 | 
					default vhost for connections matching no other vhost.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@item @code{root} (default: @code{"/srv/http"})
 | 
				
			||||||
 | 
					Root of the website nginx will serve.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@item @code{index} (default: @code{(list "index.html")})
 | 
				
			||||||
 | 
					Index files to look for when clients ask for a directory.  If it cannot be found,
 | 
				
			||||||
 | 
					Nginx will send the list of files in the directory.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@item @code{ssl-certificate} (default: @code{"/etc/nginx/cert.pem"})
 | 
				
			||||||
 | 
					Where to find the certificate for secure connections.  Set it to @code{#f} if
 | 
				
			||||||
 | 
					you don't have a certificate or you don't want to use HTTPS.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@item @code{ssl-certificate-key} (default: @code{"/etc/nginx/key.pem"})
 | 
				
			||||||
 | 
					Where to find the private key for secure connections.  Set it to @code{#f} if
 | 
				
			||||||
 | 
					you don't have a key or you don't want to use HTTPS.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@item @code{server-tokens?} (default: @code{#f})
 | 
				
			||||||
 | 
					Whether the server should add its configuration to response.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@end table
 | 
				
			||||||
 | 
					@end deftp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@node Network File System
 | 
					@node Network File System
 | 
				
			||||||
@subsubsection Network File System
 | 
					@subsubsection Network File System
 | 
				
			||||||
@cindex NFS
 | 
					@cindex NFS
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,7 @@
 | 
				
			||||||
;;; Copyright © 2015 David Thompson <davet@gnu.org>
 | 
					;;; Copyright © 2015 David Thompson <davet@gnu.org>
 | 
				
			||||||
;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
 | 
					;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org>
 | 
				
			||||||
;;; Copyright © 2016 ng0 <ng0@we.make.ritual.n0.is>
 | 
					;;; Copyright © 2016 ng0 <ng0@we.make.ritual.n0.is>
 | 
				
			||||||
 | 
					;;; Copyright © 2016 Julien Lepiller <julien@lepiller.eu>
 | 
				
			||||||
;;;
 | 
					;;;
 | 
				
			||||||
;;; This file is part of GNU Guix.
 | 
					;;; This file is part of GNU Guix.
 | 
				
			||||||
;;;
 | 
					;;;
 | 
				
			||||||
| 
						 | 
					@ -29,6 +30,8 @@
 | 
				
			||||||
  #:use-module (ice-9 match)
 | 
					  #:use-module (ice-9 match)
 | 
				
			||||||
  #:export (nginx-configuration
 | 
					  #:export (nginx-configuration
 | 
				
			||||||
            nginx-configuration?
 | 
					            nginx-configuration?
 | 
				
			||||||
 | 
					            nginx-vhost-configuration
 | 
				
			||||||
 | 
					            nginx-vhost-configuration?
 | 
				
			||||||
            nginx-service
 | 
					            nginx-service
 | 
				
			||||||
            nginx-service-type))
 | 
					            nginx-service-type))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,6 +41,26 @@
 | 
				
			||||||
;;;
 | 
					;;;
 | 
				
			||||||
;;; Code:
 | 
					;;; Code:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(define-record-type* <nginx-vhost-configuration>
 | 
				
			||||||
 | 
					  nginx-vhost-configuration make-nginx-vhost-configuration
 | 
				
			||||||
 | 
					  nginx-vhost-configuration?
 | 
				
			||||||
 | 
					  (http-port           nginx-vhost-configuration-http-port
 | 
				
			||||||
 | 
					                       (default 80))
 | 
				
			||||||
 | 
					  (https-port          nginx-vhost-configuration-https-port
 | 
				
			||||||
 | 
					                       (default 443))
 | 
				
			||||||
 | 
					  (server-name         nginx-vhost-configuration-server-name
 | 
				
			||||||
 | 
					                       (default (list 'default)))
 | 
				
			||||||
 | 
					  (root                nginx-vhost-configuration-root
 | 
				
			||||||
 | 
					                       (default "/srv/http"))
 | 
				
			||||||
 | 
					  (index               nginx-vhost-configuration-index
 | 
				
			||||||
 | 
					                       (default (list "index.html")))
 | 
				
			||||||
 | 
					  (ssl-certificate     nginx-vhost-configuration-ssl-certificate
 | 
				
			||||||
 | 
					                       (default "/etc/nginx/cert.pem"))
 | 
				
			||||||
 | 
					  (ssl-certificate-key nginx-vhost-configuration-ssl-certificate-key
 | 
				
			||||||
 | 
					                       (default "/etc/nginx/key.pem"))
 | 
				
			||||||
 | 
					  (server-tokens?      nginx-vhost-configuration-server-tokens?
 | 
				
			||||||
 | 
					                       (default #f)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(define-record-type* <nginx-configuration>
 | 
					(define-record-type* <nginx-configuration>
 | 
				
			||||||
  nginx-configuration make-nginx-configuration
 | 
					  nginx-configuration make-nginx-configuration
 | 
				
			||||||
  nginx-configuration?
 | 
					  nginx-configuration?
 | 
				
			||||||
| 
						 | 
					@ -46,16 +69,70 @@
 | 
				
			||||||
  (run-directory nginx-configuration-run-directory) ;string
 | 
					  (run-directory nginx-configuration-run-directory) ;string
 | 
				
			||||||
  (file          nginx-configuration-file))         ;string | file-like
 | 
					  (file          nginx-configuration-file))         ;string | file-like
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(define (default-nginx-config log-directory run-directory)
 | 
					(define (config-domain-strings names)
 | 
				
			||||||
 | 
					 "Return a string denoting the nginx config representation of NAMES, a list
 | 
				
			||||||
 | 
					of domain names."
 | 
				
			||||||
 | 
					 (string-concatenate
 | 
				
			||||||
 | 
					  (map (match-lambda
 | 
				
			||||||
 | 
					        ('default "_")
 | 
				
			||||||
 | 
					        ((? string? str) str))
 | 
				
			||||||
 | 
					       names)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(define (config-index-strings names)
 | 
				
			||||||
 | 
					 "Return a string denoting the nginx config representation of NAMES, a list
 | 
				
			||||||
 | 
					of index files."
 | 
				
			||||||
 | 
					 (string-concatenate
 | 
				
			||||||
 | 
					  (map (match-lambda
 | 
				
			||||||
 | 
					        ((? string? str) str))
 | 
				
			||||||
 | 
					       names)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(define (default-nginx-vhost-config vhost)
 | 
				
			||||||
 | 
					  (string-append
 | 
				
			||||||
 | 
					   "    server {\n"
 | 
				
			||||||
 | 
					   (if (nginx-vhost-configuration-http-port vhost)
 | 
				
			||||||
 | 
					       (string-append "      listen "
 | 
				
			||||||
 | 
					                      (number->string (nginx-vhost-configuration-http-port vhost))
 | 
				
			||||||
 | 
					                      ";\n")
 | 
				
			||||||
 | 
					       "")
 | 
				
			||||||
 | 
					   (if (nginx-vhost-configuration-https-port vhost)
 | 
				
			||||||
 | 
					       (string-append "      listen "
 | 
				
			||||||
 | 
					                      (number->string (nginx-vhost-configuration-https-port vhost))
 | 
				
			||||||
 | 
					                      " ssl;\n")
 | 
				
			||||||
 | 
					       "")
 | 
				
			||||||
 | 
					   "      server_name " (config-domain-strings
 | 
				
			||||||
 | 
					                         (nginx-vhost-configuration-server-name vhost))
 | 
				
			||||||
 | 
					                        ";\n"
 | 
				
			||||||
 | 
					   (if (nginx-vhost-configuration-ssl-certificate vhost)
 | 
				
			||||||
 | 
					       (string-append "      ssl_certificate "
 | 
				
			||||||
 | 
					                      (nginx-vhost-configuration-ssl-certificate vhost) ";\n")
 | 
				
			||||||
 | 
					       "")
 | 
				
			||||||
 | 
					   (if (nginx-vhost-configuration-ssl-certificate-key vhost)
 | 
				
			||||||
 | 
					       (string-append "      ssl_certificate_key "
 | 
				
			||||||
 | 
					                      (nginx-vhost-configuration-ssl-certificate-key vhost) ";\n")
 | 
				
			||||||
 | 
					       "")
 | 
				
			||||||
 | 
					   "      root " (nginx-vhost-configuration-root vhost) ";\n"
 | 
				
			||||||
 | 
					   "      index " (config-index-strings (nginx-vhost-configuration-index vhost)) ";\n"
 | 
				
			||||||
 | 
					   "      server_tokens " (if (nginx-vhost-configuration-server-tokens? vhost)
 | 
				
			||||||
 | 
					                              "on" "off") ";\n"
 | 
				
			||||||
 | 
					   "    }\n"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(define (default-nginx-config log-directory run-directory vhost-list)
 | 
				
			||||||
  (plain-file "nginx.conf"
 | 
					  (plain-file "nginx.conf"
 | 
				
			||||||
              (string-append
 | 
					              (string-append
 | 
				
			||||||
               "user nginx nginx;\n"
 | 
					               "user nginx nginx;\n"
 | 
				
			||||||
               "pid " run-directory "/pid;\n"
 | 
					               "pid " run-directory "/pid;\n"
 | 
				
			||||||
               "error_log " log-directory "/error.log info;\n"
 | 
					               "error_log " log-directory "/error.log info;\n"
 | 
				
			||||||
               "http {\n"
 | 
					               "http {\n"
 | 
				
			||||||
 | 
					               "    client_body_temp_path " run-directory "/client_body_temp;\n"
 | 
				
			||||||
 | 
					               "    proxy_temp_path " run-directory "/proxy_temp;\n"
 | 
				
			||||||
 | 
					               "    fastcgi_temp_path " run-directory "/fastcgi_temp;\n"
 | 
				
			||||||
 | 
					               "    uwsgi_temp_path " run-directory "/uwsgi_temp;\n"
 | 
				
			||||||
 | 
					               "    scgi_temp_path " run-directory "/scgi_temp;\n"
 | 
				
			||||||
               "    access_log " log-directory "/access.log;\n"
 | 
					               "    access_log " log-directory "/access.log;\n"
 | 
				
			||||||
               "    root /var/www;\n"
 | 
					               (let ((http (map default-nginx-vhost-config vhost-list)))
 | 
				
			||||||
               "    server {}\n"
 | 
					                 (do ((http http (cdr http))
 | 
				
			||||||
 | 
					                      (block "" (string-append (car http) "\n" block )))
 | 
				
			||||||
 | 
					                     ((null? http) block)))
 | 
				
			||||||
               "}\n"
 | 
					               "}\n"
 | 
				
			||||||
               "events {}\n")))
 | 
					               "events {}\n")))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -79,6 +156,12 @@
 | 
				
			||||||
         (mkdir-p #$log-directory)
 | 
					         (mkdir-p #$log-directory)
 | 
				
			||||||
         (format #t "creating nginx run directory '~a'~%" #$run-directory)
 | 
					         (format #t "creating nginx run directory '~a'~%" #$run-directory)
 | 
				
			||||||
         (mkdir-p #$run-directory)
 | 
					         (mkdir-p #$run-directory)
 | 
				
			||||||
 | 
					         (format #t "creating nginx temp directories '~a/{client_body,proxy,fastcgi,uwsgi,scgi}_temp'~%" #$run-directory)
 | 
				
			||||||
 | 
					         (mkdir-p (string-append #$run-directory "/client_body_temp"))
 | 
				
			||||||
 | 
					         (mkdir-p (string-append #$run-directory "/proxy_temp"))
 | 
				
			||||||
 | 
					         (mkdir-p (string-append #$run-directory "/fastcgi_temp"))
 | 
				
			||||||
 | 
					         (mkdir-p (string-append #$run-directory "/uwsgi_temp"))
 | 
				
			||||||
 | 
					         (mkdir-p (string-append #$run-directory "/scgi_temp"))
 | 
				
			||||||
         ;; Check configuration file syntax.
 | 
					         ;; Check configuration file syntax.
 | 
				
			||||||
         (system* (string-append #$nginx "/sbin/nginx")
 | 
					         (system* (string-append #$nginx "/sbin/nginx")
 | 
				
			||||||
                  "-c" #$config-file "-t")))))
 | 
					                  "-c" #$config-file "-t")))))
 | 
				
			||||||
| 
						 | 
					@ -114,8 +197,9 @@
 | 
				
			||||||
(define* (nginx-service #:key (nginx nginx)
 | 
					(define* (nginx-service #:key (nginx nginx)
 | 
				
			||||||
                        (log-directory "/var/log/nginx")
 | 
					                        (log-directory "/var/log/nginx")
 | 
				
			||||||
                        (run-directory "/var/run/nginx")
 | 
					                        (run-directory "/var/run/nginx")
 | 
				
			||||||
 | 
					                        (vhost-list (list (nginx-vhost-configuration)))
 | 
				
			||||||
                        (config-file
 | 
					                        (config-file
 | 
				
			||||||
                         (default-nginx-config log-directory run-directory)))
 | 
					                         (default-nginx-config log-directory run-directory vhost-list)))
 | 
				
			||||||
  "Return a service that runs NGINX, the nginx web server.
 | 
					  "Return a service that runs NGINX, the nginx web server.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The nginx daemon loads its runtime configuration from CONFIG-FILE, stores log
 | 
					The nginx daemon loads its runtime configuration from CONFIG-FILE, stores log
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Reference in a new issue