]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nominatim/templates/default/nginx.erb
nominatim: correctly forward to https
[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) 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 limit_req_zone $limit_www zone=www:50m rate=2r/s;
90 limit_req_zone $limit_tarpit zone=tarpit:10m rate=1r/s;
91 limit_req_zone $binary_remote_addr zone=blocked:10m rate=20r/m;
92
93 server {
94     listen 80 default_server;
95     listen [::]:80 default_server;
96
97     access_log <%= node[:nominatim][:logdir] %>/nominatim.openstreetmap.org-access.log combined;
98     error_log <%= node[:nominatim][:logdir] %>/nominatim.openstreetmap.org-error.log;
99
100     location /nginx_status {
101         stub_status on;
102         access_log   off;
103         allow 127.0.0.1;
104         allow ::1;
105         deny all;
106     }
107
108      rewrite ^/\.well-known/acme-challenge/(.*)$ http://acme.openstreetmap.org/.well-known/acme-challenge/$1 permanent;
109
110      location / {
111          return 301 https://$host$request_uri;
112      }
113 }
114
115 server {
116     # IPv4
117     listen       443 ssl deferred backlog=16384 reuseport http2 default_server;
118     # IPv6
119     listen       [::]:443 ssl deferred backlog=16384 reuseport http2 default_server;
120     server_name  localhost;
121
122     ssl_certificate /etc/ssl/certs/<%= node[:fqdn] %>.pem;
123     ssl_certificate_key /etc/ssl/private/<%= node[:fqdn] %>.key;
124
125     root <%= @directory %>/website;
126     index search.php;
127
128     access_log <%= node[:nominatim][:logdir] %>/nominatim.openstreetmap.org-access.log combined;
129     error_log <%= node[:nominatim][:logdir] %>/nominatim.openstreetmap.org-error.log;
130
131     location /nginx_status {
132         stub_status on;
133         access_log   off;
134         allow 127.0.0.1;
135         allow ::1;
136         deny all;
137     }
138
139     error_page 403 /403.html;
140     location /403.html {
141         limit_req zone=blocked burst=5;
142     }
143
144     error_page 429 /509.html;
145     location /509.html {
146         limit_req zone=blocked burst=5;
147     }
148
149     location / {
150         try_files $uri $uri/ @php;
151     }
152
153     location /ui/ {
154         alias <%= @ui_directory %>/dist/;
155         index search.html;
156     }
157
158     location @php {
159         if ($blocked_user_agent ~ ^2$)
160         { return 403; }
161         if ($blocked_referrer)
162         { return 403; }
163         if ($blocked_email)
164         { return 403; }
165
166         limit_req zone=www burst=10;
167         limit_req zone=tarpit burst=2;
168         limit_req_status 429;
169         fastcgi_pass nominatim_service;
170         include fastcgi_params;
171         fastcgi_param QUERY_STRING    $args;
172         fastcgi_param PATH_INFO       "$nominatim_path_info";
173         fastcgi_param SCRIPT_FILENAME  "$document_root/$nominatim_script_name";
174         if ($forward_to_ui) {
175             rewrite ^(/[^/]*) https://$host/ui$1.html redirect;
176         }
177     }
178
179     location ~* \.php$ {
180         if ($blocked_user_agent ~ ^2$)
181         { return 403; }
182         if ($blocked_referrer)
183         { return 403; }
184         if ($blocked_email)
185         { return 403; }
186
187         limit_req zone=www burst=10;
188         limit_req zone=tarpit burst=2;
189         limit_req_status 429;
190         fastcgi_pass    nominatim_service;
191         include         fastcgi_params;
192         fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
193
194         if ($forward_to_ui) {
195             rewrite (.*).php https://$host/ui$1.html redirect;
196         }
197     }
198 }