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