Skip to content

Commit 974a9eb

Browse files
committed
POC: Rename auth-request
1 parent 3245146 commit 974a9eb

File tree

5 files changed

+62
-32
lines changed

5 files changed

+62
-32
lines changed

exampleSite/content/njs/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ Below you will find NJS examples, which you can tweak and run in your browser.
1111

1212
- [Basic Example](basic-example) - Return "Hello!" for all requests
1313
- [Echo Server](echo-server) - Returns a `JSON` version of the request you just sent
14-
- [Magic AI](magic-ai-thing) - WIP - Something to do with AI
14+
- [Choose Upstream](choose-upstream) - Selects an upstream based on query param content

exampleSite/content/njs/auth-request.md

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
description: Choose Upstream
3+
title: Choose Upstream
4+
weight: 100
5+
---
6+
7+
In this example, when a client sends a request to the `/` endpoint, NGINX calls the `chooseUpstream` function. This function reads the token from the request's query string and then selects the appropriate backend server based on the token's value. If the token does not match a known value, the function returns a 404 error.
8+
9+
```nginx
10+
load_module modules/ngx_http_js_module.so;
11+
12+
http {
13+
js_import choose_upstream.js;
14+
15+
server {
16+
listen 80;
17+
18+
location /secure/ {
19+
auth_request /fetch_upstream;
20+
auth_request_set $backend $upstream_http_x_backend;
21+
22+
proxy_pass http://$backend;
23+
}
24+
25+
location /fetch_upstream {
26+
internal;
27+
28+
proxy_pass http://127.0.0.1:8079;
29+
proxy_pass_request_body off;
30+
proxy_set_header Content-Length "";
31+
proxy_set_header X-Original-URI $request_uri;
32+
}
33+
34+
}
35+
36+
server {
37+
listen 127.0.0.1:8079;
38+
39+
location / {
40+
js_content main.chooseUpstream;
41+
}
42+
43+
}
44+
45+
server {
46+
listen 127.0.0.1:8081;
47+
return 200 "BACKEND A:$uri\n";
48+
}
49+
50+
server {
51+
listen 127.0.0.1:8082;
52+
return 200 "BACKEND B:$uri\n";
53+
}
54+
55+
}
56+
```
57+
58+
59+
{{< njs-playground exampleNJS=chooseUpstream.js fnName=chooseUpstream >}}

exampleSite/static/njs/auth_request.js renamed to exampleSite/static/njs/chooseUpstream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function choose_upstream(r) {
1+
function chooseUpstream(r) {
22
let backend;
33

44
let uriParts = r.headersIn['X-Original-URI'].split('?');
@@ -30,4 +30,4 @@ function choose_upstream(r) {
3030
}
3131

3232
// uncomment for us in NJS
33-
// export default { choose_upstream };
33+
// export default { chooseUpstream };

exampleSite/static/njs/echo.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ function echo(r) {
99
requestText,
1010
} = r;
1111

12-
// Construct the echo response (JSON format)
1312
const response = {
1413
method,
1514
uri,

0 commit comments

Comments
 (0)