]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/templates/default/nginx.erb
nominatim: refuse to geocode IP addresses
[chef.git] / cookbooks / nominatim / templates / default / nginx.erb
1 upstream nominatim_service {
2 <% if node[:nominatim][:api_flavour] == "php" %>
3   server unix:/run/php/php-nominatim.openstreetmap.org-fpm.sock fail_timeout=0;
4 <% elsif node[:nominatim][:api_flavour] == "python"  %>
5   server unix:/run/gunicorn-nominatim.openstreetmap.org.sock fail_timeout=0;
6 <% end -%>
7 }
8
9 map $uri $nominatim_script_name {
10     ~^/*(.+?)\.php        $1;
11     ~^/*([^/]+)           $1;
12     ^$                   search;
13 }
14
15 map $uri $nominatim_path_info {
16     ~^/([^/]+)(.*)$      $2;
17 }
18
19 map $args $format {
20     default                  default;
21     ~(^|&)format=html(&|$)   html;
22     ~(^|&)format=            other;
23 }
24
25 map $uri/$format $forward_to_ui {
26     default               1;
27     ~^/ui                 0;
28     ~/other$              0;
29     ~/reverse.*/default   0;
30     ~/lookup.*/default    0;
31     ~/status.*/default    0;
32 }
33
34 map $query_string $email_id {
35     ~(^|&)email=([^&]+)  $2;
36 }
37
38 map $email_id $missing_email {
39     default "";
40     "" 1;
41 }
42
43 map $http_user_agent $missing_ua {
44     default "";
45     "" 1;
46 }
47
48 map $http_referer $missing_referer {
49     default "";
50     "" 1;
51 }
52
53 # Whitelisted IPs
54 geo $whitelisted {
55     default 0;
56 <% @frontends.each do |frontend| -%>
57 <% frontend.ipaddresses(:role => :external).sort.each do |address| -%>
58     <%= address %> 1;
59 <% end -%>
60 <% end -%>
61     46.235.224.148 1;
62     209.132.180.180 1;
63     209.132.180.168 1;
64     8.43.85.3 1; # gnome
65     8.43.85.4 1; # gnome
66     8.43.85.5 1; # gnome
67     2620:52:3:1:5054:ff:fe0a:75a4 1; # gnome
68     2620:52:3:1:5054:ff:fe0a:75a2 1; # gnome
69     2620:52:3:1:5054:ff:fe0a:75aa 1; # gnome
70 }
71
72 map $missing_email$missing_referer$http_user_agent $blocked_user_agent {
73    default 0;
74    "11" 2; # block any requests without identifier
75    include <%= @confdir %>/nginx_blocked_user_agent.conf;
76 }
77
78 map $missing_email$missing_ua$http_referer $blocked_referrer {
79    default 0;
80    include <%= @confdir %>/nginx_blocked_referrer.conf;
81 }
82
83 map $missing_referer$missing_ua$email_id $blocked_email {
84    default 0;
85    include <%= @confdir %>/nginx_blocked_email.conf;
86 }
87
88 map $nominatim_script_name$missing_referer $blocked_path {
89    default 0;
90    "details1" 1;
91 }
92
93 map $whitelisted $limit_www {
94     1 "";
95     0 $binary_remote_addr;
96 }
97
98 map $blocked_user_agent $limit_tarpit {
99     0 "";
100     1 $binary_remote_addr;
101     2 $binary_remote_addr;
102 }
103
104 map $missing_email$missing_referer$http_user_agent $generic_mozilla {
105     default 0;
106     ~^11Mozilla/4.0 1;
107     ~^11Mozilla/5.0 2;
108 }
109
110 map $whitelisted$generic_mozilla$uri $limit_reverse {
111     default "";
112     ~01/reverse.*  $binary_remote_addr;
113     ~02/reverse.*  $binary_remote_addr;
114 }
115
116 limit_req_zone $limit_www zone=www:50m rate=2r/s;
117 limit_req_zone $limit_tarpit zone=tarpit:10m rate=1r/s;
118 limit_req_zone $binary_remote_addr zone=blocked:10m rate=20r/m;
119 limit_req_zone $limit_reverse zone=reverse:10m rate=10r/m;
120
121 server {
122     listen 80 default_server;
123     listen [::]:80 default_server;
124
125     access_log <%= node[:nominatim][:logdir] %>/nominatim.openstreetmap.org-access.log combined;
126     error_log <%= node[:nominatim][:logdir] %>/nominatim.openstreetmap.org-error.log;
127
128     location /nginx_status {
129         stub_status on;
130         access_log   off;
131         allow 127.0.0.1;
132         allow ::1;
133         deny all;
134     }
135
136      rewrite ^/\.well-known/acme-challenge/(.*)$ http://acme.openstreetmap.org/.well-known/acme-challenge/$1 permanent;
137
138      location / {
139          return 301 https://$host$request_uri;
140      }
141 }
142
143 server {
144     # IPv4
145     listen       443 ssl http2 default_server;
146     # IPv6
147     listen       [::]:443 ssl http2 default_server;
148     server_name  localhost;
149
150     ssl_certificate /etc/ssl/certs/<%= node[:fqdn] %>.pem;
151     ssl_certificate_key /etc/ssl/private/<%= node[:fqdn] %>.key;
152
153     root <%= @directory %>/static-website;
154     index /search;
155
156     access_log <%= node[:nominatim][:logdir] %>/nominatim.openstreetmap.org-access.log combined;
157     error_log <%= node[:nominatim][:logdir] %>/nominatim.openstreetmap.org-error.log;
158
159     location /nginx_status {
160         stub_status on;
161         access_log   off;
162         allow 127.0.0.1;
163         allow ::1;
164         deny all;
165     }
166
167     error_page 403 /403.html;
168     location /403.html {
169         limit_req zone=blocked burst=5;
170     }
171
172     error_page 429 /509.html;
173     location /509.html {
174         limit_req zone=blocked burst=5;
175     }
176
177     location / {
178         try_files $uri $uri/ @php;
179     }
180
181     location /ui/ {
182         alias <%= @ui_directory %>/dist/;
183         index search.html;
184     }
185
186     location /qa-data/ {
187         add_header Access-Control-Allow-Origin "*" always;
188     }
189
190     location ~* ^/(search|reverse)(\.php)?/ {
191         error_page 404 /404-old-search-syntax.html;
192         return 404;
193     }
194
195     location @php {
196         if ($blocked_user_agent ~ ^2$)
197         { return 403; }
198         if ($blocked_referrer)
199         { return 403; }
200         if ($blocked_email)
201         { return 403; }
202         if ($blocked_path)
203         { return 403; }
204         if ($args ~* "q=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[ &]")
205         { return 418; }
206         include <%= @confdir %>/nginx_blocked_generic.conf;
207
208         limit_req zone=www burst=10;
209         limit_req zone=tarpit burst=5;
210         limit_req zone=reverse burst=5;
211         limit_req_status 429;
212 <% if node[:nominatim][:api_flavour] == "php" %>
213         fastcgi_pass nominatim_service;
214         include fastcgi_params;
215         fastcgi_param QUERY_STRING    $args;
216         fastcgi_param PATH_INFO       "$nominatim_path_info";
217         fastcgi_param SCRIPT_FILENAME  "<%= @directory %>/website/$nominatim_script_name";
218 <% elsif node[:nominatim][:api_flavour] == "python" %>
219
220         if ($request_method = 'OPTIONS') {
221           add_header 'Content-Type' 'text/plain; charset=UTF-8';
222           add_header 'Content-Length' 0;
223           add_header Access-Control-Allow-Origin "*";
224           add_header Access-Control-Allow-Methods 'GET,OPTIONS';
225           add_header Access-Control-Allow-Headers $http_access_control_request_headers;
226           return 204;
227         }
228
229         proxy_set_header Host $http_host;
230         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
231         proxy_set_header X-Forwarded-Proto $scheme;
232         proxy_redirect off;
233         proxy_pass http://nominatim_service;
234 <% end -%>
235         if ($forward_to_ui) {
236             rewrite ^(/[^/]*) https://$host/ui$1.html redirect;
237         }
238     }
239
240 <% if node[:nominatim][:api_flavour] == "php" %>
241     location ~* \.php$ {
242         if ($blocked_user_agent ~ ^2$)
243         { return 403; }
244         if ($blocked_referrer)
245         { return 403; }
246         if ($blocked_email)
247         { return 403; }
248         include <%= @confdir %>/nginx_blocked_generic.conf;
249
250         limit_req zone=www burst=10;
251         limit_req zone=tarpit burst=2;
252         limit_req zone=reverse burst=5;
253         limit_req_status 429;
254         fastcgi_pass    nominatim_service;
255         include         fastcgi_params;
256         fastcgi_param   SCRIPT_FILENAME    <%= @directory %>/website/$fastcgi_script_name;
257
258         if ($forward_to_ui) {
259             rewrite (.*).php https://$host/ui$1.html redirect;
260         }
261     }
262 <% end -%>
263 }