about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-12-01 12:27:39 +0000
committerPjotr Prins <pjotr.public01@thebird.nl>2026-12-01 12:27:39 +0000
commita13b63a3bd5ce7dda0af9bf13b7e83e4f431934b (patch)
treef35308ef4df89bc5e5557e0c51eaf3fd4aacb525
parent3a5a0b088b6b5ee9d3f6501582dcffc3eb5ed83a (diff)
downloadgenenetwork4-a13b63a3bd5ce7dda0af9bf13b7e83e4f431934b.tar.gz
nginx: one site for two names, and why certbot broke it
certbot --nginx inserted its TLS directives into the port-80 redirect block and
left the redirect in place, so that block became a :443 server whose first
directive was `return 301 https://romeral.systemsgenetics.org$request_uri;'.
It also claimed both server_names, and nginx serves a request from the first
block that matches: every HTTPS request was answered by that redirect, romeral
redirected to itself in a loop, and vesuvio was served romeral's certificate
and shadowed its own block.

So: one :80 server for both names that redirects to $host (a hardcoded name
sends each site's visitors to the other) and answers ACME on both ports, and
one :443 server for both names with a certificate that has to cover both --
the header says how to expand it and that certbot must not add a second block.

Also added: the Cloudflare ranges as trusted real-IP sources, or every access
line and the per-address limit_conn count Cloudflare's edge instead of the
visitor, and the root hang's `return 444' alternative in the comment where the
524 that Cloudflare turns it into is explained.

Verified with a scratch nginx on the host (paths and ports stubbed, self-signed
cert): syntax ok; https /gn4/version 200 on both names; http redirects to the
name that was asked for; https root held until the client gave up.
-rw-r--r--etc/nginx-vesuvio.pu.ac.ke.conf89
1 files changed, 71 insertions, 18 deletions
diff --git a/etc/nginx-vesuvio.pu.ac.ke.conf b/etc/nginx-vesuvio.pu.ac.ke.conf
index b29208a..79c88b1 100644
--- a/etc/nginx-vesuvio.pu.ac.ke.conf
+++ b/etc/nginx-vesuvio.pu.ac.ke.conf
@@ -6,6 +6,25 @@ log_format gn4 '$remote_addr - $remote_user [$time_local] "$request" '
                'user=$upstream_http_x_gn4_user '
                'ua="$http_user_agent"';
 
+# Cloudflare fronts romeral.systemsgenetics.org, so the peer nginx sees is an
+# edge address. Trust the edge to name the client, from the published ranges
+# only (https://www.cloudflare.com/ips/), or every log line and every
+# per-address rule -- limit_conn above all -- would count Cloudflare, not the
+# visitor.
+set_real_ip_from 173.245.48.0/20;   set_real_ip_from 103.21.244.0/22;
+set_real_ip_from 103.22.200.0/22;   set_real_ip_from 103.31.4.0/22;
+set_real_ip_from 141.101.64.0/18;   set_real_ip_from 108.162.192.0/18;
+set_real_ip_from 190.93.240.0/20;   set_real_ip_from 188.114.96.0/20;
+set_real_ip_from 197.234.240.0/22;  set_real_ip_from 198.41.128.0/17;
+set_real_ip_from 162.158.0.0/15;    set_real_ip_from 104.16.0.0/13;
+set_real_ip_from 104.24.0.0/14;     set_real_ip_from 172.64.0.0/13;
+set_real_ip_from 131.0.72.0/22;
+set_real_ip_from 2400:cb00::/32;    set_real_ip_from 2606:4700::/32;
+set_real_ip_from 2803:f800::/32;    set_real_ip_from 2405:b500::/32;
+set_real_ip_from 2405:8100::/32;    set_real_ip_from 2a06:98c0::/29;
+set_real_ip_from 2c0f:f248::/32;
+real_ip_header CF-Connecting-IP;
+
 # The root location holds a request open (limit_conn below caps how many), and
 # its zone is declared in the http block: on vesuvio that is line 43 of
 # /etc/nginx/nginx.conf --
@@ -16,6 +35,19 @@ log_format gn4 '$remote_addr - $remote_user [$time_local] "$request" '
 # Uncomment with the limit_req line inside the location below.
 # limit_req_zone $binary_remote_addr zone=gn4_per_ip:10m rate=20r/s;
 
+# Two names, one site: vesuvio.pu.ac.ke and romeral.systemsgenetics.org (the
+# latter behind Cloudflare) serve the same GN4. The certificate must cover BOTH:
+#
+#   sudo certbot --nginx -d vesuvio.pu.ac.ke -d romeral.systemsgenetics.org \
+#        --cert-name vesuvio.pu.ac.ke --expand
+#
+# and it must stay that way at renewal (certbot renew keeps the names it was
+# asked for). Do not let certbot add a second :443 block for one name: nginx
+# serves a request from the first matching block, so the new one shadows this
+# one, and a redirect left in it makes the site redirect to itself.
+# Cloudflare forwards :80 to this origin, which is why the ACME path below is
+# answered there before the redirect.
+
 # nginx: https://vesuvio.pu.ac.ke/gn4/ -> genenetwork4 on 127.0.0.1:3000
 #
 # Drop into /etc/nginx/conf.d/ (included from inside nginx's http block) or
