]> git.openstreetmap.org Git - chef.git/blob - cookbooks/tilecache/templates/default/nginx_tile.conf.erb
Block more default, fake and malware UAs from tile
[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   'http://www.openstreetmap.org/'  1; # Faked
113   'http://www.openstreetmap.org'   1; # Faked
114   'https://www.openstreetmap.org'  1; # Faked
115   'http://openstreetmap.org/'      1; # Faked
116   'http://openstreetmap.org'       1; # Faked
117   'https://openstreetmap.org'      1; # Faked
118   'http://www.osm.org/'            1; # Faked
119   'http://www.osm.org'             1; # Faked
120   'http://osm.org/'                1; # Faked
121   'http://osm.org'                 1; # Faked
122   'http://google.com'              1; # Faked
123   'http://www.google.com'          1; # Faked
124   'http://google.com/'             1; # Faked
125   'http://www.google.com/'         1; # Faked
126   'https://google.com'             1; # Faked
127   'https://www.google.com'         1; # Faked
128   'https://google.com/'            1; # Faked
129   'https://www.google.com/'        1; # Faked
130   'http://www.microsoft.com/'      1; # Faked
131   '~^https?://pmap\.kuku\.lu/'           1; # Too much traffic
132   '~^https?://[^.]*\.pmap\.kuku\.lu/'    1; # Too much traffic
133   '~^https?://fastpokemap\.com/'         1; # Too much traffic
134   '~^https?://[^.]*\.fastpokemap\.com/'  1; # Too much traffic
135   '~^https?://pkget\.com/'               1; # Too much traffic
136   '~^https?://[^.]*\.pkget\.com/'        1; # Too much traffic
137   '~^https?://twpkinfo\.com/'            1; # Too much traffic
138   '~^https?://[^.]*\.twpkinfo\.com/'     1; # Too much traffic
139   '~^https?://9db\.jp/'                  1; # Too much traffic
140   '~^https?://[^.]*\.9db\.jp/'           1; # Too much traffic
141   '~^https?://clustrmaps\.com/'          1; # Too much traffic
142   '~^https?://[^.]*\.clustrmaps\.com/'   1; # Too much traffic
143   '~^https?://geoportal360\.pl/'         1; # Too much traffic
144   '~^https?://skelbiu\.lt/'              1; # Too much traffic
145   '~^https?://[^.]*\.skelbiu\.lt/'       1; # Too much traffic
146   # '~^https?://[^.]*\.wialon.com/'        1; # Too much traffic (hold per 2020-04-10 email)
147 }
148
149 map $http_referer $osm_referer {
150   default                                 '';    # False
151   '~^https:\/\/www\.openstreetmap\.org\/' 'osm'; # True
152 }
153
154 # Limit Cache-Control header to only approved User-Agents
155 map $tile_cache$osm_referer$http_user_agent $limit_http_cache_control {
156   default '';                             # Unset Header
157   '~^clientosmMozilla\/5\.0\ \(X11'       $http_cache_control; # Pass Header
158   '~^clientosmMozilla\/5\.0\ \(Windows'   $http_cache_control; # Pass Header
159   '~^clientosmMozilla\/5\.0\ \(iPhone'    $http_cache_control; # Pass Header
160   '~^clientosmMozilla\/5\.0\ \(Macintosh' $http_cache_control; # Pass Header
161   '~^clientosmMozilla\/5\.0\ \(Linux'     $http_cache_control; # Pass Header
162 }
163
164 # Limit Pragma header to only approved User-Agents
165 map $tile_cache$osm_referer$http_user_agent $limit_http_pragma {
166   default '';                             # Unset Header
167   '~^clientosmMozilla\/5\.0\ \(X11'       $http_pragma; # Pass Header
168   '~^clientosmMozilla\/5\.0\ \(Windows'   $http_pragma; # Pass Header
169   '~^clientosmMozilla\/5\.0\ \(iPhone'    $http_pragma; # Pass Header
170   '~^clientosmMozilla\/5\.0\ \(Macintosh' $http_pragma; # Pass Header
171   '~^clientosmMozilla\/5\.0\ \(Linux'     $http_pragma; # Pass Header
172 }
173
174 # Find Browser User-Agents which are not sending a referer.
175 # Browsers with no referer could be due to Browser extension or website Referrer-Policy
176 map $tile_cache$http_referer$scheme$http_user_agent $deny_missing_referer {
177   default                                   0; # Not denied
178   '~^clienthttpsMozilla\/5\.0\ \(X11'       1;
179   '~^clienthttpsMozilla\/5\.0\ \(Windows'   1;
180   '~^clienthttpsMozilla\/5\.0\ \(iPhone'    1;
181   '~^clienthttpsMozilla\/5\.0\ \(Macintosh' 1;
182   '~^clienthttpsMozilla\/5\.0\ \(Linux'     1;
183 }
184
185 server {
186     # IPv4
187     listen       80 deferred backlog=16384 reuseport fastopen=2048 default_server;
188     listen       443 ssl deferred backlog=16384 reuseport fastopen=2048 http2 default_server;
189     # IPv6
190     listen       [::]:80 deferred backlog=16384 reuseport fastopen=2048 default_server;
191     listen       [::]:443 ssl deferred backlog=16384 reuseport fastopen=2048 http2 default_server;
192     server_name  localhost;
193
194     proxy_buffers 8 64k;
195     proxy_busy_buffers_size 64k;
196
197     ssl_certificate      /etc/ssl/certs/tile.openstreetmap.org.pem;
198     ssl_certificate_key  /etc/ssl/private/tile.openstreetmap.org.key;
199
200     # Requests sent within early data are subject to replay attacks.
201     # See: http://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_early_data
202     ssl_early_data on;
203
204     # Immediately 404 layers we do not support
205 <% for i in 20..99 do %>
206     location /<%= i %>/ {
207       return 404;
208     }
209 <% end %>
210
211     # Immediately 404 silly tile requests
212     location = /0/0/-1.png {
213       return 404;
214     }
215     location = /1/0/-1.png {
216       return 404;
217     }
218     location = /1/-1/0.png {
219       return 404;
220     }
221     location = /1/-1/1.png {
222       return 404;
223     }
224     location = /1/-1/-1.png {
225       return 404;
226     }
227     location = /1/-1/2.png {
228       return 404;
229     }
230     location = /1/1/-1.png {
231       return 404;
232     }
233     location = /1/2/-1.png {
234       return 404;
235     }
236     location = /2/0/-1.png {
237       return 404;
238     }
239     location = /2/-1/0.png {
240       return 404;
241     }
242     location = /2/-1/1.png {
243       return 404;
244     }
245     location = /2/1/-1.png {
246       return 404;
247     }
248     location = /2/-1/2.png {
249       return 404;
250     }
251     location = /2/-1/3.png {
252       return 404;
253     }
254
255 <% for i in 0..16 do %>
256 <% if i == 0 -%>
257     # Default Fallback Location Handler (lowest)
258     location / {
259 <% elsif -%>
260     # Dedicated zoom handler for caching
261     location /<%= i %>/ {
262 <% end %>
263       # Only allow GET / HEAD / OPTIONS (CORS) requests
264       limit_except GET HEAD OPTIONS {
265         deny all;
266       }
267
268       proxy_pass http://tile_cache_backend;
269       proxy_set_header X-Forwarded-For $remote_addr;
270       proxy_http_version 1.1;
271       proxy_set_header Connection '';
272
273       proxy_connect_timeout 20s;
274
275       proxy_next_upstream_tries 2;
276
277       # Replace host header.
278       proxy_set_header Host 'tile.openstreetmap.org';
279       # Do not pass cookies to backends.
280       proxy_set_header Cookie '';
281       # Do not pass Accept-Encoding to backends.
282       proxy_set_header Accept-Encoding '';
283       # Do not pass Accept to backends.
284       proxy_set_header Accept '';
285       # Do not pass Accept-Language to backends as unused.
286       proxy_set_header Accept-Language '';
287       proxy_set_header Accept-Charset '';
288       # Do not send origin, we allow all.
289       proxy_set_header origin '';
290       # Do not pass invalid headers to backend.
291       proxy_set_header X-Forwarded-Host '';
292       proxy_set_header X-Host '';
293       proxy_set_header Authorization '';
294       proxy_set_header Proxy-Authorization '';
295
296       # Drop partial requests
297       proxy_set_header range '';
298
299       # Do not allow setting cookies from backends due to caching.
300       proxy_ignore_headers Set-Cookie;
301       proxy_hide_header Set-Cookie;
302
303 <% if i != 0 -%>
304       # Caching
305       proxy_cache "proxy_cache_zone";
306       proxy_cache_lock on;
307       proxy_cache_valid 200 2d;
308       proxy_cache_valid 404 15m;
309       # Serve stale cache on errors or if updating
310       proxy_cache_use_stale error timeout updating http_404 http_500 http_503 http_504;
311       # If in cache as stale, serve stale and update in background
312       proxy_cache_background_update on;
313       # Workaround nginx async bug which causes stale cache replies to wait for the async backend cache update reply (seen in v1.16.0)
314       keepalive_requests 0;
315       # Enable revalidation using If-Modified-Since and If-None-Match for stale items
316       proxy_cache_revalidate on;
317       proxy_cache_min_uses 4;
318
319       add_header x-cache-status "$upstream_cache_status - <%= node[:hostname] %>";
320 <% else %>
321       # Severely rate limit Browser UAs which are not sending a referer.
322       # With no referer we do not know who is using tiles
323       if ($deny_missing_referer) {
324         set $limit_rate 1024;
325         add_header x-cache-ratelimit "missing-referer";
326       }
327 <% end -%>
328
329       # Set a QoS cookie if none presented (uses nginx Map)
330       # add_header Set-Cookie $cookie_qos_token_set;
331 <% if node[:ssl][:strict_transport_security] -%>
332       # Ensure Strict-Transport-Security header is removed from proxied server responses
333       proxy_hide_header Strict-Transport-Security;
334
335       # Enable HSTS
336       add_header Strict-Transport-Security "<%= node[:ssl][:strict_transport_security] %>" always;
337 <% end -%>
338
339       # QoS Traffic Rate see $limit_rate on http://nginx.org/en/docs/http/ngx_http_core_module.html
340       # set $limit_rate $limit_rate_qos;
341
342       # Allow Higher Traffic Rate from Approved User-Agents which do not support cookies (uses nginx Map)
343       # if ($approved_scraper) {
344       #   set $limit_rate 65536;
345       # }
346
347       if ($denied_scraper) {
348         set $limit_rate 512;
349         return 429;
350       }
351       if ($denied_referer) {
352         set $limit_rate 512;
353         return 418;
354       }
355
356       # Strip any ?query parameters from urls
357       set $args '';
358
359       # Allow cache purging headers only from select User-Agents (uses nginx Map)
360       proxy_set_header Cache-Control $limit_http_cache_control;
361       proxy_set_header Pragma $limit_http_pragma;
362     }
363 <% end %>
364 }