Skip to content

Commit 8081f84

Browse files
author
Matthew Yacobucci
committed
Code review notes and changes after rebasingx
1 parent 55807ea commit 8081f84

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

examples/README.md

Lines changed: 19 additions & 11 deletions
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

@@ -168,20 +169,27 @@ This module was converted from https://github.com/gabihodoroaga/nginx-upstream-m
168169
load_module "modules/upstream.so"
169170
170171
http {
171-
upstream backend {
172-
server localhost:8081;
172+
upstream backend {
173+
server localhost:15501;
174+
custom 32;
175+
}
176+
177+
server {
178+
listen 15500;
179+
server_name _;
173180
174-
custom 32;
175-
}
181+
location / {
182+
proxy_pass http://backend;
183+
}
184+
}
176185
177-
server {
178-
listen 8080;
179-
server_name _;
186+
server {
187+
listen 15501;
180188
181-
location / {
182-
proxy_pass http://backend;
189+
location / {
190+
return 418;
191+
}
183192
}
184-
}
185193
}
186194
```
187195

@@ -215,4 +223,4 @@ http {
215223
nginx -t && nginx -s reload
216224
```
217225

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

examples/upstream.conf

Lines changed: 24 additions & 0 deletions
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+
}

examples/upstream.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ use ngx::{
1414
ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin,
1515
ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, ngx_http_upstream_t, ngx_int_t, ngx_module_t,
1616
ngx_peer_connection_t, ngx_str_t, ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_UNSET, NGX_ERROR,
17-
NGX_HTTP_MODULE, NGX_HTTP_UPS_CONF, NGX_LOG_DEBUG_HTTP, NGX_LOG_EMERG, NGX_RS_HTTP_SRV_CONF_OFFSET,
18-
NGX_RS_MODULE_SIGNATURE,
17+
NGX_HTTP_MODULE, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG, NGX_RS_HTTP_SRV_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
1918
},
2019
http::{
2120
ngx_http_conf_get_module_srv_conf, ngx_http_conf_upstream_srv_conf_immutable,
2221
ngx_http_conf_upstream_srv_conf_mutable, HTTPModule, Merge, MergeConfigError, Request,
2322
},
24-
http_upstream_peer_init, ngx_log_debug_http, ngx_log_debug_mask, ngx_modules, ngx_null_command, ngx_string,
23+
http_upstream_init_peer_pt,
24+
log::DebugMask,
25+
ngx_log_debug_http, ngx_log_debug_mask, ngx_modules, ngx_null_command, ngx_string,
2526
};
2627
use std::{
2728
mem,
@@ -140,7 +141,7 @@ pub static mut ngx_http_upstream_custom_module: ngx_module_t = ngx_module_t {
140141
// http_upstream_init_custom_peer
141142
// The module's custom peer.init callback. On HTTP request the peer upstream get and free callbacks
142143
// are saved into peer data and replaced with this module's custom callbacks.
143-
http_upstream_peer_init!(
144+
http_upstream_init_peer_pt!(
144145
http_upstream_init_custom_peer,
145146
|request: &mut Request, us: *mut ngx_http_upstream_srv_conf_t| {
146147
ngx_log_debug_http!(request, "CUSTOM UPSTREAM request peer init");
@@ -194,7 +195,7 @@ unsafe extern "C" fn ngx_http_upstream_get_custom_peer(pc: *mut ngx_peer_connect
194195
let hcpd: *mut UpstreamPeerData = unsafe { mem::transmute(data) };
195196

196197
ngx_log_debug_mask!(
197-
NGX_LOG_DEBUG_HTTP,
198+
DebugMask::Http,
198199
(*pc).log,
199200
"CUSTOM UPSTREAM get peer, try: {}, conn: {:p}",
200201
(*pc).tries,
@@ -208,7 +209,7 @@ unsafe extern "C" fn ngx_http_upstream_get_custom_peer(pc: *mut ngx_peer_connect
208209
return rc;
209210
}
210211

211-
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*pc).log, "CUSTOM UPSTREAM end get peer");
212+
ngx_log_debug_mask!(DebugMask::Http, (*pc).log, "CUSTOM UPSTREAM end get peer");
212213
Status::NGX_OK.into()
213214
}
214215

@@ -221,15 +222,15 @@ unsafe extern "C" fn ngx_http_upstream_free_custom_peer(
221222
data: *mut c_void,
222223
state: ngx_uint_t,
223224
) {
224-
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*pc).log, "CUSTOM UPSTREAM free peer");
225+
ngx_log_debug_mask!(DebugMask::Http, (*pc).log, "CUSTOM UPSTREAM free peer");
225226

226227
let hcpd: *mut UpstreamPeerData = unsafe { mem::transmute(data) };
227228

228229
let original_free_peer = (*hcpd).original_free_peer.unwrap();
229230

230231
original_free_peer(pc, (*hcpd).data, state);
231232

232-
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*pc).log, "CUSTOM UPSTREAM end free peer");
233+
ngx_log_debug_mask!(DebugMask::Http, (*pc).log, "CUSTOM UPSTREAM end free peer");
233234
}
234235

235236
// ngx_http_upstream_init_custom
@@ -240,7 +241,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
240241
cf: *mut ngx_conf_t,
241242
us: *mut ngx_http_upstream_srv_conf_t,
242243
) -> ngx_int_t {
243-
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*cf).log, "CUSTOM UPSTREAM peer init_upstream");
244+
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM peer init_upstream");
244245

245246
let maybe_conf: Option<*mut SrvConfig> =
246247
ngx_http_conf_upstream_srv_conf_mutable(us, &ngx_http_upstream_custom_module);
@@ -273,7 +274,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
273274
(*hccf).original_init_peer = (*us).peer.init;
274275
(*us).peer.init = Some(http_upstream_init_custom_peer);
275276

276-
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*cf).log, "CUSTOM UPSTREAM end peer init_upstream");
277+
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM end peer init_upstream");
277278
isize::from(Status::NGX_OK)
278279
}
279280

@@ -286,7 +287,7 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom(
286287
cmd: *mut ngx_command_t,
287288
conf: *mut c_void,
288289
) -> *mut c_char {
289-
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*cf).log, "CUSTOM UPSTREAM module init");
290+
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM module init");
290291

291292
let mut ccf = &mut (*(conf as *mut SrvConfig));
292293

@@ -318,7 +319,7 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom(
318319

319320
(*uscf).peer.init_upstream = Some(ngx_http_upstream_init_custom);
320321

321-
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*cf).log, "CUSTOM UPSTREAM end module init");
322+
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM end module init");
322323
// NGX_CONF_OK
323324
std::ptr::null_mut()
324325
}
@@ -350,7 +351,7 @@ impl HTTPModule for Module {
350351

351352
(*conf).max = NGX_CONF_UNSET as u32;
352353

353-
ngx_log_debug_mask!(NGX_LOG_DEBUG_HTTP, (*cf).log, "CUSTOM UPSTREAM end create_srv_conf");
354+
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM end create_srv_conf");
354355
conf as *mut c_void
355356
}
356357
}

0 commit comments

Comments
 (0)