]> git.openstreetmap.org Git - chef.git/blob - cookbooks/imagery/templates/default/nginx_imagery_layer_fragment.conf.erb
imagery: extra headers and handle 204 responses
[chef.git] / cookbooks / imagery / templates / default / nginx_imagery_layer_fragment.conf.erb
1 <% require 'uri' %>
2 # DO NOT EDIT - This file is being maintained by Chef
3 location ~* "^/layer/<%= @layer %>/(\d+)/(\d+)/(\d+)\.(jpg|jpeg|png|webp)$" {
4   # keep captures in variables for use in the named location
5   set $z   $1;
6   set $x   $2;
7   set $y   $3;
8   set $ext $4;
9
10   # Serve local file if present; otherwise jump to proxy
11   try_files /layer/<%= @layer %>/$z/$x/$y.jpg /layer/<%= @layer %>/$z/$x/$y.jpeg /layer/<%= @layer %>/$z/$x/$y.png /layer/<%= @layer %>/$z/$x/$y.webp @tile_backend;
12
13   # Return transparent PNG for HTTP 204 (no content) responses from backend
14   error_page 204 =200 /transparent.png;
15
16   gzip off;
17   expires 31d;
18   add_header Cache-Control "stale-while-revalidate=2678400, stale-if-error=2678400";
19   add_header x-cache-status $upstream_cache_status;
20   add_header 'Access-Control-Allow-Origin' '*' always;
21 }
22
23 location @tile_backend {
24   internal;
25
26 <% if @uses_tiler -%>
27   set $upstream_uri /mosaicjson/tiles/WebMercatorQuad/$z/$x/$y@1x.$ext?url=<%= URI.encode_www_form_component(@source) %>&pixel_selection=first;
28
29   proxy_pass http://<%= @site %>_tiler_backend$upstream_uri;
30   proxy_set_header Host $host;
31   proxy_set_header Referer $http_referer;
32   proxy_set_header X-Forwarded-For $remote_addr;
33   proxy_set_header X-Forwarded-Proto https;
34   proxy_set_header X-Forwarded-SSL on;
35   proxy_http_version 1.1;
36   proxy_set_header Connection "";
37   proxy_set_header Cache-Control "";
38   proxy_set_header Pragma "";
39   proxy_redirect off;
40
41   proxy_cache_key "<%= @layer %><%= @revision %> $request_method $z $x $y $ext";
42   proxy_cache proxy_cache_zone;
43   proxy_cache_valid 200 204 180d;
44   proxy_cache_use_stale error timeout updating http_502 http_503 http_504;
45   proxy_cache_background_update on;
46
47   proxy_next_upstream error timeout invalid_header http_500 http_503;
48   proxy_next_upstream_timeout 30s;
49   proxy_next_upstream_tries 3;
50
51   proxy_intercept_errors on;
52   proxy_ignore_headers "Cache-Control";
53   proxy_ignore_headers "Expires";
54   proxy_hide_header "Cache-Control";
55   proxy_hide_header "Expires";
56   proxy_hide_header "Access-Control-Allow-Origin";
57   proxy_hide_header "Content-BBOX";
58   proxy_hide_header "Content-CRS";
59 <% else -%>
60   # Override QUERY_STRING to force mapserver query parameters
61   fastcgi_param QUERY_STRING "map=/srv/imagery/mapserver/layer-<%= @layer %>.map&mode=tile&layers=<%= @layer %>&tilemode=gmap&tile=$x+$y+$z";
62   fastcgi_pass "<%= @site %>_fastcgi";
63   include fastcgi_params;
64   fastcgi_param REQUEST_METHOD "GET";
65   fastcgi_param HTTP_PROXY "";
66   fastcgi_read_timeout 60s;
67
68   # Caching
69   fastcgi_cache "fastcgi_cache_zone";
70   fastcgi_cache_key "<%= @layer %><%= @revision %> $request_method $z $x $y";
71
72   # Free connection to socket for other requests
73   fastcgi_keep_conn off;
74
75   fastcgi_cache_valid 200 21d;
76
77   # Serve stale cache on errors or if updating
78   fastcgi_cache_use_stale error timeout updating http_500 http_503;
79   # If in cache as stale, serve stale and update in background
80   fastcgi_cache_background_update on;
81   # Enable revalidation using If-Modified-Since and If-None-Match for stale items
82   fastcgi_cache_revalidate on;
83
84   keepalive_requests 0;
85
86   fastcgi_catch_stderr "Image handling error";
87
88   fastcgi_next_upstream error timeout invalid_header http_500 http_503;
89   fastcgi_next_upstream_tries 8;
90 <% end -%>
91   gzip off;
92   expires 31d;
93   add_header Cache-Control "stale-while-revalidate=2678400, stale-if-error=2678400";
94   add_header x-cache-status $upstream_cache_status;
95   add_header 'Access-Control-Allow-Origin' '*' always;
96 }
97
98 <% if @root_layer -%>
99 rewrite "^/(\d+)/(\d+)/(\d+)\.(jpg|jpeg|png|webp)$" "/layer/<%= @layer %>/$1/$2/$3.$4" last;
100 <% end -%>
101 <% @url_aliases.each do |url| -%>
102 rewrite "^<%= url %>/(\d+)/(\d+)/(\d+)\.(jpg|jpeg|png|webp)$" "/layer/<%= @layer %>/$1/$2/$3.$4" last;
103 <% end -%>