Skip to content

Commit c6ff14c

Browse files
author
Obada Haddad
committed
replace caddy with nginx allowing us to route all connection through it (rabbit,minio etc)
1 parent 46a62cc commit c6ff14c

15 files changed

Lines changed: 318 additions & 30 deletions

.env_circleci

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ DB_PORT=5432
88

99
RABBITMQ_DEFAULT_USER=rabbit-username
1010
RABBITMQ_DEFAULT_PASS=rabbit-password-you-should-change
11+
RABBITMQ_MANAGEMENT_PORT=15672
1112
RABBITMQ_PORT=5672
1213
RABBITMQ_HOST=rabbit
1314
WORKER_CONNECTION_TIMEOUT=100000000 # milliseconds
1415

1516
FLOWER_BASIC_AUTH=root:password-you-should-change
16-
17+
FLOWER_PUBLIC_PORT=5555
1718
DJANGO_SETTINGS_MODULE=settings.test
1819

1920
# Minio local storage example
@@ -28,11 +29,17 @@ AWS_SECRET_ACCESS_KEY=testsecret
2829
AWS_STORAGE_BUCKET_NAME=public
2930
AWS_STORAGE_PRIVATE_BUCKET_NAME=private
3031
# NOTE! port 9000 here should match $MINIO_PORT
31-
AWS_S3_ENDPOINT_URL=http://172.17.0.1:9000/
32+
AWS_S3_ENDPOINT_URL=http://minio:9000/
3233
AWS_QUERYSTRING_AUTH=False
3334
DJANGO_SUPERUSER_PASSWORD=codabench
3435
DJANGO_SUPERUSER_EMAIL=test@test.com
3536
DJANGO_SUPERUSER_USERNAME=codabench
36-
DOMAIN_NAME=localhost:80
3737
TLS_EMAIL=your@email.com
38-
SUBMISSIONS_API_URL=http://django:8000/api
38+
SUBMISSIONS_API_URL=http://django:8000/api
39+
40+
# -----------------------------------------------------------------------------
41+
# Nginx settings
42+
# -----------------------------------------------------------------------------
43+
HTTPS=False
44+
RATE_LIMIT=100
45+
DOMAIN_NAME=localhost

.env_sample

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SECRET_KEY=change-this-secret
22

33
# For local setup and debug
4-
DEBUG=True
4+
DEBUG=False
55

66
# Database
77
DB_HOST=db
@@ -15,8 +15,13 @@ ALLOWED_HOSTS=localhost,example.com
1515
SUBMISSIONS_API_URL=http://django:8000/api
1616
MAX_EXECUTION_TIME_LIMIT=600 # time limit for the default queue (in seconds)
1717

18-
# Local domain definition
19-
DOMAIN_NAME=localhost:80
18+
# -----------------------------------------------------------------------------
19+
# Nginx settings
20+
# -----------------------------------------------------------------------------
21+
HTTPS=False
22+
RATE_LIMIT=5
23+
DOMAIN_NAME=localhost
24+
2025

2126
# SSL style domain definition
2227
TLS_EMAIL=your@email.com
@@ -28,9 +33,6 @@ RABBITMQ_DEFAULT_PASS=rabbit-password-you-should-change
2833
RABBITMQ_MANAGEMENT_PORT=15672
2934
RABBITMQ_PORT=5672
3035
WORKER_CONNECTION_TIMEOUT=100000000 # milliseconds
31-
#RABBITMQ_HTTP_PROXY=http://proxy-example:3128
32-
#RABBITMQ_HTTPS_PROXY=http://proxy-example:3128
33-
#RABBITMQ_NO_PROXY=localhost,172.0.0.0/8
3436

3537
FLOWER_PUBLIC_PORT=5555
3638

docker-compose.yml

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,37 @@ services:
22
#----------------------------------------------------------------------------------------------------
33
# Web Services
44
#----------------------------------------------------------------------------------------------------
5-
caddy:
6-
image: caddy:2.11.1
5+
nginx:
6+
image: nginx:latest
77
env_file: .env
88
environment:
9-
- ACME_AGREE=true
9+
- NGINX_ENVSUBST_OUTPUT_DIR=/etc/nginx
10+
command: ["nginx", "-g", "daemon off;"]
1011
volumes:
11-
- ./Caddyfile:/etc/caddy/Caddyfile
1212
- ./src/staticfiles:/var/www/django/static
13-
- ./caddy_data:/data
14-
- ./caddy_config:/config
15-
- ./var/log/caddy:/var/log/
1613
- ./maintenance_mode/:/srv
14+
- ./certsNginx:/var/cache/nginx/acme-letsencrypt
15+
- ./nginx/:/etc/nginx/templates/
16+
- ./var/log/nginx/:/var/log/nginx
1717
restart: unless-stopped
1818
ports:
1919
- 80:80
2020
- 443:443
21+
- 8000:8000
22+
- 5432:5432
23+
- ${RABBITMQ_MANAGEMENT_PORT:-15672}:15672
24+
- ${RABBITMQ_PORT}:5672
25+
- ${MINIO_PORT}:9000
26+
- ${FLOWER_PUBLIC_PORT:-5555}:5555
2127
depends_on:
2228
- django
29+
- minio
30+
- rabbit
31+
- site_worker
32+
- db
33+
networks:
34+
- frontend
35+
- backend
2336

