-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnginx.scm
452 lines (364 loc) · 13.5 KB
/
nginx.scm
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
(import (scheme base) (scheme file) (scheme write) (srfi 1))
(define (display-lines lines)
(for-each (lambda (line) (display line) (newline))
lines))
(define (flatten-all x)
(if (list? x)
(append-map flatten-all x)
(list x)))
(define (string-join strings delimiter)
(if (null? strings)
""
(fold (lambda (s so-far) (string-append so-far delimiter s))
(car strings) (cdr strings))))
(define (indent string)
(string-append (make-string 4 #\space) string))
(define (block head . body)
(append (list (string-append head " {"))
(map indent (flatten-all body))
(list "}")))
(define (alist-change alist key val)
(map (lambda (pair) (if (equal? key (car pair)) (cons key val) pair))
alist))
(define (add-header name . params)
(if (null? params)
'()
(list
(string-join (list "add_header"
name
(string-append "\"" (string-join params "; ") "\"")
"always;")
" "))))
;;;;
(define letsencrypt-etc "/etc/letsencrypt")
(define certificate-hostname (make-parameter #f))
(define blocked-features
'("accelerometer"
"ambient-light-sensor"
"autoplay"
"camera"
"display-capture"
"document-domain"
"encrypted-media"
"fullscreen"
"geolocation"
"gyroscope"
"layout-animations"
"magnetometer"
"microphone"
"midi"
"payment"
"picture-in-picture"
"speaker"
"usb"
"vibrate"
"vr"))
(define content-security-policy
(make-parameter
'(("default-src" "'self'")
("style-src" "'self'" "'unsafe-inline'")
("script-src" "'self'" "'unsafe-inline'")
("upgrade-insecure-requests"))))
(define (encode-csp-directive directive) (string-join directive " "))
(define (https-security-header-lines)
(append
(apply add-header "Content-Security-Policy"
(map encode-csp-directive (content-security-policy)))
(apply add-header "Feature-Policy"
(map (lambda (feature) (string-append feature " 'none'"))
blocked-features))
(add-header "Referrer-Policy" "no-referrer")
(add-header "Strict-Transport-Security"
"max-age=31536000"
"includeSubDomains")
(add-header "X-Content-Type-Options" "nosniff")
(add-header "X-Frame-Options" "SAMEORIGIN")
(add-header "X-Permitted-Cross-Domain-Policies" "none")
(add-header "X-Xss-Protection" "1" "mode=block")))
(define (default-servers)
(list (block "server"
"server_name _;"
"listen 80 default_server;"
"listen [::]:80 default_server;"
"return 444;")
(block "server"
"server_name _;"
"listen 443 default_server;"
"listen [::]:443 default_server;"
"ssl_reject_handshake on;")))
(define (http->https-redirect-server primary alias)
(block "server"
(string-append "server_name " alias ";")
"listen [::]:80;"
"listen 80;"
(string-append "return 301 https://" primary "$request_uri;")))
(define (https-redirect-server primary alias)
(block "server"
(string-append "server_name " alias ";")
"listen [::]:443 ssl;"
"listen 443 ssl;"
(string-append "include " letsencrypt-etc "/options-ssl-nginx.conf;")
(https-security-header-lines)
(string-append "return 301 https://" primary "$request_uri;")))
(define (https-only-server hostname . lines)
(apply block
"server"
(string-append "server_name " hostname ";")
"listen [::]:443 ssl;"
"listen 443 ssl;"
(string-append "include " letsencrypt-etc "/options-ssl-nginx.conf;")
(https-security-header-lines)
lines))
(define (https-server hostnames . lines)
(let ((primary (car hostnames))
(aliases (cdr hostnames)))
(append (apply https-only-server primary lines)
(append-map (lambda (hostname)
(https-redirect-server primary hostname))
aliases)
(append-map (lambda (hostname)
(http->https-redirect-server primary hostname))
hostnames))))
(define (http-redirect-only-server hostname redirect-to)
(append
(http->https-redirect-server hostname hostname)
(block
"server"
(string-append "server_name " hostname ";")
"listen [::]:443 ssl;"
"listen 443 ssl;"
(string-append "include " letsencrypt-etc "/options-ssl-nginx.conf;")
(https-security-header-lines)
(block "location = /"
(string-append "return 301 " redirect-to ";")))))
(define (log-directives hostname)
(list (string-append "access_log /var/log/nginx/" hostname "_access.log;")
(string-append "error_log /var/log/nginx/" hostname "_error.log;")))
(define (static-site subdomain . body)
(let ((hostname (string-append subdomain ".scheme.org")))
(apply
https-server
(list hostname)
(append
(log-directives hostname)
(list (string-append "root /production/" subdomain "/www;"))
body))))
;;;;
(define cors
(list "add_header 'Access-Control-Allow-Origin' '*';"
"add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';"
"add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';"))
(define (http-block-common-part)
(list
"include /etc/nginx/mime.types;"
(block "types"
;; Unix manual pages in troff format.
"text/plain 1;"
"text/plain 2;"
"text/plain 3;"
"text/plain 4;"
"text/plain 5;"
"text/plain 6;"
"text/plain 7;"
"text/plain 8;"
"text/plain 9;"
"text/plain diff;"
"text/plain patch;"
"text/plain scm;" ; Scheme source code
"text/plain ss;" ; Ditto
"text/plain pp;" ; Ditto (used by psyntax)
"text/plain pose;" ; Portable S-expressions
"text/plain text;")
"default_type application/octet-stream;"
"charset utf-8;"
"charset_types text/plain;"
"sendfile on;"
"gzip on;"
;; An "expires" time of one month is way too long for some
;; things, especially planet and gitea.
;; TODO: Perhaps we should specify "expires" per subdomain?
;;"expires 1M;"
"server_tokens off;"
"ssl_stapling on;"
"ssl_stapling_verify on;"
(string-append "ssl_certificate " letsencrypt-etc
"/live/" (certificate-hostname) "/fullchain.pem;")
(string-append "ssl_certificate_key " letsencrypt-etc
"/live/" (certificate-hostname) "/privkey.pem;")
(string-append "ssl_dhparam " letsencrypt-etc
"/ssl-dhparams.pem;")
(default-servers)))
(define (write-host-nginx-conf hostname hostname-short http-block-extra)
(let ((conf-file (string-append hostname-short "-nginx.conf")))
(with-output-to-file conf-file
(lambda ()
(parameterize ((certificate-hostname hostname))
(display-lines
(append
(block "events")
(block "http"
(http-block-common-part)
http-block-extra))))))))
(define (write-jenkins-nginx-conf)
(write-host-nginx-conf
"jenkins.scheme.org"
"jenkins"
(list
(https-server
'("jenkins.scheme.org")
(log-directives "jenkins.scheme.org")
(block "location /"
"proxy_pass http://localhost:8080;"
"proxy_set_header Host $host;"
"proxy_set_header X-Real-IP $remote_addr;"
"proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;")))))
(define (write-tuonela-nginx-conf)
(write-host-nginx-conf
"tuonela.scheme.org"
"tuonela"
(list
(let ((server "tuonela.scheme.org"))
(https-server (list server)
(log-directives server)
"root /production/server/www;"))
;; (https-server
;; '("api.scheme.org")
;; "access_log /var/log/nginx/api.scheme.org_access.log;"
;; "error_log /var/log/nginx/api.scheme.org_error.log;"
;; (block "location /"
;; "proxy_pass http://127.0.0.1:4090;"
;; (apply block "if ($request_method = 'OPTIONS')"
;; (append cors
;; (list "add_header 'Access-Control-Max-Age' 1728000;"
;; "add_header 'Content-Type' 'text/plain; charset=utf-8';"
;; "add_header 'Content-Length' 0;"
;; "return 204;")))
;; (apply block "if ($request_method = 'POST')"
;; (append cors
;; (list "add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';")))
;; (apply block "if ($request_method = 'GET')"
;; (append cors
;; (list "add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';")))))
;; (https-server
;; '("api.staging.scheme.org")
;; "access_log /staging/api/log/nginx/access.log;"
;; "error_log /staging/api/log/nginx/error.log;"
;; (block "location /"
;; "proxy_pass http://127.0.0.1:7090;"))
(static-site "planet")
(static-site "books")
(static-site "chat")
(static-site "community")
(static-site "cookbook")
(static-site "docs")
(https-server
'("docs.staging.scheme.org")
(log-directives "docs.staging.scheme.org")
"root /staging/docs/www;")
(https-server
'("get.staging.scheme.org")
(log-directives "get.staging.scheme.org")
"root /staging/get/www;")
(static-site
"index"
(block "location /"
"try_files $uri $uri/ /index.html =404;"))
(https-server
'("index.staging.scheme.org")
(log-directives "index.staging.scheme.org")
"root /staging/index/www;"
(block "location /"
"try_files $uri $uri/ /index.html =404;"))
(static-site "man"
"include /etc/nginx/mime.types;"
(block "types"
"text/html 1;"
"text/html 3scm;"
"text/html 7scm;")
(block "location /raw"
"include /etc/nginx/mime.types;"
(block "types"
"text/plain 1;"
"text/plain 3scm;"
"text/plain 7scm;")))
(static-site "registry")
(static-site "persist")
(static-site "comm")
(static-site "test")
(static-site "web")
(static-site "files")
(static-site "conservatory")
(static-site "containers")
(static-site "events")
(static-site
"get"
(block "location /v2/"
"proxy_pass http://localhost:4050;"
"proxy_set_header Host $host;"
"proxy_set_header X-Real-IP $remote_addr;"
"proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;"))
(static-site "groups")
(block "map $research_scheme_source $research_scheme_target"
"include /production/research/nginx/map.conf;")
(static-site
"research"
(block "location ~ ^/([a-z][a-z0-9]*\.pdf)$"
"set $research_scheme_source $1;"
(block "if ($research_scheme_target)"
"return 307 $research_scheme_target;")))
(static-site "standards")
(parameterize ((content-security-policy '()))
(static-site
"try"
"gzip on;"
"gzip_comp_level 6;"
"gzip_types application/javascript;"))
(static-site "video")
(parameterize ((content-security-policy
(alist-change (content-security-policy)
"script-src"
'("'self'"
"'unsafe-inline'"
"'unsafe-eval'"))))
(https-server
'("gitea.scheme.org")
(log-directives "gitea.scheme.org")
(block "location /"
"proxy_pass http://localhost:4040;"
"proxy_set_header Host $host;"
"proxy_set_header X-Real-IP $remote_addr;"
"proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;"
"client_max_body_size 1G;")))
(block "map $go_scheme_source $go_scheme_target"
"include /production/go/nginx/map.conf;")
(static-site
"go"
(block "location ~ ^/([a-z0-9][a-z0-9-]*)$"
"set $go_scheme_source $1;"
(block "if ($go_scheme_target)"
"return 301 $go_scheme_target;")))
(https-server
'("wiki.staging.scheme.org")
(log-directives "wiki.staging.scheme.org")
(block "location /"
"proxy_pass http://localhost:7070;"
"proxy_set_header Host $host;"
"proxy_set_header X-Real-IP $remote_addr;"
"proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;"))
(https-server
'("www.schemeworkshop.org" "schemeworkshop.org")
(log-directives "www.schemeworkshop.org")
"root /production/workshop/www;")
(https-server
'("www.staging.schemeworkshop.org")
(log-directives "www.staging.schemeworkshop.org")
"root /staging/workshop/www;")
;;
(http-redirect-only-server
"blog.scheme.org" "https://planet.scheme.org/")
(http-redirect-only-server
"doc.scheme.org" "https://docs.scheme.org/")
(http-redirect-only-server
"play.scheme.org" "https://try.scheme.org/"))))
(write-jenkins-nginx-conf)
(write-tuonela-nginx-conf)