Skip to content

Commit 4e98614

Browse files
author
Matthew Yacobucci
committed
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.
1 parent 4ae3f5b commit 4e98614

File tree

3 files changed

+428
-3
lines changed

3 files changed

+428
-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

+67-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ The following embedded variables are provided:
9898

9999
1. Clone the git repository.
100100
```
101-
https://github.com/nginxinc/ngx-rust
101+
git clone git@github.com:nginxinc/ngx-rust.git
102102
```
103103

104104
2. Compile the module from the cloned repo.
@@ -150,3 +150,69 @@ The following embedded variables are provided:
150150
### Caveats
151151

152152
This module only supports IPv4.
153+
154+
## UPSTREAM - Example upstream / load balancing module for HTTP
155+
156+
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.
157+
158+
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.
159+
160+
### Attributions
161+
162+
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`.
163+
164+
### Example Configuration
165+
#### HTTP
166+
167+
```nginx configuration
168+
load_module "modules/upstream.so"
169+
170+
http {
171+
upstream backend {
172+
server localhost:8081;
173+
174+
custom 32;
175+
}
176+
177+
server {
178+
listen 8080;
179+
server_name _;
180+
181+
location / {
182+
proxy_pass http://backend;
183+
}
184+
}
185+
}
186+
```
187+
188+
### Usage
189+
190+
1. Clone the git repository.
191+
```
192+
git clone git@github.com:nginxinc/ngx-rust.git
193+
```
194+
195+
2. Compile the module from the cloned repo.
196+
```
197+
cd ${CLONED_DIRECTORY}/ngx-rust
198+
cargo buile --package=examples --example=upstream
199+
```
200+
201+
3. Copy the shared object to the modules directory, /etc/nginx/modules.
202+
```
203+
cp ./target/debug/examples/libupstream.so /etc/nginx/modules
204+
```
205+
206+
4. Add the `load_module` directive to your configuration.
207+
```
208+
load_module "modules/libupstream.so";
209+
```
210+
211+
5. Add the example `server` and `upstream` block from the example above.
212+
213+
6. Reload NGINX.
214+
```
215+
nginx -t && nginx -s reload
216+
```
217+
218+
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 "custom" log messages (see the source code for log examples).

0 commit comments

Comments
 (0)