Skip to content

Commit 4769ac3

Browse files
committed
feat: advanced example of proxy server
1 parent b7a22ec commit 4769ac3

File tree

7 files changed

+122
-109
lines changed

7 files changed

+122
-109
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ Manual configuration involves reviewing the following files so that they match y
161161
- Set a unique value for `$oidc_hmac_key` to ensure nonce values are unpredictable
162162
- If NGINX Plus is deployed behind another proxy or load balancer, modify the `map…$redirect_base` and `map…$proto` blocks to define how to obtain the original protocol and port number.
163163

164-
- **frontend.conf, frontend_backend_sample_v2.conf** - this of reverse proxy configuration
164+
- **frontend.conf** - this of reverse proxy configuration
165165

166166
- Modify the upstream group to match your backend site or app
167167
- Configure the preferred listen port and [enable SSL/TLS configuration](https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-http/)
@@ -170,8 +170,9 @@ Manual configuration involves reviewing the following files so that they match y
170170

171171
> Note:
172172
>
173-
> - Sample 1. frontend.conf: landing page starts OIDC flow without a login/logout button
174-
> - Sample 2. frontend_backend_sample_v2.conf
173+
> - 1. Basic Example. Landing page starts OIDC flow without a login button
174+
>
175+
> - 2. Advanced Example. Landing page, login/logout button to start/finish OIDC workflow
175176
> - Landing page with `login` button
176177
> - `login` button to start OIDC flow by validating `id token` with the JWK of IdP.
177178
> - Landing page calls the `/userinfo` endpoint to show user information by validating `access token` with the JWK of IdP.