2437
django:
2538
container_name: django
@@ -39,7 +52,7 @@ services:
3952
- ./var/logs:/app/logs
4053
restart: unless-stopped
4154
ports:
42-
- 8000:8000
55+
- 8000
4356
depends_on:
4457
- db
4558
- rabbit
@@ -50,6 +63,8 @@ services:
5063
options:
5164
max-size: "20m"
5265
max-file: "5"
66+
networks:
67+
- backend
5368

5469

5570
#----------------------------------------------------------------------------------------------------
@@ -62,12 +77,14 @@ services:
6277
- ./var/minio:/export
6378
restart: unless-stopped
6479
ports:
65-
- $MINIO_PORT:9000
80+
- 9000
6681
env_file: .env
6782
healthcheck:
6883
test: ["CMD", "curl", "-I", "http://minio:9000/minio/health/live"]
6984
interval: 5s
7085
retries: 5
86+
networks:
87+
- backend
7188
createbuckets:
7289
image: minio/mc:RELEASE.2025-07-21T05-28-08Z
7390
depends_on:
@@ -92,6 +109,8 @@ services:
92109
fi;
93110
exit 0;
94111
"
112+
networks:
113+
- backend
95114

96115
#----------------------------------------------------------------------------------------------------
97116
# Local development helper, rebuilds RiotJS/Stylus on change
@@ -109,7 +128,6 @@ services:
109128
max-size: "20m"
110129
max-file: "5"
111130

112-
113131
#----------------------------------------------------------------------------------------------------
114132
# Database Service
115133
#
@@ -125,7 +143,7 @@ services:
125143
- POSTGRES_PASSWORD=${DB_PASSWORD}
126144
command: ["postgres", "-c", "log_statement=all", "-c", "log_destination=stderr", "-c", "config_file=/etc/postgresql/postgresql.conf"]
127145
ports:
128-
- 5432:5432
146+
- 5432
129147
volumes:
130148
- ./var/postgres:/var/lib/postgresql/18/:delegated
131149
- ./backups:/app/backups
@@ -135,6 +153,8 @@ services:
135153
options:
136154
max-size: "20m"
137155
max-file: "5"
156+
networks:
157+
- backend
138158

139159
#----------------------------------------------------------------------------------------------------
140160
# Rabbitmq & Flower monitoring tool
@@ -150,20 +170,18 @@ services:
150170
# containers being destroyed..!
151171
hostname: rabbit
152172
env_file: .env
153-
environment:
154-
- http_proxy=${RABBITMQ_HTTP_PROXY}
155-
- https_proxy=${RABBITMQ_HTTPS_PROXY}
156-
- no_proxy=${RABBITMQ_NO_PROXY}
157173
ports:
158-
- ${RABBITMQ_MANAGEMENT_PORT:-15672}:15672
159-
- ${RABBITMQ_PORT}:5672
174+
- 15672
175+
- 5672
160176
volumes:
161177
- ./var/rabbit:/var/lib/rabbitmq
162178
restart: unless-stopped
163179
logging:
164180
options:
165181
max-size: "20m"
166182
max-file: "5"
183+
networks:
184+
- backend
167185

168186
flower:
169187
container_name: flower
@@ -173,13 +191,15 @@ services:
173191
- CELERY_BROKER_URL=pyamqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@${RABBITMQ_HOST}:${RABBITMQ_PORT}//
174192
restart: unless-stopped
175193
ports:
176-
- ${FLOWER_PUBLIC_PORT:-5555}:5555
194+
- 5555
177195
depends_on:
178196
- rabbit
179197
logging:
180198
options:
181199
max-size: "20m"
182200
max-file: "5"
201+
networks:
202+
- backend
183203

184204
#----------------------------------------------------------------------------------------------------
185205
# Redis
@@ -188,12 +208,14 @@ services:
188208
container_name: redis
189209
image: redis
190210
ports:
191-
- 6379:6379
211+
- 6379
192212
restart: unless-stopped
193213
logging:
194214
options:
195215
max-size: "20m"
196216
max-file: "5"
217+
networks:
218+
- backend
197219

198220
#----------------------------------------------------------------------------------------------------
199221
# Celery Service
@@ -221,6 +243,8 @@ services:
221243
# Limit memory substantially here so we see any problems that may
222244
# appear on Heroku ahead of time
223245
memory: 256M
246+
networks:
247+
- backend
224248

