Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Issues 30 loadbalancer #35

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [

[package]
name = "ngx"
version = "0.3.0-beta"
version = "0.4.0-beta"
edition = "2021"
autoexamples = false
categories = ["api-bindings", "network-programming"]
Expand Down
7 changes: 5 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ name = "awssig"
path = "awssig.rs"
crate-type = ["cdylib"]



[[example]]
name = "httporigdst"
path = "httporigdst.rs"
crate-type = ["cdylib"]
required-features = ["linux"]

[[example]]
name = "upstream"
path = "upstream.rs"
crate-type = ["cdylib"]

[features]
linux = []
73 changes: 72 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
- [Embedded Variables](#embedded-variables)
- [Usage](#usage)
- [Caveats](#caveats)
- [UPSTREAM - Example upstream module for HTTP](#upstream---example-upstream-module-for-http)
- [Attributions](#attributions)
- [Example Configuration](#example-configuration)
- [HTTP](#http)
- [Usage](#usage)


# Examples
Expand Down Expand Up @@ -98,7 +103,7 @@ The following embedded variables are provided:

1. Clone the git repository.
```
https://github.com/nginxinc/ngx-rust
git clone git@github.com:nginxinc/ngx-rust.git
```

2. Compile the module from the cloned repo.
Expand Down Expand Up @@ -150,3 +155,69 @@ The following embedded variables are provided:
### Caveats

This module only supports IPv4.

## UPSTREAM - Example upstream module for HTTP

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.

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.

### Attributions

This module was converted from https://github.com/gabihodoroaga/nginx-upstream-module.

### Example Configuration
#### HTTP

```nginx configuration
load_module "modules/upstream.so"

http {
upstream backend {
server localhost:8081;

custom 32;
}

server {
listen 8080;
server_name _;

location / {
proxy_pass http://backend;
}
}
}
```

### Usage

1. Clone the git repository.
```
git clone git@github.com:nginxinc/ngx-rust.git
```

2. Compile the module from the cloned repo.
```
cd ${CLONED_DIRECTORY}/ngx-rust
cargo build --package=examples --example=upstream
```

3. Copy the shared object to the modules directory, /etc/nginx/modules.
```
cp ./target/debug/examples/libupstream.so /etc/nginx/modules
```

4. Add the `load_module` directive to your configuration.
```
load_module "modules/libupstream.so";
```

5. Add the example `server` and `upstream` block from the example above.

6. Reload NGINX.
```
nginx -t && nginx -s reload
```

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).
Loading