From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id mCWZMBlQg1+KEAAA0tVLHw (envelope-from ) for ; Sun, 11 Oct 2020 18:34:01 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id WFFmLBlQg18VGQAA1q6Kng (envelope-from ) for ; Sun, 11 Oct 2020 18:34:01 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 43AF79401BC for ; Sun, 11 Oct 2020 18:34:01 +0000 (UTC) Received: from localhost ([::1]:35822 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kRgAS-0008QQ-5i for larch@yhetil.org; Sun, 11 Oct 2020 14:34:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37464) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kRg7c-00061c-Mn for guix-patches@gnu.org; Sun, 11 Oct 2020 14:31:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:57726) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kRg7c-0008MU-CD for guix-patches@gnu.org; Sun, 11 Oct 2020 14:31:04 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kRg7c-0007TS-9u for guix-patches@gnu.org; Sun, 11 Oct 2020 14:31:04 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#43933] [PATCH 8/8] services: nginx: Add lua module. Resent-From: Oleg Pykhalov Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 11 Oct 2020 18:31:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 43933 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 43933@debbugs.gnu.org Cc: Oleg Pykhalov Received: via spool by 43933-submit@debbugs.gnu.org id=B43933.160244105828670 (code B ref 43933); Sun, 11 Oct 2020 18:31:04 +0000 Received: (at 43933) by debbugs.gnu.org; 11 Oct 2020 18:30:58 +0000 Received: from localhost ([127.0.0.1]:41031 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kRg7R-0007SC-Ov for submit@debbugs.gnu.org; Sun, 11 Oct 2020 14:30:57 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:34517) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kRg7P-0007Qz-5C for 43933@debbugs.gnu.org; Sun, 11 Oct 2020 14:30:51 -0400 Received: by mail-lf1-f65.google.com with SMTP id u8so16068910lff.1 for <43933@debbugs.gnu.org>; Sun, 11 Oct 2020 11:30:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SE8HIWySTJxpkeV27HL1QEGlhUbP1HLAdeb/ek2jgHw=; b=HSU5OhTOXHIsWtR1Ztth4RdiqjtodT1Yp8h6iPLlrGZtH5I08H95iZp3LTmMTVS2PM 8Ncn1Bfum0FECOpZTL3T/LAbQ1Hhgkb5Bgsnh4snJZ83mVwvKShcGkGq4o93TuYP1N8i IkJXy69jIAM4VpkoYzXiJXCtSNuhDEW/HP0wMMiQxK/fRJGNK9KdUPhWqeGkwl+Ztg0V fW51DyAFXREuS4Rr/ZFA/KhrQOQyWVpCdl0mh/PxpQnVLBFbxkA8+3Rm9wiSHfSjidlA gRsqxRj6dQge4owZByAjvZDTSuPpt+CYtaQOQe6ZaNBKKK1i4rQtp6+LPzT8deJWnMWm 5olQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SE8HIWySTJxpkeV27HL1QEGlhUbP1HLAdeb/ek2jgHw=; b=YvT+3Vzaebrwu8ddpUw3I6ci4XSly9k5woOt/8g1CKFHRCSS25FXZT+WMl53QRAhGV UXotXzqbhTIOyF93nPGDMpmR9hMiavV8BW8ZfGYDHkm2lQ87J/D952GetqVyKSMLaYpz FZM9FL0lr6FwCqDQQN1CPf+4Cs1q07R3zXgxwzBozov0cF+CCp9jhzXvU4FVJUA1uQza 2u8HVTbpJ6pMkZsdauBPk6rhgyDM+pnh3imJNbbKng+IJTwjPhPLNEpxARga6j/+erWy 8kqZlb2lX1f6cSJAPj0qDFVcipxR7G5rWR5e0YRII+R2TYT95ZOx+Y4GAYBHXW3EQVGg wjcA== X-Gm-Message-State: AOAM530DMfIf3JUdq6l9XSka6ew4xkGiKZPgBw4xuonA7DjPCYutNSjH DGRw+sbbPaqldYzEZRXOxs9hzeSxlic= X-Google-Smtp-Source: ABdhPJzTgR+ZNV7Wc1XXUrBjO28PlBXBifGpDXRgpVwWaaGrPlHu/PdN2dwMsanzu2dc/q/14vujtQ== X-Received: by 2002:ac2:5a05:: with SMTP id q5mr6812584lfn.592.1602441044930; Sun, 11 Oct 2020 11:30:44 -0700 (PDT) Received: from localhost.localdomain (95-55-4-176.dynamic.avangarddsl.ru. [95.55.4.176]) by smtp.gmail.com with ESMTPSA id l188sm2669151lfd.127.2020.10.11.11.30.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Oct 2020 11:30:44 -0700 (PDT) From: Oleg Pykhalov Date: Sun, 11 Oct 2020 21:30:12 +0300 Message-Id: <20201011183012.15932-8-go.wigust@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201011183012.15932-1-go.wigust@gmail.com> References: <20201011183012.15932-1-go.wigust@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Score: 0.0 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Spam-Score: -1.0 (-) X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=fail (rsa verify failed) header.d=gmail.com header.s=20161025 header.b=HSU5OhTO; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Spam-Score: 1.69 X-TUID: 9s6wh3xAJWyZ * gnu/services/web.scm () [lua-package-path, lua-package-cpath]: New record types. * gnu/services/web.scm (default-nginx-config): Use them. * doc/guix.texi (Web Services): Document this. * doc/guix-cookbook.texi (System Configuration): Document this. --- doc/guix-cookbook.texi | 58 ++++++++++++++++++++++++++++++++++++++++++ doc/guix.texi | 24 ++++++++++++++++- gnu/services/web.scm | 25 ++++++++++++++---- 3 files changed, 101 insertions(+), 6 deletions(-) diff --git a/doc/guix-cookbook.texi b/doc/guix-cookbook.texi index a783c0ae4c..6577c340d9 100644 --- a/doc/guix-cookbook.texi +++ b/doc/guix-cookbook.texi @@ -1352,6 +1352,7 @@ reference. * Running Guix on a Linode Server:: Running Guix on a Linode Server * Setting up a bind mount:: Setting up a bind mount in the file-systems definition. * Getting substitutes from Tor:: Configuring Guix daemon to get substitutes through Tor. +* Setting up NGINX with Lua:: Configuring NGINX web-server to load Lua modules. @end menu @node Customizing the Kernel @@ -2113,6 +2114,63 @@ sudo herd set-http-proxy guix-daemon http://localhost:9250 guix build --substitute-urls=https://bp7o7ckwlewr4slm.onion … @end example +@node Setting up NGINX with Lua +@section Setting up NGINX with Lua +@cindex nginx, lua, openresty, resty + +NGINX could be extended with Lua scripts. + +Guix provides NGINX service with ability to load Lua module and specific +Lua packages, and reply to requests by evaluating Lua scripts. + +The following example demonstrates system definition with configuration +to evaluate @file{index.lua} Lua script on HTTP request to +@uref{http://localhost/hello} endpoint: + +@example +local shell = require "resty.shell" + +local stdin = "" +local timeout = 1000 -- ms +local max_size = 4096 -- byte + +local ok, stdout, stderr, reason, status = + shell.run([[/run/current-system/profile/bin/ls /tmp]], stdin, timeout, max_size) + +ngx.say(stdout) +@end example + +@lisp +(use-modules (gnu)) +(use-service-modules #;… web) +(use-package-modules #;… lua) +(operating-system + ;; … + (services + ;; … + (service nginx-service-type + (nginx-configuration + (modules + (list + (file-append nginx-lua-module "/etc/nginx/modules/ngx_http_lua_module.so"))) + (lua-package-path (list lua-resty-core + lua-resty-lrucache + lua-resty-signal + lua-tablepool + lua-resty-shell)) + (lua-package-cpath (list lua-resty-signal)) + (server-blocks + (list (nginx-server-configuration + (server-name '("localhost")) + (listen '("80")) + (root "/etc") + (locations (list + (nginx-location-configuration + (uri "/hello") + (body (list #~(format #f "content_by_lua_file ~s;" + #$(local-file "index.lua")))))))))))))) +@end lisp + @c ********************************************************************* @node Advanced package management @chapter Advanced package management diff --git a/doc/guix.texi b/doc/guix.texi index 8514dfe86f..c8e5928590 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -21903,7 +21903,29 @@ names of loadable modules, as in this example: (modules (list (file-append nginx-accept-language-module "\ -/etc/nginx/modules/ngx_http_accept_language_module.so"))) +/etc/nginx/modules/ngx_http_accept_language_module.so") + (file-append nginx-lua-module "\ +/etc/nginx/modules/ngx_http_lua_module.so"))) +@end lisp + +@item @code{lua-package-path} (default: @code{'()}) +List of nginx lua packages to load. This should be a list of package +names of loadable lua modules, as in this example: + +@lisp +(lua-package-path (list lua-resty-core + lua-resty-lrucache + lua-resty-signal + lua-tablepool + lua-resty-shell)) +@end lisp + +@item @code{lua-package-cpath} (default: @code{'()}) +List of nginx lua C packages to load. This should be a list of package +names of loadable lua C modules, as in this example: + +@lisp +(lua-package-cpath (list lua-resty-signal)) @end lisp @item @code{global-directives} (default: @code{'((events . ()))}) diff --git a/gnu/services/web.scm b/gnu/services/web.scm index c8ffc19d83..d17faa2b59 100644 --- a/gnu/services/web.scm +++ b/gnu/services/web.scm @@ -13,6 +13,7 @@ ;;; Copyright © 2020 Ricardo Wurmus ;;; Copyright © 2020 Tobias Geerinckx-Rice ;;; Copyright © 2020 Arun Isaac +;;; Copyright © 2020 Oleg Pykhalov ;;; ;;; This file is part of GNU Guix. ;;; @@ -525,6 +526,10 @@ (modules nginx-configuration-modules (default '())) (global-directives nginx-configuration-global-directives (default '((events . ())))) + (lua-package-path nginx-lua-package-path ;list of + (default #f)) + (lua-package-cpath nginx-lua-package-cpath ;list of + (default #f)) (extra-content nginx-configuration-extra-content (default "")) (file nginx-configuration-file ;#f | string | file-like @@ -630,6 +635,8 @@ of index files." server-names-hash-bucket-max-size modules global-directives + lua-package-path + lua-package-cpath extra-content) (apply mixed-text-file "nginx.conf" (flatten @@ -646,11 +653,19 @@ of index files." " scgi_temp_path " run-directory "/scgi_temp;\n" " access_log " log-directory "/access.log;\n" " include " nginx "/share/nginx/conf/mime.types;\n" - (if server-names-hash-bucket-size - (string-append - " server_names_hash_bucket_size " - (number->string server-names-hash-bucket-size) - ";\n") + (if lua-package-path + #~(format #f " lua_package_path ~s;~%" + (string-join (map (lambda (path) + (string-append path "/lib/?.lua")) + '#$lua-package-path) + ";")) + "") + (if lua-package-cpath + #~(format #f " lua_package_cpath ~s;~%" + (string-join (map (lambda (cpath) + (string-append cpath "/lib/lua/?.lua")) + '#$lua-package-cpath) + ";")) "") (if server-names-hash-bucket-max-size (string-append -- 2.28.0