Skip to content

Commit 0ec4e74

Browse files
committed
wip: add nginx in docker-compose.yml
1 parent 89033e0 commit 0ec4e74

File tree

5 files changed

+139
-8
lines changed

5 files changed

+139
-8
lines changed

Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This part installs deps needed at runtime.
2-
FROM python:3.11-slim AS runtime
2+
FROM python:3.12-slim AS common
33

44
RUN apt-get update && \
55
apt-get install -y --no-install-recommends \
@@ -13,7 +13,7 @@ RUN apt-get update && \
1313
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
1414

1515
# This part adds deps needed only at buildtime.
16-
FROM runtime AS build
16+
FROM common AS build
1717

1818
RUN apt-get update && \
1919
apt-get install -y --no-install-recommends \
@@ -40,7 +40,7 @@ COPY . /srv/umap
4040

4141
RUN /venv/bin/pip install .[docker,s3,sync]
4242

43-
FROM runtime
43+
FROM common
4444

4545
COPY --from=build /srv/umap/docker/ /srv/umap/docker/
4646
COPY --from=build /venv/ /venv/

docker-compose.yml

+16-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ services:
2626
condition: service_healthy
2727
redis:
2828
condition: service_healthy
29-
image: umap/umap:3.0.0
30-
ports:
31-
- "${PORT-8000}:8000"
29+
image: umap/umap:3.0.2
3230
environment:
31+
- STATIC_ROOT=/srv/umap/static
32+
- MEDIA_ROOT=/srv/umap/uploads
3333
- DATABASE_URL=postgis://postgres@db/postgres
3434
- SECRET_KEY=some-long-and-weirdly-unrandom-secret-key
3535
- SITE_URL=https://umap.local/
@@ -39,7 +39,20 @@ services:
3939
- REDIS_URL=redis://redis:6379
4040
volumes:
4141
- data:/srv/umap/uploads
42+
- static:/srv/umap/static
43+
44+
proxy:
45+
image: nginx:latest
46+
ports:
47+
- "8000:80"
48+
volumes:
49+
- ./docker/nginx.conf:/etc/nginx/nginx.conf:ro
50+
- static:/static:ro
51+
- data:/data:ro
52+
depends_on:
53+
- app
4254

4355
volumes:
4456
data:
57+
static:
4558
db:

docker/nginx.conf

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
events {
2+
worker_connections 1024; # Adjust this to your needs
3+
}
4+
5+
6+
7+
http {
8+
proxy_cache_path /tmp/nginx_ajax_proxy_cache levels=1:2 keys_zone=ajax_proxy:10m inactive=60m;
9+
proxy_cache_key "$uri$is_args$args";
10+
11+
map $http_upgrade $connection_upgrade {
12+
default upgrade;
13+
'' close;
14+
}
15+
16+
types {
17+
application/javascript mjs;
18+
}
19+
20+
include mime.types;
21+
default_type application/octet-stream;
22+
sendfile on;
23+
keepalive_timeout 65;
24+
25+
# Server block
26+
server {
27+
listen 80;
28+
server_name localhost;
29+
30+
# Static file serving
31+
location /static/ {
32+
alias /static/;
33+
gzip on;
34+
gzip_vary on;
35+
gzip_proxied any;
36+
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
37+
expires 365d;
38+
access_log /dev/null;
39+
}
40+
41+
# Geojson files
42+
location /uploads/ {
43+
alias /data/;
44+
expires 30d;
45+
}
46+
47+
location /favicon.ico {
48+
alias /static/favicon.ico;
49+
}
50+
51+
# X-Accel-Redirect
52+
location /internal/ {
53+
internal;
54+
gzip_vary on;
55+
gzip_static on;
56+
add_header X-DataLayer-Version $upstream_http_x_datalayer_version;
57+
alias /data/;
58+
}
59+
60+
# Ajax proxy
61+
location ~ ^/proxy/(.*) {
62+
internal;
63+
add_header X-Proxy-Cache $upstream_cache_status always;
64+
proxy_cache_background_update on;
65+
proxy_cache_use_stale updating;
66+
proxy_cache ajax_proxy;
67+
proxy_cache_valid 1m; # Default. Umap will override using X-Accel-Expires
68+
set $target_url $1;
69+
# URL is encoded, so we need a few hack to clean it back.
70+
if ( $target_url ~ (.+)%3A%2F%2F(.+) ){ # fix :// between scheme and destination
71+
set $target_url $1://$2;
72+
}
73+
if ( $target_url ~ (.+?)%3A(.*) ){ # fix : between destination and port
74+
set $target_url $1:$2;
75+
}
76+
if ( $target_url ~ (.+?)%2F(.*) ){ # fix / after port, the rest will be decoded by proxy_pass
77+
set $target_url $1/$2;
78+
}
79+
resolver 8.8.8.8;
80+
add_header X-Proxy-Target $target_url; # For debugging
81+
proxy_pass_request_headers off;
82+
proxy_set_header Content-Type $http_content_type;
83+
proxy_set_header Content-Encoding $http_content_encoding;
84+
proxy_set_header Content-Length $http_content_length;
85+
proxy_read_timeout 10s;
86+
proxy_connect_timeout 5s;
87+
proxy_ssl_server_name on;
88+
proxy_pass $target_url;
89+
proxy_intercept_errors on;
90+
error_page 301 302 307 = @handle_proxy_redirect;
91+
}
92+
location @handle_proxy_redirect {
93+
resolver 8.8.8.8;
94+
set $saved_redirect_location '$upstream_http_location';
95+
proxy_pass $saved_redirect_location;
96+
}
97+
98+
# Proxy pass to ASGI server
99+
location / {
100+
proxy_pass http://app:8000;
101+
proxy_set_header Host $host;
102+
proxy_set_header X-Real-IP $remote_addr;
103+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
104+
proxy_set_header X-Forwarded-Proto $scheme;
105+
proxy_set_header Upgrade $http_upgrade;
106+
proxy_set_header Connection $connection_upgrade;
107+
proxy_redirect off;
108+
proxy_buffering off;
109+
}
110+
}
111+
}

docs/changelog.md

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ Other notable changes:
3232
Note: you may want to update your search index to include the category search,
3333
see https://docs.umap-project.org/en/stable/config/settings/#umap_search_configuration
3434

35+
36+
### Breaking change
37+
38+
* The Docker image will not serve assets and data files anymore, an Nginx container must
39+
be configured. See [docker-compose.yml](https://github.com/umap-project/umap/blob/master/docker-compose.yml)
40+
for an example.
41+
3542
### New features
3643
* add collaborative real-time map editing
3744
* add atomic undo redo by @yohanboniface in #2570

umap/settings/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@
164164
STATIC_URL = "/static/"
165165
MEDIA_URL = "/uploads/"
166166

167-
STATIC_ROOT = os.path.join("static")
168-
MEDIA_ROOT = os.path.join("uploads")
167+
STATIC_ROOT = env("STATIC_ROOT", default=os.path.join("static"))
168+
MEDIA_ROOT = env("MEDIA_ROOT", default=os.path.join("uploads"))
169169

170170
STATICFILES_FINDERS = [
171171
"django.contrib.staticfiles.finders.FileSystemFinder",

0 commit comments

Comments
 (0)