]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tilecache/templates/default/nginx_tile.conf.erb
Rearrange blocked tile blocked UAs into sections
[chef.git] / cookbooks / tilecache / templates / default / nginx_tile.conf.erb
1 # DO NOT EDIT - This file is being maintained by Chef
2
3 upstream tile_cache_backend {
4   server 127.0.0.1:8080 weight=1000 max_fails=32;
5   server 127.0.0.2:8080 weight=1000 max_fails=32;
6
7   # Add the tile_siblings caches to relieve pressure if local squid failing
8   # Balancer: round-robin
9 <% server_weight = 1000 -%>
10 <% Array(@node[:tilecache][:tile_siblings]).each do |cache_peer| -%>
11 <% @caches.each do |cache| -%>
12 <% if cache_peer == cache[:fqdn] -%>
13 <% if cache[:hostname] != node[:hostname] -%>
14 <% cache.ipaddresses(:family => :inet, :role => :external).sort.each do |address| -%>
15   server <%= address %>:80 weight=<%= server_weight %> max_fails=32 backup; # Server <%= cache[:hostname] %>
16 <% server_weight -= server_weight.div(2) -%>
17 <% end -%>
18 <% end -%>
19 <% end -%>
20 <% end -%>
21 <% end -%>
22
23   keepalive 128;
24 }
25
26 # Geo Map of tile caches
27 geo $tile_cache {
28   default "client";
29 <% @caches.each do |cache| -%>
30 <% cache.ipaddresses(:family => :inet, :role => :external).sort.each do |address| -%>
31   <%= address %> "cache"; # <%= cache[:hostname] %>
32 <% end -%>
33 <% end -%>
34 }
35
36 # Rates table based on current cookie value
37 # map $cookie__osm_totp_token $limit_rate_qos {
38 #  include /etc/nginx/conf.d/tile_qos_rates.map;
39 # }
40
41 # Set-Cookie table based on current cookie value
42 # map $cookie__osm_totp_token $cookie_qos_token_set {
43 #  include /etc/nginx/conf.d/tile_qos_cookies.map;
44 # }
45
46 map $http_user_agent $approved_scraper {
47   default                   0; # Not approved
48   '~^JOSM\/'                1; # JOSM
49   '~^Mozilla\/5\.0\ QGIS\/' 1; # QGIS
50 }
51
52 map $http_user_agent $denied_scraper {
53   default                0; # Not denied
54   ''                     1; # No User-Agent Set
55
56   # Library defaults
57   '~^Python\-urllib\/'   1;
58   '~^python\-requests\/' 1;
59   '~^node\-fetch\/'      1;
60   '~^R$'                 1;
61   '~^Java\/'             1;
62   '~^tiles$'             1;
63   '~^okhttp\/'           1;
64   '~^Microsoft-ATL-Native\/' 1;
65   '/n software IPWorks HTTP/S Component - www.nsoftware.com' 1;
66   '~^Wget\/'             1;
67
68   # Library defaults or fakes
69   'Android'              1;
70   'kc_android'           1;
71   'host'                 1;
72   '~^maptestapp'         1;
73   'Other'                1;
74
75   # Fakes
76   'Mozilla/4.0'          1;
77   'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)' 1;
78
79   # Bulk downloaders
80   'C# TilesDownloader'   1;
81   'MapDownloader'        1;
82   '~^staticmaps'         1;
83
84   # Overusage apps
85   '~^runtastic'          1;
86   '~^Where\ my\ children' 1;
87   'nossoonibusjp.android.crosswalk' 1;
88   'br.com.concisoti.potybus' 1;
89   'com.soft373.taptaxi'  1;
90   'com.kradac.ktxcore'   1;
91   'ru.crowdsystems.topcontrol.knd' 1;
92   'pl.itaxi.driver'      1;
93   'net.uztaxi.driver'    1;
94   'OSMDroid/2.1 (its; rutaxi 3.28.0)' 1;
95   'com.helleniccomms.mercedes.driver' 1;
96   'ru.taximaster.www'    1;
97   'com.arobs.trackgps'   1;
98   'com.helleniccomms.asteras.driver' 1;
99   # '~[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}' 1; # Fake UA
100 }
101
102 map $http_referer $denied_referer {
103   default                          0; # Not denied
104   'http://www.openstreetmap.org/'  1; # Faked
105   'http://www.openstreetmap.org'   1; # Faked
106   'https://www.openstreetmap.org'  1; # Faked
107   'http://openstreetmap.org/'      1; # Faked
108   'http://openstreetmap.org'       1; # Faked
109   'https://openstreetmap.org'      1; # Faked
110   'http://www.osm.org/'            1; # Faked
111   'http://www.osm.org'             1; # Faked
112   'http://osm.org/'                1; # Faked
113   'http://osm.org'                 1; # Faked
114   'http://google.com'              1; # Faked
115   'http://www.google.com'          1; # Faked
116   'http://google.com/'             1; # Faked
117   'http://www.google.com/'         1; # Faked
118   'https://google.com'             1; # Faked
119   'https://www.google.com'         1; # Faked
120   'https://google.com/'            1; # Faked
121   'https://www.google.com/'        1; # Faked
122   'http://www.microsoft.com/'      1; # Faked
123   '~^https?://pmap\.kuku\.lu/'           1; # Too much traffic
124   '~^https?://[^.]*\.pmap\.kuku\.lu/'    1; # Too much traffic
125   '~^https?://fastpokemap\.com/'         1; # Too much traffic
126   '~^https?://[^.]*\.fastpokemap\.com/'  1; # Too much traffic
127   '~^https?://pkget\.com/'               1; # Too much traffic
128   '~^https?://[^.]*\.pkget\.com/'        1; # Too much traffic
129   '~^https?://twpkinfo\.com/'            1; # Too much traffic
130   '~^https?://[^.]*\.twpkinfo\.com/'     1; # Too much traffic
131   '~^https?://9db\.jp/'                  1; # Too much traffic
132   '~^https?://[^.]*\.9db\.jp/'           1; # Too much traffic
133   '~^https?://clustrmaps\.com/'          1; # Too much traffic
134   '~^https?://[^.]*\.clustrmaps\.com/'   1; # Too much traffic
135   '~^https?://geoportal360\.pl/'         1; # Too much traffic
136   '~^https?://skelbiu\.lt/'              1; # Too much traffic
137   '~^https?://[^.]*\.skelbiu\.lt/'       1; # Too much traffic
138   # '~^https?://[^.]*\.wialon.com/'        1; # Too much traffic (hold per 2020-04-10 email)
139 }
140
141 map $http_referer $osm_referer {
142   default                                 '';    # False
143   '~^https:\/\/www\.openstreetmap\.org\/' 'osm'; # True
144 }
145
146 # Limit Cache-Control header to only approved User-Agents
147 map $tile_cache$osm_referer$http_user_agent $limit_http_cache_control {
148   default '';                             # Unset Header
149   '~^clientosmMozilla\/5\.0\ \(X11'       $http_cache_control; # Pass Header
150   '~^clientosmMozilla\/5\.0\ \(Windows'   $http_cache_control; # Pass Header
151   '~^clientosmMozilla\/5\.0\ \(iPhone'    $http_cache_control; # Pass Header
152   '~^clientosmMozilla\/5\.0\ \(Macintosh' $http_cache_control; # Pass Header
153   '~^clientosmMozilla\/5\.0\ \(Linux'     $http_cache_control; # Pass Header
154 }
155
156 # Limit Pragma header to only approved User-Agents
157 map $tile_cache$osm_referer$http_user_agent $limit_http_pragma {
158   default '';                             # Unset Header
159   '~^clientosmMozilla\/5\.0\ \(X11'       $http_pragma; # Pass Header
160   '~^clientosmMozilla\/5\.0\ \(Windows'   $http_pragma; # Pass Header
161   '~^clientosmMozilla\/5\.0\ \(iPhone'    $http_pragma; # Pass Header
162   '~^clientosmMozilla\/5\.0\ \(Macintosh' $http_pragma; # Pass Header
163   '~^clientosmMozilla\/5\.0\ \(Linux'     $http_pragma; # Pass Header
164 }
165
166 # Find Browser User-Agents which are not sending a referer.
167 # Browsers with no referer could be due to Browser extension or website Referrer-Policy
168 map $tile_cache$http_referer$scheme$http_user_agent $deny_missing_referer {
169   default                                   0; # Not denied
170   '~^clienthttpsMozilla\/5\.0\ \(X11'       1;
171   '~^clienthttpsMozilla\/5\.0\ \(Windows'   1;
172   '~^clienthttpsMozilla\/5\.0\ \(iPhone'    1;
173   '~^clienthttpsMozilla\/5\.0\ \(Macintosh' 1;
174   '~^clienthttpsMozilla\/5\.0\ \(Linux'     1;
175 }
176
177 server {
178     # IPv4
179     listen       80 deferred backlog=16384 reuseport fastopen=2048 default_server;
180     listen       443 ssl deferred backlog=16384 reuseport fastopen=2048 http2 default_server;
181     # IPv6
182     listen       [::]:80 deferred backlog=16384 reuseport fastopen=2048 default_server;
183     listen       [::]:443 ssl deferred backlog=16384 reuseport fastopen=2048 http2 default_server;
184     server_name  localhost;
185
186     proxy_buffers 8 64k;
187     proxy_busy_buffers_size 64k;
188
189     ssl_certificate      /etc/ssl/certs/tile.openstreetmap.org.pem;
190     ssl_certificate_key  /etc/ssl/private/tile.openstreetmap.org.key;
191
192     # Requests sent within early data are subject to replay attacks.
193     # See: http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_early_data
194     ssl_early_data on;
195
196     # Immediately 404 layers we do not support
197 <% for i in 20..99 do %>
198     location /<%= i %>/ {
199       return 404;
200     }
201 <% end %>
202
203     # Immediately 404 silly tile requests
204     location = /0/0/-1.png {
205       return 404;
206     }
207     location = /1/0/-1.png {
208       return 404;
209     }
210     location = /1/-1/0.png {
211       return 404;
212     }
213     location = /1/-1/1.png {
214       return 404;
215     }
216     location = /1/-1/-1.png {
217       return 404;
218     }
219     location = /1/-1/2.png {
220       return 404;
221     }
222     location = /1/1/-1.png {
223       return 404;
224     }
225     location = /1/2/-1.png {
226       return 404;
227     }
228     location = /2/0/-1.png {
229       return 404;
230     }
231     location = /2/-1/0.png {
232       return 404;
233     }
234     location = /2/-1/1.png {
235       return 404;
236     }
237     location = /2/1/-1.png {
238       return 404;
239     }
240     location = /2/-1/2.png {
241       return 404;
242     }
243     location = /2/-1/3.png {
244       return 404;
245     }
246
247 <% for i in 0..16 do %>
248 <% if i == 0 -%>
249     # Default Fallback Location Handler (lowest)
250     location / {
251 <% elsif -%>
252     # Dedicated zoom handler for caching
253     location /<%= i %>/ {
254 <% end %>
255       # Only allow GET / HEAD / OPTIONS (CORS) requests
256       limit_except GET HEAD OPTIONS {
257         deny all;
258       }
259
260       proxy_pass http://tile_cache_backend;
261       proxy_set_header X-Forwarded-For $remote_addr;
262       proxy_http_version 1.1;
263       proxy_set_header Connection '';
264
265       proxy_connect_timeout 20s;
266
267       proxy_next_upstream_tries 2;
268
269       # Replace host header.
270       proxy_set_header Host 'tile.openstreetmap.org';
271       # Do not pass cookies to backends.
272       proxy_set_header Cookie '';
273       # Do not pass Accept-Encoding to backends.
274       proxy_set_header Accept-Encoding '';
275       # Do not pass Accept to backends.
276       proxy_set_header Accept '';
277       # Do not pass Accept-Language to backends as unused.
278       proxy_set_header Accept-Language '';
279       proxy_set_header Accept-Charset '';
280       # Do not send origin, we allow all.
281       proxy_set_header origin '';
282       # Do not pass invalid headers to backend.
283       proxy_set_header X-Forwarded-Host '';
284       proxy_set_header X-Host '';
285       proxy_set_header Authorization '';
286       proxy_set_header Proxy-Authorization '';
287
288       # Drop partial requests
289       proxy_set_header range '';
290
291       # Do not allow setting cookies from backends due to caching.
292       proxy_ignore_headers Set-Cookie;
293       proxy_hide_header Set-Cookie;
294
295 <% if i != 0 -%>
296       # Caching
297       proxy_cache "proxy_cache_zone";
298       proxy_cache_lock on;
299       proxy_cache_valid 200 2d;
300       proxy_cache_valid 404 15m;
301       # Serve stale cache on errors or if updating
302       proxy_cache_use_stale error timeout updating http_404 http_500 http_503 http_504;
303       # If in cache as stale, serve stale and update in background
304       proxy_cache_background_update on;
305       # Workaround nginx async bug which causes stale cache replies to wait for the async backend cache update reply (seen in v1.16.0)
306       keepalive_requests 0;
307       # Enable revalidation using If-Modified-Since and If-None-Match for stale items
308       proxy_cache_revalidate on;
309       proxy_cache_min_uses 4;
310
311       add_header x-cache-status "$upstream_cache_status - <%= node[:hostname] %>";
312 <% else %>
313       # Severely rate limit Browser UAs which are not sending a referer.
314       # With no referer we do not know who is using tiles
315       if ($deny_missing_referer) {
316         set $limit_rate 1024;
317         add_header x-cache-ratelimit "missing-referer";
318       }
319 <% end -%>
320
321       # Set a QoS cookie if none presented (uses nginx Map)
322       # add_header Set-Cookie $cookie_qos_token_set;
323 <% if node[:ssl][:strict_transport_security] -%>
324       # Ensure Strict-Transport-Security header is removed from proxied server responses
325       proxy_hide_header Strict-Transport-Security;
326
327       # Enable HSTS
328       add_header Strict-Transport-Security "<%= node[:ssl][:strict_transport_security] %>" always;
329 <% end -%>
330
331       # QoS Traffic Rate see $limit_rate on http://nginx.org/en/docs/http/ngx_http_core_module.html
332       # set $limit_rate $limit_rate_qos;
333
334       # Allow Higher Traffic Rate from Approved User-Agents which do not support cookies (uses nginx Map)
335       # if ($approved_scraper) {
336       #   set $limit_rate 65536;
337       # }
338
339       if ($denied_scraper) {
340         set $limit_rate 512;
341         return 429;
342       }
343       if ($denied_referer) {
344         set $limit_rate 512;
345         return 418;
346       }
347
348       # Strip any ?query parameters from urls
349       set $args '';
350
351       # Allow cache purging headers only from select User-Agents (uses nginx Map)
352       proxy_set_header Cache-Control $limit_http_cache_control;
353       proxy_set_header Pragma $limit_http_pragma;
354     }
355 <% end %>
356 }