From mboxrd@z Thu Jan 1 00:00:00 1970 From: Julien Lepiller Subject: Re: [PATCH] improve nginx-service Date: Thu, 20 Oct 2016 14:37:44 +0200 Message-ID: <20161020143744.516a1184@polymos.lepiller.eu> References: <20161016143347.38d8a6f2@polymos.lepiller.eu> <874m483vap.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/BsBuplU2hx0hVuYA4zDT2X7" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:47549) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bxCaO-0003f0-0h for guix-devel@gnu.org; Thu, 20 Oct 2016 08:36:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bxCaK-0004uX-KR for guix-devel@gnu.org; Thu, 20 Oct 2016 08:36:40 -0400 Received: from dau94-h03-89-91-205-84.dsl.sta.abo.bbox.fr ([89.91.205.84]:39792 helo=skaro.lepiller.eu) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1bxCaK-0004tJ-0G for guix-devel@gnu.org; Thu, 20 Oct 2016 08:36:36 -0400 Received: from localhost (localhost [127.0.0.1]) by skaro.lepiller.eu (Postfix) with ESMTP id A8B8780FB1 for ; Thu, 20 Oct 2016 14:34:49 +0200 (CEST) Received: from skaro.lepiller.eu ([127.0.0.1]) by localhost (skaro.lepiller.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id berSRYG5EeyK for ; Thu, 20 Oct 2016 14:34:42 +0200 (CEST) Received: from polymos.lepiller.eu (unknown [131.254.252.220]) by skaro.lepiller.eu (Postfix) with ESMTPSA id 52B647FAEA for ; Thu, 20 Oct 2016 14:34:42 +0200 (CEST) In-Reply-To: <874m483vap.fsf@gnu.org> List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: "Guix-devel" To: guix-devel@gnu.org --MP_/BsBuplU2hx0hVuYA4zDT2X7 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Wed, 19 Oct 2016 23:04:14 +0200 ludo@gnu.org (Ludovic Court=C3=A8s) wrote: > Hi Julien, >=20 > This looks like a great improvement to me! Sounds nicer than fiddling > with config files. >=20 > I suppose we could make =E2=80=98nginx-service-type=E2=80=99 extensible (= info "(guix) > Service Types and Services") so that people can write services that > define new vhosts? You mean something like udev-service-type where you could extend it with a list of vhosts? Except for that comment, I modified the patch following your advice. >=20 > Julien Lepiller skribis: >=20 > > From 613d5db739d4010be2037fd2fcfc70baca4625aa Mon Sep 17 00:00:00 > > 2001 From: Julien Lepiller > > Date: Mon, 26 Sep 2016 23:55:58 +0200 > > Subject: [PATCH] services: improve nginx-service configuration > > > > * gnu/services/web.scm (): New record > > type. > > * doc/guix.texi (Web Services): Document > > 'nginx-vhost-configuration'. =20 >=20 > Please mention the other changes in web.scm: new procedures, changes > in existing procedures, etc. >=20 > > +As an alternative to using a @var{config-file}, @var{vhost-list} > > can be +used to specify the list of vhosts required on the host. > > For this to work, =20 >=20 > =E2=80=9Cthe list of @dfn{virtual hosts} or @dfn{vhosts}=E2=80=9D >=20 > > +@deftp {Data Type} nginx-vhost-configuration > > +Data type representing the configuration of an nginx virtual > > host. =20 >=20 > @dfn{virtual host} >=20 > > +This type has the following parameters: > > +@table @asis > > +@item @code{http-port} (default: 80) =20 >=20 > @code{80}; likewise for all the other default values. >=20 > > +Nginx will listen for http connection on this port. Set it at #f > > if nginx should +not listen for http (non secure) connection for > > this vhost. =20 >=20 > s/http/HTTP/ and s/#f/@code{#f}/ > Please leave two spaces after an end-of-sentence period. >=20 > > +(define (list-names names) > > + (match names > > + ('() "") > > + ((head tail ...) (string-append (if (eq? head 'default) "_" head) > > + " " > > + (list-names tail))))) =20 >=20 > Maybe call it =E2=80=98config-value-strings=E2=80=99? Please add a docst= ring. I > think it=E2=80=99d be more readable like this: >=20 > (define (config-value-strings names) > "Return a string denoting the nginx config representation of > NAMES, a list of foobars=E2=80=A6" > (string-concatenate > (map (match-lambda > ('default "_") > ((? string? str) str)) > names))) >=20 > Could you send an updated patch? >=20 > Unless David, who initially wrote this service has objections, this > patch looks good to me with the changes as suggested above. >=20 > Thanks! >=20 > Ludo=E2=80=99. --MP_/BsBuplU2hx0hVuYA4zDT2X7 Content-Type: text/x-patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=0001-services-improve-nginx-service-configuration.patch =46rom 8bda6fdd53b3cc7470fac67228a88e0d165134dd Mon Sep 17 00:00:00 2001 From: Julien Lepiller Date: Mon, 26 Sep 2016 23:55:58 +0200 Subject: [PATCH] services: improve nginx-service configuration * gnu/services/web.scm (): 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'. --- doc/guix.texi | 46 ++++++++++++++++++++++++++ gnu/services/web.scm | 92 ++++++++++++++++++++++++++++++++++++++++++++++++= +--- 2 files changed, 134 insertions(+), 4 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 0c5d641..c36a29c 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -10394,6 +10394,7 @@ The @code{(gnu services web)} module provides the f= ollowing service: @deffn {Scheme Procedure} nginx-service [#:nginx nginx] @ [#:log-directory ``/var/log/nginx''] @ [#:run-directory ``/var/run/nginx''] @ + [#:vhost-list (list (nginx-vhost-configuration))] @ [#:config-file] =20 Return a service that runs @var{nginx}, the nginx web server. @@ -10404,8 +10405,53 @@ files are written to @var{run-directory}. For pro= per operation, these arguments should match what is in @var{config-file} to ensure that the directories are created when the service is activated. =20 +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 =20 +@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{virtua= l 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 t= he +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 @subsubsection Network File System @cindex NFS diff --git a/gnu/services/web.scm b/gnu/services/web.scm index 0a2a09b..49a2962 100644 --- a/gnu/services/web.scm +++ b/gnu/services/web.scm @@ -2,6 +2,7 @@ ;;; Copyright =C2=A9 2015 David Thompson ;;; Copyright =C2=A9 2015, 2016 Ludovic Court=C3=A8s ;;; Copyright =C2=A9 2016 ng0 +;;; Copyright =C2=A9 2016 Julien Lepiller ;;; ;;; This file is part of GNU Guix. ;;; @@ -29,6 +30,8 @@ #:use-module (ice-9 match) #:export (nginx-configuration nginx-configuration? + nginx-vhost-configuration + nginx-vhost-configuration? nginx-service nginx-service-type)) =20 @@ -38,6 +41,26 @@ ;;; ;;; Code: =20 +(define-record-type* + 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 make-nginx-configuration nginx-configuration? @@ -46,16 +69,70 @@ (run-directory nginx-configuration-run-directory) ;string (file nginx-configuration-file)) ;string | file-like =20 -(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=20 + " 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-por= t 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 v= host)) ";\n" + " server_tokens " (if (nginx-vhost-configuration-server-tokens? vh= ost) + "on" "off") ";\n" + " }\n")) + +(define (default-nginx-config log-directory run-directory vhost-list) (plain-file "nginx.conf" (string-append "user nginx nginx;\n" "pid " run-directory "/pid;\n" "error_log " log-directory "/error.log info;\n" "http {\n" + " client_body_temp_path " run-directory "/client_body_te= mp;\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" - " root /var/www;\n" - " server {}\n" + (let ((http (map default-nginx-vhost-config vhost-list))) + (do ((http http (cdr http))=20 + (block "" (string-append (car http) "\n" block ))) + ((null? http) block))) "}\n" "events {}\n"))) =20 @@ -79,6 +156,12 @@ (mkdir-p #$log-directory) (format #t "creating nginx run directory '~a'~%" #$run-directory) (mkdir-p #$run-directory) + (format #t "creating nginx temp directories '~a/{client_body,prox= y,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. (system* (string-append #$nginx "/sbin/nginx") "-c" #$config-file "-t"))))) @@ -114,8 +197,9 @@ (define* (nginx-service #:key (nginx nginx) (log-directory "/var/log/nginx") (run-directory "/var/run/nginx") + (vhost-list (list (nginx-vhost-configuration))) (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. =20 The nginx daemon loads its runtime configuration from CONFIG-FILE, stores = log --=20 2.10.1 --MP_/BsBuplU2hx0hVuYA4zDT2X7--