From 8c00b83818acbbc3dfd406465e50439c3418afc5 Mon Sep 17 00:00:00 2001 From: Julien Lepiller Date: Mon, 26 Sep 2016 23:55:58 +0200 Subject: services: nginx: Virtual hosts are first-class configuration items. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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'. Signed-off-by: Ludovic Courtès --- gnu/services/web.scm | 92 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 4 deletions(-) (limited to 'gnu/services') diff --git a/gnu/services/web.scm b/gnu/services/web.scm index 0a2a09bbf5..59e1e54e04 100644 --- a/gnu/services/web.scm +++ b/gnu/services/web.scm @@ -2,6 +2,7 @@ ;;; Copyright © 2015 David Thompson ;;; Copyright © 2015, 2016 Ludovic Courtès ;;; Copyright © 2016 ng0 +;;; Copyright © 2016 Julien Lepiller ;;; ;;; This file is part of GNU Guix. ;;; @@ -29,6 +30,8 @@ (define-module (gnu services web) #:use-module (ice-9 match) #:export (nginx-configuration nginx-configuration? + nginx-vhost-configuration + nginx-vhost-configuration? nginx-service nginx-service-type)) @@ -38,6 +41,26 @@ (define-module (gnu services web) ;;; ;;; Code: +(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 @@ (define-record-type* (run-directory nginx-configuration-run-directory) ;string (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" (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_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" - " root /var/www;\n" - " server {}\n" + (let ((http (map default-nginx-vhost-config vhost-list))) + (do ((http http (cdr http)) + (block "" (string-append (car http) "\n" block ))) + ((null? http) block))) "}\n" "events {}\n"))) @@ -79,6 +156,12 @@ (define nginx-activation (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,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. (system* (string-append #$nginx "/sbin/nginx") "-c" #$config-file "-t"))))) @@ -114,8 +197,9 @@ (define nginx-service-type (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. The nginx daemon loads its runtime configuration from CONFIG-FILE, stores log -- cgit v1.2.3