11worker_processes 1;
22
3+ include modules/*.conf;
4+
35events {
46 worker_connections 1024 ;
57}
68
79http {
8- log_format mylog '$remote_addr - "$request"\tStatus: $status JWT-Subject: $jwt_sub JWT-Email: $jwt_email' ;
10+ limit_req_zone $jwt_email zone =one:10m rate=1r /s;
11+ limit_req_status 429 ;
12+ limit_req_log_level error;
13+
14+ log_format mylog '$remote_addr - "$request"\tStatus: $status JWT-Subject: $jwt_sub JWT-Email: $jwt_email RateLimit: $limit_req_status' ;
915 access_log /dev/stdout mylog;
1016 server {
1117 listen 8888 ;
@@ -22,22 +28,30 @@ http {
2228 location /private {
2329 access_by_lua_block {
2430 local libjwt = require( "resty.libjwt" )
25- local cjson = require( "cjson.safe" )
26- local token = libjwt.validate( {
31+ libjwt.validate( {
2732 jwks_files = { "/usr/share/tokens/jwks.json" } ,
2833 extract_claims = { "sub" , "email" } ,
2934 } )
30- if token then
31- local claim_str = cjson.encode( token) or "Invalid Token"
32- ngx.status = ngx.HTTP_OK
33- return ngx.say( claim_str)
34- end
35- ngx.status = ngx.HTTP_UNAUTHORIZED
36- local response = {
37- message = "Unauthorized"
38- }
39- return ngx.say( cjson.encode( response))
4035 }
36+
37+ echo 'private' ;
38+ }
39+
40+ location /private_limited {
41+ limit_req zone =one burst=1 nodelay;
42+ rewrite_by_lua_block {
43+ local libjwt = require( "resty.libjwt" )
44+ libjwt.validate( {
45+ jwks_files = { "/usr/share/tokens/jwks.json" } ,
46+ extract_claims = { "sub" , "email" } ,
47+ } )
48+ }
49+
50+ echo 'private+rate-limited' ;
51+ }
52+
53+ location /ratelimit-api {
54+ limit_req_rw_handler;
4155 }
4256 }
4357}
0 commit comments