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