From mboxrd@z Thu Jan 1 00:00:00 1970 From: Julien Lepiller Subject: [PATCH] improve nginx-service Date: Sun, 16 Oct 2016 14:33:47 +0200 Message-ID: <20161016143347.38d8a6f2@polymos.lepiller.eu> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/+jNOs=9xIqPfvG+4z8aIuTY" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:45909) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bvkcV-0003kV-T9 for guix-devel@gnu.org; Sun, 16 Oct 2016 08:32:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bvkcQ-0002lO-VZ for guix-devel@gnu.org; Sun, 16 Oct 2016 08:32:51 -0400 Received: from dau94-h03-89-91-205-84.dsl.sta.abo.bbox.fr ([89.91.205.84]:34487 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 1bvkcQ-0002kb-7v for guix-devel@gnu.org; Sun, 16 Oct 2016 08:32:46 -0400 Received: from localhost (localhost [127.0.0.1]) by skaro.lepiller.eu (Postfix) with ESMTP id 8BD8181021 for ; Sun, 16 Oct 2016 14:31:00 +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 050wDQvLp4Hu for ; Sun, 16 Oct 2016 14:30:54 +0200 (CEST) Received: from polymos.lepiller.eu (89-93-157-164.hfc.dyn.abo.bbox.fr [89.93.157.164]) by skaro.lepiller.eu (Postfix) with ESMTPSA id 5E6B07FB55 for ; Sun, 16 Oct 2016 14:30:53 +0200 (CEST) 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 --MP_/+jNOs=9xIqPfvG+4z8aIuTY Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, this patch fixes a problem with nginx configuration (ensuring some directories are available to nginx) and adds vhost support in its configuration. Also updates the doc accordingly. --MP_/+jNOs=9xIqPfvG+4z8aIuTY Content-Type: text/x-patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=0001-services-improve-nginx-service-configuration.patch =46rom 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'. --- doc/guix.texi | 44 ++++++++++++++++++++++++++++ gnu/services/web.scm | 81 ++++++++++++++++++++++++++++++++++++++++++++++++= +--- 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 47fc199..2e7af90 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -10370,6 +10370,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. @@ -10380,8 +10381,51 @@ 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 vhosts 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: 80) +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. + +@item @code{https-port} (default: 443) +Nginx will listen for https connection on this port. Set it at #f if nginx +should not listen for https (secure) connection for this vhost. + +Note that nginx can listen for http and https connections in the same vhos= t. + +@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: ``/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: ``/etc/nginx/cert.pem'') +Where to find the certificate for secure connections. Set it to #f if you = don't +have a certificate or you don't want to use https. + +@item @code{ssl-certificate-key} (default: ``/etc/nginx/key.pem'') +Where to find the private key for secure connections. Set it to #f if you = don't +have a key or you don't want to use https. + +@item @code{server-tokens?} (default: #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..dca4972 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")) + (ssh-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,59 @@ (run-directory nginx-configuration-run-directory) ;string (file nginx-configuration-file)) ;string | file-like =20 -(define (default-nginx-config log-directory run-directory) +(define (list-names names) + (match names + ('() "") + ((head tail ...) (string-append (if (eq? head 'default) "_" head) + " " + (list-names tail))))) + +(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 " (list-names (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 " (list-names (nginx-vhost-configuration-index vhost)) ";\= 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 +145,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 +186,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_/+jNOs=9xIqPfvG+4z8aIuTY--