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