Skip to content

Commit 069d866

Browse files
committed
refactor: update Dockerfile to use nginx-tsuru and enhance nginx configuration with rate limiting
1 parent f356cda commit 069d866

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

Dockerfile.nginx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
FROM alpine:3.21.2 as libjwt-builder
1+
FROM tsuru/nginx-tsuru:1.26.3-main
22

3-
WORKDIR /home/app
4-
RUN apk add --no-cache git cmake make gcc g++ jansson-dev openssl-dev
5-
RUN git clone https://github.com/benmcollins/libjwt.git && \
6-
cd libjwt && \
7-
git checkout 8ac4200
3+
COPY ./nginx.conf /etc/nginx/nginx.conf
4+
COPY ./lib/resty/libjwt /usr/local/lib/lua/5.1/resty/libjwt
85

9-
RUN mkdir -p /home/app/libjwt/build && \
10-
cd /home/app/libjwt/build && \
11-
cmake .. && make && make install
12-
13-
FROM openresty/openresty:1.27.1.1-1-alpine
14-
15-
COPY ./nginx.conf /usr/local/openresty/nginx/conf/nginx.conf
16-
COPY ./lib/resty/libjwt /usr/local/openresty/lualib/resty/libjwt
17-
18-
RUN apk add --no-cache jansson
19-
COPY --from=libjwt-builder /usr/local/lib/libjwt.so /usr/local/lib/libjwt.so
206
EXPOSE 8888
21-
CMD ["openresty", "-g", "daemon off;"]
7+
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
worker_processes 1;
22

3+
include modules/*.conf;
4+
35
events {
46
worker_connections 1024;
57
}
68

79
http {
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

Comments
 (0)