docker/build-context/nginx/nginx.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ http {
2323
keepalive_timeout 65;
2424

2525
include conf.d/openid_connect_configuration.conf;
26-
include conf.d/frontend_backend_sample_v2.conf;
2726
include conf.d/frontend.conf;
2827
include sample/proxy_server_frontend.conf;
2928
include sample/proxy_server_backend.conf;

docs/01-oidc-local-test.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Update the NGINX Plus configuration file if you want. Otherwise, skip the follow
188188

189189
> Note:
190190
>
191-
> - In the [`frontend_backend_sample_v2.conf`](../frontend_backend_sample_v2.conf), you can add additional API endpoints like:
191+
> - In the [`frontend.conf`](../frontend.conf), you can add additional API endpoints like:
192192
>
193193
> ```nginx
194194
> location /v1/api/example {

frontend.conf

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# -----------------------------------------------------------------------------#
2+
# #
3+
# Sample Reverse Proxy Configuration: Frontend Site, Backend App #
4+
# (for Open ID Connect workflow) #
5+
# #
6+
# -----------------------------------------------------------------------------#
7+
8+
# -----------------------------------------------------------------------------#
9+
# #
10+
# 1. Basic Example: Landing page starts OIDC workflow w/o login/logout button. #
11+
# #
12+
# -----------------------------------------------------------------------------#
13+
114
# This is the backend application we are protecting with OpenID Connect
215
upstream my_backend {
316
zone my_backend 64k;
@@ -33,4 +46,104 @@ server {
3346
}
3447
}
3548

49+
# -----------------------------------------------------------------------------#
50+
# #
51+
# 2. Advanced Example: Landing page, login/logout button to handle OIDC kflow #
52+
# #
53+
# - Landing page shows 'login' button #
54+
# - 'login' button calls `/login` endpoint to start OIDC flow by validating
55+
# 'id_token' w/ IdP's JWK. #
56+
# - Landing page calls `/userinfo` to show user info using 'access_token`. #
57+
# - 'logout' button to be finished OIDC session by IdP. #
58+
# - API authorization by validating `access_token` w/ IdP's JWK #
59+
# #
60+
# -----------------------------------------------------------------------------#
61+
62+
#
63+
# Upstream server for proxing to the frontend site.
64+
# - This is a bundle frontend app to locally test NGINX Plus OIDC workflow.
65+
# + Sample: ./docker/build-context/nginx/sample/proxy_server_frontend.conf
66+
# - Modify this configuration to match your frontend site.
67+
#
68+
upstream my_frontend_site {
69+
zone my_frontend_site 64k;
70+
server 127.0.0.1:9091;
71+
}
72+
73+
#
74+
# Upstream sample for proxing to the backend API server.
75+
# - This is a bundle backend app to locally test an API using access token.
76+
# + Sample: ./docker/build-context/nginx/sample/proxy_server_backend.conf
77+
# - Modify this configuration to match your backend app.
78+
#
79+
upstream my_backend_app {
80+
zone my_backend_app 64k;
81+
server 127.0.0.1:9092;
82+
}
83+
84+
#
85+
# Sample Frontend-site & backend-api-server for the OIDC workflow.
86+
#
87+
server {
88+
# Enable when debugging is needed.
89+
error_log /var/log/nginx/error.log debug; # Reduce severity level as required
90+
access_log /var/log/nginx/access.log main;
91+
92+
# Replace the following server name with your host name.
93+
#
94+
# [Example: if you want to locally test OIDC in your laptop]
95+
# - Add '127.0.0.1 nginx.oidc.test` in your `/etc/hosts'.
96+
# - Use the command like 'make start'.
97+
# - Type 'https://nginx.oidc.test' in your browser.
98+
# - You will see the sample landing page and 'Sign In' button.
99+
#
100+
listen 8020; # Use SSL/TLS in production
101+
server_name nginx.oidc.test;
102+
103+
# Replace the following files with your certificate.
104+
ssl_certificate /etc/ssl/nginx/nginx-repo.crt;
105+
ssl_certificate_key /etc/ssl/nginx/nginx-repo.key;
106+
107+
# OIDC workflow
108+
include conf.d/openid_connect.server_conf;
109+
110+
#
111+
# Frontend example:
112+
#
113+
# - Default landing page: no need OIDC workflow to show 'Sign In' button.
114+
# - The site is protected with OpenID Connect(OIDC) by calling the API
115+
# endpoint of `/login` when users click 'login' button.
116+
#
117+
location / {
118+
proxy_pass http://my_frontend_site;
119+
access_log /var/log/nginx/access.log main_jwt;
120+
}
121+
122+
#
123+
# Backend API example to interact with proxied backend service:
124+
#
125+
# - This API resource is protected by access token which is received by IdP
126+
# after successful signing-in among the frontend site, NGINX Plus and IdP.
127+
#
128+
# - To ensure that client requests access the API securely, access token is
129+
# used for API authorization.
130+
# + Most of IdP generate an access token for API authorization of IdP's
131+
# endpoints (like /userinfo) as well as customer's endpoints.
132+
# + But Azure AD generate two types of access token for API authorization
133+
# of Microsoft graph API endpoints and customers' endpoints.
134+
# + Therefore, we recommend that you use $session_jwt for Azure AD and
135+
# $access_token for most of IdPs such as Cognito, Auth0, Keycloak, Okta,
136+
# OneLogin, Ping Identity, etc as for now.
137+
#
138+
location /v1/api/example {
139+
auth_jwt "" token=$access_token; # Use $session_jwt for Azure AD
140+
auth_jwt_key_request /_jwks_uri; # Enable when using URL
141+
#auth_jwt_key_file $oidc_jwt_keyfile; # Enable when using filename
142+
143+
proxy_set_header Authorization "Bearer $access_token";
144+
proxy_pass http://my_backend_app;
145+
access_log /var/log/nginx/access.log main_jwt;
146+
}
147+
}
148+
36149
# vim: syntax=nginx

frontend_backend_sample_v2.conf

Lines changed: 0 additions & 100 deletions
This file was deleted.

openid_connect.server_conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
proxy_ssl_server_name on; # For SNI to the IdP
8383
proxy_set_header Authorization "Bearer $access_token";
8484
proxy_pass $oidc_userinfo_endpoint;
85-
access_log /var/log/nginx/access.log oidc_jwt;
85+
access_log /var/log/nginx/access.log main_jwt;
8686
}
8787

8888
#
@@ -99,7 +99,7 @@
9999

100100
# Redirect to the the original URI of UI after successful login to IDP.
101101
js_content oidc.redirectPostLogin;
102-
access_log /var/log/nginx/access.log oidc_jwt;
102+
access_log /var/log/nginx/access.log main_jwt;
103103
}
104104

105105
#

openid_connect_configuration.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ map $host $post_logout_return_uri {
6363
# -> redirect to the '/' location block
6464
# ./docker/build-context/content/index.html
6565
#
66-
# default $redirect_base;
66+
default $redirect_base;
6767

6868
# Example 2: Redirect to a custom logout page
6969
# ./docker/build-context/nginx/sample/proxy_server_frontend.conf
7070
# -> redirect to the '/signout' location block
7171
# ./docker/build-context/content/signout.html
7272
#
73-
default $redirect_base/signout;
73+
# default $redirect_base/signout;
7474

7575
# Example 3: Redirect to an another URL
7676
# default https://www.nginx.com;

0 commit comments

Comments
 (0)