Skip to content

Commit 82fc0c7

Browse files
committed
Add support for 3rd-party OSM clients
1 parent f909495 commit 82fc0c7

File tree

1 file changed

+79
-25
lines changed

1 file changed

+79
-25
lines changed

osm-web/nginx.conf

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ upstream osm-rails-upstream {
1111
server osm-rails:3000;
1212
}
1313

14+
# Improve support for third-party clients by enabling workspace selection using
15+
# a path prefix (/workspace/123/...). We strip the prefix for upstream services
16+
# here:
17+
#
18+
map $request_uri $mapped_request_uri {
19+
~^(/workspace/\d+)?(.*)$ $2;
20+
}
21+
22+
# Improve support for third-party clients by mapping workspace IDs from the URI
23+
# path prefix. We carry it here from a query parameter as needed:
24+
#
25+
map $http_x_workspace $mapped_workspace_id {
26+
default $http_x_workspace;
27+
"" $arg_x_workspace;
28+
}
29+
30+
# Improve support for third-party clients by mapping TDEI tokens from a request
31+
# basic authorization header or from the user information in a URI:
32+
#
33+
map $http_authorization $mapped_tdei_token {
34+
~^Basic $remote_user;
35+
~^Bearer\ (.*)$ $1;
36+
"" $remote_user;
37+
}
38+
1439
server {
1540
listen 80;
1641
server_name _;
@@ -20,44 +45,73 @@ server {
2045

2146
error_page 404 /dispatch.map;
2247

48+
# This is effectively the maximum size of a dataset that we're willing to
49+
# accept when creating a workspace:
50+
#
51+
client_max_body_size 2g;
52+
53+
# Improve support for third-party clients by enabling workspace selection
54+
# using a path prefix (/workspace/123/...). We rewrite the path to remove
55+
# the prefix and carry the workspace ID to the next location context with
56+
# a query parameter:
57+
#
58+
location ~ ^/workspace/(\d+)/ {
59+
rewrite ^/workspace/(\d+)/(.*) /$2?x_workspace=$1 last;
60+
}
61+
62+
# If a request does not match an API endpoint implemented in CGImap, pass
63+
# the request to the Rails implementation:
64+
#
2365
location / {
2466
if ($request_method = GET) {
25-
rewrite ^/api/0\.6/map(\.(json|xml))?(\?(.*))?$ /dispatch.map break;
26-
rewrite ^/api/0\.6/(node|way|relation)/[0-9]+(\.(json|xml))?$ /dispatch.map break;
27-
rewrite ^/api/0\.6/(node|way|relation)/[0-9]+/history.*$ /dispatch.map break;
28-
rewrite ^/api/0\.6/(node|way|relation)/[0-9]+/[0-9]+.*$ /dispatch.map break;
29-
rewrite ^/api/0\.6/(node|way|relation)/[0-9]+/relations$ /dispatch.map break;
30-
rewrite ^/api/0\.6/node/[0-9]+/ways$ /dispatch.map break;
31-
rewrite ^/api/0\.6/(way|relation)/[0-9]+/full$ /dispatch.map break;
32-
rewrite ^/api/0\.6/changeset/[0-9]+.*$ /dispatch.map break;
33-
rewrite ^/api/0\.6/(nodes|ways|relations)(\?(.*))?$ /dispatch.map break;
34-
rewrite ^/api/0\.6/changeset/[0-9]+/download$ /dispatch.map break;
67+
rewrite ^/api/0\.6/map(\.(json|xml))?$ /dispatch.map last;
68+
rewrite ^/api/0\.6/(node|way|relation)/[0-9]+(\.(json|xml))?$ /dispatch.map last;
69+
rewrite ^/api/0\.6/(node|way|relation)/[0-9]+/history.*$ /dispatch.map last;
70+
rewrite ^/api/0\.6/(node|way|relation)/[0-9]+/[0-9]+.*$ /dispatch.map last;
71+
rewrite ^/api/0\.6/(node|way|relation)/[0-9]+/relations$ /dispatch.map last;
72+
rewrite ^/api/0\.6/node/[0-9]+/ways$ /dispatch.map last;
73+
rewrite ^/api/0\.6/(way|relation)/[0-9]+/full$ /dispatch.map last;
74+
rewrite ^/api/0\.6/changeset/[0-9]+.*$ /dispatch.map last;
75+
rewrite ^/api/0\.6/(nodes|ways|relations)$ /dispatch.map last;
76+
rewrite ^/api/0\.6/changeset/[0-9]+/download$ /dispatch.map last;
3577
}
3678

3779
if ($request_method = POST) {
38-
rewrite ^/api/0\.6/changeset/[0-9]+/upload.*$ /dispatch.map break;
80+
rewrite ^/api/0\.6/changeset/[0-9]+/upload.*$ /dispatch.map last;
3981
}
4082

4183
if ($request_method = PUT) {
42-
rewrite ^/api/0\.6/changeset/[0-9]+/close.*$ /dispatch.map break;
43-
rewrite ^/api/0\.6/changeset/[0-9]+$ /dispatch.map break;
44-
rewrite ^/api/0\.6/changeset/create.*$ /dispatch.map break;
84+
rewrite ^/api/0\.6/changeset/[0-9]+/close.*$ /dispatch.map last;
85+
rewrite ^/api/0\.6/changeset/[0-9]+$ /dispatch.map last;
86+
rewrite ^/api/0\.6/changeset/create.*$ /dispatch.map last;
4587
}
4688

47-
location ~ ^/(?!(dispatch\.map)) {
48-
proxy_pass http://osm-rails-upstream;
49-
proxy_set_header Host $host;
50-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
51-
proxy_set_header X-Forwarded-Proto $scheme;
52-
proxy_set_header X-Real-IP $remote_addr;
53-
proxy_set_header X-Remote-User $remote_user;
54-
}
89+
proxy_pass http://osm-rails-upstream;
90+
91+
proxy_set_header Host $host;
92+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
93+
proxy_set_header X-Forwarded-Proto $scheme;
94+
proxy_set_header X-Real-IP $remote_addr;
95+
96+
proxy_set_header Authorization "Bearer $mapped_tdei_token";
97+
proxy_set_header X-Workspace $mapped_workspace_id;
5598
}
5699

100+
# Handle requests for the endpoints implemented in CGImap:
101+
#
57102
location ~ \.map$ {
58103
fastcgi_pass osm-cgimap:8000;
59-
include fastcgi_params;
60-
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
61-
fastcgi_param CONNECT_TIMEOUT 0;
104+
105+
# Large dataset uploads may take a long time to process
106+
fastcgi_read_timeout 3h;
107+
108+
fastcgi_param CONTENT_LENGTH $content_length;
109+
fastcgi_param QUERY_STRING $query_string;
110+
fastcgi_param REMOTE_ADDR $remote_addr;
111+
fastcgi_param REQUEST_METHOD $request_method;
112+
fastcgi_param REQUEST_URI $mapped_request_uri;
113+
114+
fastcgi_param HTTP_AUTHORIZATION "Bearer $mapped_tdei_token";
115+
fastcgi_param HTTP_X_WORKSPACE $mapped_workspace_id;
62116
}
63117
}

0 commit comments

Comments
 (0)