-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhttp-reverse-proxy.ae
More file actions
60 lines (54 loc) · 1.9 KB
/
Copy pathhttp-reverse-proxy.ae
File metadata and controls
60 lines (54 loc) · 1.9 KB
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
// Reverse-proxy demo — std.http.proxy.
//
// Forwards every request to a single upstream HTTP server. The
// proxy strips Hop-by-Hop headers, adds X-Forwarded-{For,Proto,
// Host} + Via, rewrites Host: to the upstream's host, and returns
// the upstream's response verbatim (status, headers, body).
//
// Run two processes:
//
// # 1) Start an upstream (any HTTP server). For demo purposes
// # we use python3's stdlib server:
// python3 -m http.server 9000 --directory /tmp
//
// # 2) Build + run this example:
// ae build examples/stdlib/http-reverse-proxy.ae -o /tmp/proxy
// /tmp/proxy
//
// # 3) Test:
// curl http://localhost:8080/ # served by python
// curl -H 'X-Forwarded-For: 1.2.3.4' http://localhost:8080/
// # → upstream sees X-Forwarded-For: 1.2.3.4, <client_ip>
//
// For the production pool shape (load balancing, health checks,
// cache, circuit breaker), see docs/http-reverse-proxy.md.
import std.http
import std.http.proxy
const PORT = 8080
main() {
server = http.server_create(PORT)
if server == 0 {
println("server_create failed")
return
}
// Keep-alive so curl --http1.1 can reuse the TCP connection
// across multiple proxied requests.
ka_err = http.server_set_keepalive(server, 1, 0, 30000ms)
if ka_err != "" {
println("keepalive: ${ka_err}")
return
}
// Single upstream, RR (no other to balance against), 30s
// request timeout, default opts (XFF + XFP + XFH on; Host
// rewritten to upstream; 8 MiB body cap).
proxy_err = proxy.mount_simple(server, "/", "http://localhost:9000", 30)
if proxy_err != "" {
println("mount_simple: ${proxy_err}")
return
}
println("listening on http://localhost:${PORT}/ (forwarding to localhost:9000)")
err = http.server_start(server)
if err != "" {
println("server_start: ${err}")
}
}