This is an NGINX module to check for a valid JWT.
Inspired by TeslaGov, ch1bo and tizpuppi, this module intend to be as light as possible and to remain simple.
- Docker image based on the official nginx Dockerfile (alpine).
- Light image (~16MB).
# nginx.conf
load_module /usr/lib/nginx/modules/ngx_http_auth_jwt_module.so;
# server.conf
server {
auth_jwt_key "0123456789abcdef" hex; # Your key as hex string
auth_jwt off;
location /secured-by-cookie/ {
auth_jwt $cookie_MyCookieName;
}
location /secured-by-auth-header/ {
auth_jwt on;
}
location /secured-by-auth-header-too/ {
auth_jwt_key "another-secret"; # Your key as utf8 string
auth_jwt on;
}
location /secured-by-rsa-key/ {
auth_jwt_key /etc/keys/rsa-public.pem file; # Your key from a PEM file
auth_jwt on;
}
location /secured-by-claim/ {
auth_jwt_claim claim_name claim_value;
auth_jwt on;
}
location /secured-by-claim-variable/ {
auth_jwt_claim claim_name $variable;
auth_jwt on;
}
location /not-secure/ {}
}
Note: don't forget to load the module in the main context:
load_module /usr/lib/nginx/modules/ngx_http_auth_jwt_module.so;
Syntax: auth_jwt $variable | on | off;
Default: auth_jwt off;
Context: http, server, location
Enables validation of JWT.
Syntax: auth_jwt_key value [encoding];
Default: ——
Context: http, server, location
Specifies the key for validating JWT signature (must be hexadecimal).
The encoding otpion may be hex | utf8 | base64 | file
(default is utf8
).
The file
option requires the value to be a valid file path (pointing to a PEM encoded key).
Syntax: auth_jwt_alg any | HS256 | HS384 | HS512 | RS256 | RS384 | RS512 | ES256 | ES384 | ES512;
Default: auth_jwt_alg any;
Context: http, server, location
Specifies which algorithm the server expects to receive in the JWT.
Syntax: auth_jwt_claim key value;
Context: http, server, location
Specifies Jwt must have this claim. This config can be used multiple times.
Image is generated with Github Actions (see nginx-jwt-module:latest)
docker pull ghcr.io/max-lt/nginx-jwt-module:latest
FROM ghcr.io/max-lt/nginx-jwt-module:latest
# Copy you nginx conf
# Don't forget to include this module in your configuration
# load_module /usr/lib/nginx/modules/ngx_http_auth_jwt_module.so;
COPY my-nginx-conf /etc/nginx
EXPOSE 8000
STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]
This module is built inside a docker container, from the nginx-alpine image.
make build # Will create a "jwt-nginx" image
# or
docker build -f Dockerfile -t jwt-nginx .
make test # Will build a test image & run test suite