1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
;;; genenetwork-machines --- Guix configuration for genenetwork machines
;;; Copyright © 2026 the GeneNetwork team
;;;
;;; This file is part of genenetwork-machines.
;;;
;;; genenetwork-machines is free software: you can redistribute it
;;; and/or modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; genenetwork-machines is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the implied
;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;;; See the GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with genenetwork-machines. If not, see
;;; <https://www.gnu.org/licenses/>.
;;; Typed serializers for nginx directives. Instead of hand-building
;;; directive strings in location bodies, directives that carry
;;; configuration values are expressed as typed records (like the
;;; environment-variable records of the standard fcgiwrap service) or
;;; built by dedicated serializer procedures, which take care of the
;;; quoting nginx needs. Used by (genenetwork services cgit),
;;; (genenetwork services genecup), ... to contribute
;;; nginx-server-configuration records to the stock
;;; nginx-service-type.
(define-module (genenetwork services nginx)
#:export (<fastcgi-parameter>
fastcgi-parameter
fastcgi-parameter?
fastcgi-parameter->nginx
fastcgi-pass
<proxy-header>
proxy-header
proxy-header?
proxy-header->nginx
proxy-pass
nginx-unix-socket-url
unix-socket-proxy-pass
try-files
nginx-include
limit-request
limit-request-status
limit-request-log-level
limit-request-zone
limit-connection
limit-connection-status
limit-connection-log-level
limit-connection-zone
set-real-ip-from
real-ip-header
real-ip-recursive
client-max-body-size)
#:use-module (guix records)
#:use-module (srfi srfi-1))
(define (quoted-if-string value)
"Return VALUE quoted if it is a string; file-like values are store
paths and need no quoting. nginx expands its own variables such as
@code{$uri} inside double quotes."
(if (string? value)
(string-append "\"" value "\"")
value))
;;; FastCGI parameters, passed to the CGI program with
;;; @code{fastcgi_param}.
(define-record-type* <fastcgi-parameter>
fastcgi-parameter make-fastcgi-parameter fastcgi-parameter?
(name fastcgi-parameter-name) ; string: FastCGI parameter name
(value fastcgi-parameter-value)) ; string or file-like: its value
(define (fastcgi-parameter->nginx parameter)
"Return an nginx @code{fastcgi_param} directive, as a body element
for nginx-location-configuration, passing PARAMETER to the CGI
program."
(list "fastcgi_param " (fastcgi-parameter-name parameter) " "
(quoted-if-string (fastcgi-parameter-value parameter))
";"))
(define (fastcgi-pass socket)
"Return an nginx @code{fastcgi_pass} directive for SOCKET, a unix
socket path."
(string-append "fastcgi_pass unix:" socket ";"))
;;; Proxy request headers, passed to the upstream with
;;; @code{proxy_set_header}.
(define-record-type* <proxy-header>
proxy-header make-proxy-header proxy-header?
(name proxy-header-name) ; string: header name
(value proxy-header-value)) ; string: header value
(define (proxy-header->nginx header)
"Return an nginx @code{proxy_set_header} directive, as a body
element for nginx-location-configuration, passing HEADER to the
proxied server."
(list "proxy_set_header " (proxy-header-name header) " "
(quoted-if-string (proxy-header-value header))
";"))
(define (proxy-pass url)
"Return an nginx @code{proxy_pass} directive for URL."
(string-append "proxy_pass " url ";"))
(define (nginx-unix-socket-url socket)
"Return the @code{proxy_pass} URL form of unix socket path SOCKET."
(string-append "http://unix:" socket))
(define (unix-socket-proxy-pass socket)
"Return an nginx @code{proxy_pass} directive proxying to unix socket
path SOCKET."
(proxy-pass (nginx-unix-socket-url socket)))
(define (try-files . uris)
"Return an nginx @code{try_files} directive trying URIS in order."
(string-append "try_files " (string-join uris " ") ";"))
(define (nginx-include file)
"Return an nginx @code{include} directive for FILE, a file name or
file-like object."
(if (string? file)
(string-append "include " file ";")
(list "include " file ";")))
(define (limit-request zone burst)
"Return an nginx @code{limit_req} directive limiting requests to
ZONE, with a burst queue of BURST requests served without delay."
(string-append "limit_req zone=" zone
" burst=" (number->string burst)
" nodelay;"))
(define (limit-request-status status)
"Return an nginx @code{limit_req_status} directive answering STATUS
when a request is rejected by rate limiting."
(string-append "limit_req_status " (number->string status) ";"))
(define (limit-request-log-level level)
"Return an nginx @code{limit_req_log_level} directive logging
rejected requests at LEVEL (a symbol naming an nginx log level)."
(string-append "limit_req_log_level " (symbol->string level) ";"))
(define (limit-request-zone key zone size rate)
"Return an nginx @code{limit_req_zone} directive keeping at most
SIZE of shared state for requests keyed on KEY, limited to RATE
(nginx's r/m or r/s notation)."
(string-append "limit_req_zone " key
" zone=" zone ":" size
" rate=" rate ";"))
(define (limit-connection zone number)
"Return an nginx @code{limit_conn} directive allowing at most
NUMBER concurrent connections to ZONE per key."
(string-append "limit_conn " zone
" " (number->string number) ";"))
(define (limit-connection-status status)
"Return an nginx @code{limit_conn_status} directive answering
STATUS when a connection is rejected by connection limiting."
(string-append "limit_conn_status " (number->string status) ";"))
(define (limit-connection-log-level level)
"Return an nginx @code{limit_conn_log_level} directive logging
rejected connections at LEVEL (a symbol naming an nginx log level)."
(string-append "limit_conn_log_level " (symbol->string level) ";"))
(define (limit-connection-zone key zone size)
"Return an nginx @code{limit_conn_zone} directive keeping at most
SIZE of shared state for concurrent connections keyed on KEY."
(string-append "limit_conn_zone " key
" zone=" zone ":" size ";"))
(define (client-max-body-size size)
"Return an nginx @code{client_max_body_size} directive for SIZE
(nginx byte-size notation, for example 32m)."
(string-append "client_max_body_size " size ";"))
;; Client address recovery. Behind a reverse proxy every request
;; arrives from the proxy's address, so a limit keyed on
;; $binary_remote_addr turns into ONE shared budget for the proxy's
;; whole clientele. @code{set_real_ip_from} marks the proxies whose
;; client-address header may be believed, @code{real_ip_header} names
;; that header, and @code{real_ip_recursive} makes nginx walk it from
;; the right past the trusted hops instead of taking the last entry.
(define (set-real-ip-from address)
"Return an nginx @code{set_real_ip_from} directive trusting ADDRESS
(an IP address or CIDR block) to report the client address."
(string-append "set_real_ip_from " address ";"))
(define (real-ip-header name)
"Return an nginx @code{real_ip_header} directive reading the client
address from the request header NAME (for example X-Forwarded-For)."
(string-append "real_ip_header " name ";"))
(define (real-ip-recursive)
"Return the nginx @code{real_ip_recursive} directive: walk the
client-address header from the right, skipping trusted addresses,
rather than taking its last entry."
"real_ip_recursive on;")
|