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