Skip to content

Commit 840d789

Browse files
f5yacobucciMatthew Yacobucci
and
Matthew Yacobucci
authored
feat: (2 of 2) Upstream module example (#37)
* feat: Upstream module example Upstream example - derived from github.com/gabihodoroaga/nginx-upstream-module and NGINX ngx_http_upstream_keepalive_module.c Simply replaces peer functions with passthrough versions, i.e., proxies traffic through this module to originally configured upstream functions. Useful as a guide and example, but does not add load balancing algorithms. --------- Co-authored-by: Matthew Yacobucci <yacobuci@gmail.com>
1 parent 8b3a119 commit 840d789

File tree

4 files changed

+461
-3
lines changed

4 files changed

+461
-3
lines changed

Diff for: examples/Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ name = "awssig"
2222
path = "awssig.rs"
2323
crate-type = ["cdylib"]
2424

25-
26-
2725
[[example]]
2826
name = "httporigdst"
2927
path = "httporigdst.rs"
3028
crate-type = ["cdylib"]
3129
required-features = ["linux"]
3230

31+
[[example]]
32+
name = "upstream"
33+
path = "upstream.rs"
34+
crate-type = ["cdylib"]
35+
3336
[features]
3437
linux = []

Diff for: examples/README.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This crate provides a couple of example using [ngx](https://crates.io/crates/ngx
1616
- [awssig.rs](./awssig.rs) - An example of NGINX dynamic module that can sign GET request using AWS Signature v4.
1717
- [curl](./curl.rs) - An example of the Access Phase NGINX dynamic module that blocks HTTP requests if `user-agent` header starts with `curl`.
1818
- [httporigdst](./httporigdst.rs) - A dynamic module recovers the original IP address and port number of the destination packet.
19+
- [upstream](./upstream.rs) - A dynamic module demonstrating the setup code to write an upstream filter or load balancer.
1920

2021
To build all these examples simply run:
2122

@@ -98,7 +99,7 @@ The following embedded variables are provided:
9899

99100
1. Clone the git repository.
100101
```
101-
https://github.com/nginxinc/ngx-rust
102+
git clone git@github.com:nginxinc/ngx-rust.git
102103
```
103104

104105
2. Compile the module from the cloned repo.
@@ -150,3 +151,76 @@ The following embedded variables are provided:
150151
### Caveats
151152

152153
This module only supports IPv4.
154+
155+
## UPSTREAM - Example upstream / load balancing module for HTTP
156+
157+
This module simply proxies requests through a custom load balancer to the previously configured balancer. This is for demonstration purposes only. As a module writer, you can start with this structure and adjust to your needs, then implement the proper algorithm for your usage.
158+
159+
The module replaces the `peer` callback functions with its own, logs, and then calls through to the originally saved `peer` functions. This may look confusing at first, but rest assured, it's intentionally not implementing an algorithm of its own.
160+
161+
### Attributions
162+
163+
This module was converted from https://github.com/gabihodoroaga/nginx-upstream-module and also highly inspired by the same techniques used in NGINX source: `ngx_http_upstream_keepalive_module.c`.
164+
165+
### Example Configuration
166+
#### HTTP
167+
168+
```nginx configuration
169+
load_module "modules/upstream.so"
170+
171+
http {
172+
upstream backend {
173+
server localhost:15501;
174+
custom 32;
175+
}
176+
177+
server {
178+
listen 15500;
179+
server_name _;
180+
181+
location / {
182+
proxy_pass http://backend;
183+
}
184+
}
185+
186+
server {
187+
listen 15501;
188+
189+
location / {
190+
return 418;
191+
}
192+
}
193+
}
194+
```
195+
196+
### Usage
197+
198+
1. Clone the git repository.
199+
```
200+
git clone git@github.com:nginxinc/ngx-rust.git
201+
```
202+
203+
2. Compile the module from the cloned repo.
204+
```
205+
cd ${CLONED_DIRECTORY}/ngx-rust
206+
cargo buile --package=examples --example=upstream
207+
```
208+
209+
3. Copy the shared object to the modules directory, /etc/nginx/modules.
210+
```
211+
cp ./target/debug/examples/libupstream.so /etc/nginx/modules
212+
```
213+
214+
4. Add the `load_module` directive to your configuration.
215+
```
216+
load_module "modules/libupstream.so";
217+
```
218+
219+
5. Add the example `server` and `upstream` block from the example above.
220+
221+
6. Reload NGINX.
222+
```
223+
nginx -t && nginx -s reload
224+
```
225+
226+
7. Test with `curl`. Traffic should pass to your listener on port 8081 (this could be another NGINX server for example). With debug logging enabled you should notice the upstream log messages (see the source code for log examples, prefixed with "CUSTOM UPSTREAM").

Diff for: examples/upstream.conf

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# example configuration block to test upstream.rs
2+
http {
3+
upstream backend {
4+
server localhost:15501;
5+
custom 32;
6+
}
7+
8+
server {
9+
listen 15500;
10+
server_name _;
11+
12+
location / {
13+
proxy_pass http://backend;
14+
}
15+
}
16+
17+
server {
18+
listen 15501;
19+
20+
location / {
21+
return 418;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)