]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tilecache/templates/default/nginx_tile.conf.erb
Merge pull request #364 from osm-hr/html-torrents
[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   '-'                    1;
56
57   # Library defaults
58   '~^Python\-urllib\/'   1;
59   '~^python\-requests\/' 1;
60   '~^node\-fetch\/'      1;
61   '~^R$'                 1;
62   '~^Java\/'             1;
63   '~^tiles$'             1;
64   '~^okhttp\/'           1;
65   '~^Microsoft-ATL-Native\/' 1;
66   '/n software IPWorks HTTP/S Component - www.nsoftware.com' 1;
67   '~^Wget\/'             1;
68   'java'                 1;
69
70   # Library defaults or fakes
71   'Android'              1;
72   'kc_android'           1;
73   'host'                 1;
74   '~^maptestapp'         1;
75   'Other'                1;
76   'osmdroid'             1;
77   '~^tilelive-http'      1;
78   '~^Java-http-client'   1;
79
80   # Fakes
81   'Mozilla/4.0'          1;
82   'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)' 1;
83
84   # Bulk downloaders
85   'C# TilesDownloader'   1;
86   'MapDownloader'        1;
87   '~^staticmaps'         1;
88
89   # Overusage apps
90   '~^runtastic'          1;
91   '~^Where\ my\ children' 1;
92   'nossoonibusjp.android.crosswalk' 1;
93   'br.com.concisoti.potybus' 1;
94   'com.soft373.taptaxi'  1;
95   'com.kradac.ktxcore'   1;
96   '~^ru.crowdsystems.topcontrol.knd' 1;
97   'pl.itaxi.driver'      1;
98   'net.uztaxi.driver'    1;
99   'OSMDroid/2.1 (its; rutaxi 3.28.0)' 1;
100   'com.helleniccomms.mercedes.driver' 1;
101   'ru.taximaster.www'    1;
102   'com.arobs.trackgps'   1;
103   'com.helleniccomms.asteras.driver' 1;
104
105   # Malware
106   'Agent Smith'          1;
107   # '~[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}' 1; # Fake UA
108 }
109
110 map $http_referer $denied_referer {
111   default                          0; # Not denied
112   # Faked sites
113   'http://www.openstreetmap.org/'  1;
114   'http://www.openstreetmap.org'   1;
115   'https://www.openstreetmap.org'  1;
116   'http://openstreetmap.org/'      1;
117   'http://openstreetmap.org'       1;
118   'https://openstreetmap.org'      1;
119   'http://www.osm.org/'            1;
120   'http://www.osm.org'             1;
121   'http://osm.org/'                1;
122   'http://osm.org'                 1;
123   'http://google.com'              1;
124   'http://www.google.com'          1;
125   'http://google.com/'             1;
126   'http://www.google.com/'         1;
127   'https://google.com'             1;
128   'https://www.google.com'         1;
129   'https://google.com/'            1;
130   'https://www.google.com/'        1;
131   'http://www.microsoft.com/'      1;
132
133   # Overusing websites
134   '~^https?://pmap\.kuku\.lu/'           1;
135   '~^https?://[^.]*\.pmap\.kuku\.lu/'    1;
136   '~^https?://fastpokemap\.com/'         1;
137   '~^https?://[^.]*\.fastpokemap\.com/'  1;
138   '~^https?://pkget\.com/'               1;
139   '~^https?://[^.]*\.pkget\.com/'        1;
140   '~^https?://twpkinfo\.com/'            1;
141   '~^https?://[^.]*\.twpkinfo\.com/'     1;
142   '~^https?://9db\.jp/'                  1;
143   '~^https?://[^.]*\.9db\.jp/'           1;
144   '~^https?://clustrmaps\.com/'          1;
145   '~^https?://[^.]*\.clustrmaps\.com/'   1;
146   '~^https?://geoportal360\.pl/'         1;
147   '~^https?://skelbiu\.lt/'              1;
148   '~^https?://[^.]*\.skelbiu\.lt/'       1;
149   '~^https?://wialon\.[^.]*/'            1; # wialon has many domains, so block them all
150   '~^https?://[^.]*\.wialon\.[^.]*/'     1;
151   '~^https?://gps-trace\.com/'           1;
152   '~^https?://[^.]*\.gps-trace\.com/'    1;
153   '~^https?://cellmapper\.net/'          1;
154   '~^https?://[^.]*\.cellmapper\.net/'   1;
155 }
156
157 map $http_referer $censored_referer {
158   default                                 0; # Not denied
159   # Blocked on board instructions
160   '~^https?://schiebt-sie-ab\.de/'        1;
161   '~^https?://[^.]*\.schiebt-sie-ab\.de/' 1;
162 }
163
164
165 map $http_referer $osm_referer {
166   default                                 '';    # False
167   '~^https:\/\/www\.openstreetmap\.org\/' 'osm'; # True
168 }
169
170 # Limit Cache-Control header to only approved User-Agents
171 map $tile_cache$osm_referer$http_user_agent $limit_http_cache_control {
172   default '';                             # Unset Header
173   '~^clientosmMozilla\/5\.0\ \(X11'       $http_cache_control; # Pass Header
174   '~^clientosmMozilla\/5\.0\ \(Windows'   $http_cache_control; # Pass Header
175   '~^clientosmMozilla\/5\.0\ \(iPhone'    $http_cache_control; # Pass Header
176   '~^clientosmMozilla\/5\.0\ \(Macintosh' $http_cache_control; # Pass Header
177   '~^clientosmMozilla\/5\.0\ \(Linux'     $http_cache_control; # Pass Header
178 }
179
180 # Limit Pragma header to only approved User-Agents
181 map $tile_cache$osm_referer$http_user_agent $limit_http_pragma {
182   default '';                             # Unset Header
183   '~^clientosmMozilla\/5\.0\ \(X11'       $http_pragma; # Pass Header
184   '~^clientosmMozilla\/5\.0\ \(Windows'   $http_pragma; # Pass Header
185   '~^clientosmMozilla\/5\.0\ \(iPhone'    $http_pragma; # Pass Header
186   '~^clientosmMozilla\/5\.0\ \(Macintosh' $http_pragma; # Pass Header
187   '~^clientosmMozilla\/5\.0\ \(Linux'     $http_pragma; # Pass Header
188 }
189
190 # Find Browser User-Agents which are not sending a referer.
191 # Browsers with no referer could be due to Browser extension or website Referrer-Policy
192 map $tile_cache$http_referer$scheme$http_user_agent $deny_missing_referer {
193   default                                   0; # Not denied
194   '~^clienthttpsMozilla\/5\.0\ \(X11'       1;
195   '~^clienthttpsMozilla\/5\.0\ \(Windows'   1;
196   '~^clienthttpsMozilla\/5\.0\ \(iPhone'    1;
197   '~^clienthttpsMozilla\/5\.0\ \(Macintosh' 1;
198   '~^clienthttpsMozilla\/5\.0\ \(Linux'     1;
199 }
200
201 server {
202     # IPv4
203     listen       80 deferred backlog=16384 reuseport default_server;
204     listen       443 ssl deferred backlog=16384 reuseport http2 default_server;
205     # IPv6
206     listen       [::]:80 deferred backlog=16384 reuseport default_server;
207     listen       [::]:443 ssl deferred backlog=16384 reuseport http2 default_server;
208     server_name  localhost;
209
210     proxy_buffers 8 64k;
211     proxy_busy_buffers_size 64k;
212
213     ssl_certificate      /etc/ssl/certs/tile.openstreetmap.org.pem;
214     ssl_certificate_key  /etc/ssl/private/tile.openstreetmap.org.key;
215
216     # Requests sent within early data are subject to replay attacks.
217     # See: http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_early_data
218     ssl_early_data on;
219
220     # Immediately 404 layers we do not support
221 <% for i in 20..99 do %>
222     location /<%= i %>/ {
223       return 404;
224     }
225 <% end %>
226
227     # Immediately 404 silly tile requests
228     location = /0/-1/-1.png {
229       return 404;
230     }
231     location = /0/-1/0.png {
232       return 404;
233     }
234     location = /0/-1/1.png {
235       return 404;
236     }
237     location = /0/0/-1.png {
238       return 404;
239     }
240     location = /0/0/1.png {
241       return 404;
242     }
243     location = /0/0/2.png {
244       return 404;
245     }
246     location = /0/1/-1.png {
247       return 404;
248     }
249     location = /0/1/0.png {
250       return 404;
251     }
252     location = /0/1/1.png {
253       return 404;
254     }
255     location = /0/1/2.png {
256       return 404;
257     }
258     location = /0/2/0.png {
259       return 404;
260     }
261     location = /0/2/1.png {
262       return 404;
263     }
264     location = /0/2/2.png {
265       return 404;
266     }
267     location = /1/-1/-1.png {
268       return 404;
269     }
270     location = /1/-1/0.png {
271       return 404;
272     }
273     location = /1/-1/1.png {
274       return 404;
275     }
276     location = /1/-1/2.png {
277       return 404;
278     }
279     location = /1/0/-1.png {
280       return 404;
281     }
282     location = /1/1/-1.png {
283       return 404;
284     }
285     location = /1/2/-1.png {
286       return 404;
287     }
288     location = /2/-1/0.png {
289       return 404;
290     }
291     location = /2/-1/1.png {
292       return 404;
293     }
294     location = /2/-1/2.png {
295       return 404;
296     }
297     location = /2/-1/3.png {
298       return 404;
299     }
300     location = /2/0/-1.png {
301       return 404;
302     }
303     location = /2/1/-1.png {
304       return 404;
305     }
306     location = /2/1/4.png {
307       return 404;
308     }
309     location = /2/2/4.png {
310       return 404;
311     }
312     location = /2/3/4.png {
313       return 404;
314     }
315     location = /2/4/0.png {
316       return 404;
317     }
318     location = /2/4/1.png {
319       return 404;
320     }
321     location = /2/4/2.png {
322       return 404;
323     }
324     location = /2/4/3.png {
325       return 404;
326     }
327     location = /2/4/4.png {
328       return 404;
329     }
330
331 <% for i in 0..16 do %>
332 <% if i == 0 -%>
333     # Default Fallback Location Handler (lowest)
334     location / {
335 <% elsif -%>
336     # Dedicated zoom handler for caching
337     location /<%= i %>/ {
338 <% end %>
339       # Only allow GET / HEAD / OPTIONS (CORS) requests
340       limit_except GET HEAD OPTIONS {
341         deny all;
342       }
343
344       proxy_pass http://tile_cache_backend;
345       proxy_set_header X-Forwarded-For $remote_addr;
346       proxy_http_version 1.1;
347       proxy_set_header Connection '';
348
349       proxy_connect_timeout 20s;
350
351       proxy_next_upstream_tries 2;
352
353       # Replace host header.
354       proxy_set_header Host 'tile.openstreetmap.org';
355       # Drop all request headers and request body
356       proxy_pass_request_headers off;
357       proxy_pass_request_body off;
358
359       # Do not allow setting cookies from backends due to caching.
360       proxy_ignore_headers Set-Cookie;
361       proxy_hide_header Set-Cookie;
362
363 <% if i != 0 -%>
364       # Caching
365       proxy_cache "proxy_cache_zone";
366       proxy_cache_lock on;
367       proxy_cache_valid 200 2d;
368       proxy_cache_valid 404 15m;
369       # Serve stale cache on errors or if updating
370       proxy_cache_use_stale error timeout updating http_404 http_500 http_503 http_504;
371       # If in cache as stale, serve stale and update in background
372       proxy_cache_background_update on;
373       # Workaround nginx async bug which causes stale cache replies to wait for the async backend cache update reply (seen in v1.16.0)
374       keepalive_requests 0;
375       # Enable revalidation using If-Modified-Since and If-None-Match for stale items
376       proxy_cache_revalidate on;
377       proxy_cache_min_uses 4;
378
379       add_header x-cache-status "$upstream_cache_status - <%= node[:hostname] %>";
380 <% else %>
381       # Severely rate limit Browser UAs which are not sending a referer.
382       # With no referer we do not know who is using tiles
383       if ($deny_missing_referer) {
384         set $limit_rate 1024;
385         add_header x-cache-ratelimit "missing-referer";
386       }
387 <% end -%>
388
389       # Set a QoS cookie if none presented (uses nginx Map)
390       # add_header Set-Cookie $cookie_qos_token_set;
391 <% if node[:ssl][:strict_transport_security] -%>
392       # Ensure Strict-Transport-Security header is removed from proxied server responses
393       proxy_hide_header Strict-Transport-Security;
394
395       # Enable HSTS
396       add_header Strict-Transport-Security "<%= node[:ssl][:strict_transport_security] %>" always;
397 <% end -%>
398
399       # QoS Traffic Rate see $limit_rate on http://nginx.org/en/docs/http/ngx_http_core_module.html
400       # set $limit_rate $limit_rate_qos;
401
402       # Allow Higher Traffic Rate from Approved User-Agents which do not support cookies (uses nginx Map)
403       # if ($approved_scraper) {
404       #   set $limit_rate 65536;
405       # }
406
407       if ($denied_scraper) {
408         set $limit_rate 512;
409         return 429;
410       }
411       if ($denied_referer) {
412         set $limit_rate 512;
413         return 418;
414       }
415
416       if ($censored_referer) {
417         set $limit_rate 512;
418         return 451 "Unavailable at OSMF Board request";
419       }
420
421       # Strip any ?query parameters from urls
422       set $args '';
423
424       # Allow cache purging headers only from select User-Agents (uses nginx Map)
425       proxy_set_header Cache-Control $limit_http_cache_control;
426       proxy_set_header Pragma $limit_http_pragma;
427     }
428 <% end %>
429 }