char/serve-esm

git clone https://git.t4t.associates/char/serve-esm

Charlotte SomRewrite serve-esm in Rust using Tower and Hypere06f6b7

main
1.3 KiB39 linesraw
1import ngx from "jsr:@char/ngx@^0.2.2";
2
3const DOMAIN = "esm.char.lt";
4const SOCKET = "/run/serve-esm/serve-esm.sock";
5const CACHE_ZONE = "serve_esm";
6const CACHE_PATH = "/var/cache/nginx/serve-esm";
7
8// proxy_cache_path must live in http {} context; emitted alongside the server block
9// and expected to be included from a file loaded at http scope.
10const cachePath =
11  `proxy_cache_path ${CACHE_PATH} levels=1:2 keys_zone=${CACHE_ZONE}:32m ` +
12  `max_size=4g inactive=30d use_temp_path=off;`;
13
14const config = ngx("server", [
15  [
16    ngx.serverName(DOMAIN),
17    ...ngx.listen(),
18    ...ngx.http3(),
19    ...ngx.letsEncrypt(DOMAIN),
20  ],
21  ngx("location /", [
22    `proxy_pass http://unix:${SOCKET}`,
23    "proxy_http_version 1.1",
24    "proxy_set_header Host $host",
25    "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for",
26    "proxy_set_header X-Forwarded-Proto $scheme",
27
28    `proxy_cache ${CACHE_ZONE}`,
29    "proxy_cache_key $request_uri",
30    "proxy_cache_valid 200 301 302 30d",
31    "proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504",
32    "proxy_cache_background_update on",
33    "proxy_cache_lock on",
34    "proxy_cache_revalidate on",
35    "add_header X-Cache-Status $upstream_cache_status",
36  ]),
37]);
38
39if (import.meta.main) console.log([cachePath, config.build()].join("\n"));