225249
compute_worker:
226250
command: ["celery -A compute_worker worker -l info -Q compute-worker -n compute-worker@%n"]
@@ -249,3 +273,10 @@ services:
249273
options:
250274
max-size: "20m"
251275
max-file: "5"
276+
networks:
277+
- backend
278+
279+
networks:
280+
frontend:
281+
backend:
282+
internal: true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Rate limit error code (429) and definition (allow for bursts of 25 requests to load static images etc) and block immediately after the rate limit is applied to an IP
2+
limit_req_status 429;
3+
limit_req zone=scraperlimit burst=25 nodelay;
4+
5+
location /robots.txt {
6+
autoindex off;
7+
alias /etc/nginx/templates/robots.txt;
8+
}
9+
10+
# Deny access to bots
11+
# Block user agents that tend to be scrapers and badly behaved bots
12+
if ($bad_bot = 1) {
13+
return 444 "? beep boop ?";
14+
}
15+
16+
# No scrapers
17+
if ($scraper = 1) {
18+
return 418 "?";
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
charset utf-8;
2+
client_max_body_size 10480m;
3+
client_body_buffer_size 32m;
4+
sendfile on;
5+
gzip on;
6+
7+
location ~ /static/(.*) {
8+
autoindex off;
9+
access_log off;
10+
include /etc/nginx/mime.types;
11+
root /var/www/django/;
12+
}
13+
location ~ /media/(.*) {
14+
autoindex off;
15+
access_log off;
16+
include /etc/nginx/mime.types;
17+
root /var/www/django;
18+
}
19+
location /favicon.ico {
20+
autoindex off;
21+
access_log off;
22+
alias /var/www/django/static/img/favicon.ico;
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
limit_req_zone $binary_remote_addr zone=scraperlimit:10m rate=${RATE_LIMIT}r/s;
2+
3+
# Block bad bots
4+
map $http_user_agent $bad_bot {
5+
default 0;
6+
~*(?i)(JikeSpider) 1;
7+
~*(?i)(proximic) 1;
8+
~*(?i)(Sosospider) 1;
9+
~*(?i)(Baiduspider) 1;
10+
~*(?i)(Twitterbot) 1;
11+
~*(?i)(SemrushBot) 1;
12+
~*(?i)(^AIBOT) 1;
13+
~*(?i)(^BunnySlippers) 1;
14+
~*(?i)(^Cegbfeieh) 1;
15+
~*(?i)(^CheeseBot) 1;
16+
}
17+
18+
# Block scrappers
19+
map $http_user_agent $scraper {
20+
default 0;
21+
~*(?i)(Google-Extended) 1;
22+
~*(?i)(Applebot-Extended) 1;
23+
~*(?i)(anthropic-ai) 1;
24+
~*(?i)(ClaudeBot) 1;
25+
~*(?i)(Claude-Web) 1;
26+
~*(?i)(GPTBot) 1;
27+
~*(?i)(Omgili) 1;
28+
~*(?i)(FacebookBot) 1;
29+
~*(?i)(node-fetch) 1;
30+
~*(?i)(Timpibot) 1;
31+
# If you don't provide a User-Agent, you can go away
32+
~*(^-$) 1;
33+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error_page 503 @maintenance;
2+
location @maintenance {
3+
root /srv;
4+
try_files $uri /maintenance.html =503;
5+
}

nginx/http/flower.conf.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
server {
2+
listen ${FLOWER_PUBLIC_PORT};
3+
location / {
4+
proxy_pass http://flower:5555;
5+
}
6+
7+
}

nginx/http/minio.conf.template

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
server {
2+
listen ${MINIO_PORT};
3+
# To allow special characters in headers
4+
ignore_invalid_headers off;
5+
# Allow any size file to be uploaded.
6+
# Set to a value such as 1000m; to restrict file size to a specific value
7+
client_max_body_size 0;
8+
# To disable buffering
9+
proxy_buffering off;
10+
proxy_request_buffering off;
11+
12+
location / {
13+
# Default is HTTP/1, keepalive is only enabled in HTTP/1.1
14+
proxy_http_version 1.1;
15+
proxy_set_header Connection "";
16+
chunked_transfer_encoding off;
17+
18+
proxy_pass http://minio:9000;
19+
}
20+
21+
location /console/ {
22+
rewrite ^/console/(.*)$ /$1 break;
23+
24+
# To support websocket
25+
proxy_http_version 1.1;
26+
proxy_set_header Upgrade $http_upgrade;
27+
proxy_set_header Connection "Upgrade";
28+
29+
chunked_transfer_encoding off;
30+
proxy_pass http://minio;
31+
}
32+
}

0 commit comments

Comments
 (0)