@@ -45,26 +77,36 @@ log_format gn4 '$remote_addr - $remote_user [$time_local] "$request" '
 # (listen 80) and the "if $host" redirect then points back at itself.
 # certbot --nginx rewrites this block in its own style; either is fine, as
 # long as the redirect does not live in the TLS block.
+# Port 80 exists only to send people to https, and to answer ACME challenges
+# (certbot renews over :80, which Cloudflare forwards to this origin). It must
+# be its own server block: merging it into the :443 block turns that block into
+# an HTTP one and the redirect then points back at itself.
 server {
     listen 80;
     listen [::]:80;
-    server_name vesuvio.pu.ac.ke;
+    server_name vesuvio.pu.ac.ke romeral.systemsgenetics.org;
 
-    return 301 https://vesuvio.pu.ac.ke$request_uri;
+    # ${host} preserves the name the visitor used; a hardcoded name sends
+    # romeral's visitors to vesuvio and vice versa.
+    location /.well-known/acme-challenge/ { root /var/www/html; }
+    location / { return 301 https://$host$request_uri; }
 }
 
+# One TLS server for both names. The certificate must cover both (see the
+# certbot line in this file's header comment); certbot --nginx otherwise
+# inserts a second :443 block for the new name, and because nginx takes the
+# first block that matches a server_name, that block shadows this one --
+# which is exactly how "it redirects to itself forever" happened once.
 server {
     listen 443 ssl;
+    listen [::]:443 ssl;
     http2 on;                      # nginx >= 1.25.1; else: listen 443 ssl http2;
-    # www is deliberately absent: the certificate's subjectAltName is only
-    # vesuvio.pu.ac.ke, so https://www.<host> would fail verification. Add
-    # it here once the certificate covers it (`certbot -d www.… --expand`).
-    server_name vesuvio.pu.ac.ke;
+    server_name vesuvio.pu.ac.ke romeral.systemsgenetics.org;
 
-    # Point these at the real files; the Let's Encrypt layout is assumed.
     ssl_certificate     /etc/letsencrypt/live/vesuvio.pu.ac.ke/fullchain.pem;
     ssl_certificate_key /etc/letsencrypt/live/vesuvio.pu.ac.ke/privkey.pem;
-    ssl_protocols TLSv1.2 TLSv1.3;
+    include /etc/letsencrypt/options-ssl-nginx.conf;
+    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
     ssl_session_cache shared:gn4_ssl:10m;
     ssl_session_timeout 1h;
     # add_header Strict-Transport-Security "max-age=31536000" always;
@@ -74,19 +116,30 @@ server {
     access_log /var/log/nginx/gn4.access.log gn4;
     error_log  /var/log/nginx/gn4.error.log;
 
-    # Bare host: send people to the mount point rather than the catch-all
-    # 404 below. Drop this if the host root should stay empty.
+    # The bare host answers nothing at all -- on either name, and through
+    # Cloudflare, which turns a held request into its own 524 after about 100
+    # seconds. /gn4/ is the interface; a visit to the hostname is not an
+    # invitation to look around. proxy_pass points at TEST-NET-1 (RFC 5737),
+    # which is never routed, so the connect attempt goes nowhere and the
+    # client gives up first; the 300s timeouts are the backstop, not the
+    # intent. `return 444' is the cheaper alternative if holding sockets ever
+    # matters: it closes the connection with no reply.
     location = / {
-       proxy_pass http://192.0.2.1:9;      # TEST-NET-1 (RFC 5737): never routed
-       proxy_connect_timeout 300s;
-       proxy_read_timeout    300s;
-       proxy_send_timeout    300s;
-       proxy_next_upstream   off;
-       limit_conn gn4_root 4;
-       access_log off;
-       error_log /dev/null emerg;
+        proxy_pass http://192.0.2.1:9;
+        proxy_connect_timeout 300s;
+        proxy_read_timeout    300s;
+        proxy_send_timeout    300s;
+        proxy_next_upstream   off;
+        limit_conn gn4_root 4;
+        access_log off;
+        error_log /dev/null emerg;
     }
 
+    # ACME is answered here too, not only on :80: Cloudflare's "Always Use
+    # HTTPS" redirects the challenge to this port, and a renewal that lands
+    # here must not fall through to the 404 below.
+    location /.well-known/acme-challenge/ { root /var/www/html; }
+
     location = /gn4 { return 301 /gn4/; }
 
     location /gn4/ {