Apologies if this is off-topic here, but I previously asked on NGINX sub-reddit (no replies) and on Serverfault stackexchange (got deleted).
I'm using NGINX by generating a config file to a non-standard location and call it via nginx -c <path/to/nginx.conf> (see NixOS Discourse thread for details), therefore I have to figure out what the nginx.conf should contain, and keep the Let's Encrypt / certbot part updated manually.
The only difference in behaviour between the original and the re-written config should be that the re-written version is more strict, returning 400 when the Host header is missing. As for the syntactic changes, I took inspiration from the Improve Certbot nginx config generate thread to remove the conditionals.
Thank you!
The original
... from the Let's Encrypt forum's Security issue with redirects added by Certbot's Nginx plugin post:
server {
root /var/www/html;
index index.html;
server_name example.com example.org;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name example.com example.org;
listen 80;
listen [::]:80;
return 404; # managed by Certbot
}
The re-written version
... using 3 server blocks:
-
Explicit default
serverblock catching requests without aHostheader.server { server_name ""; listen 80 default_server; return 400; }Returning
400 (Bad Request)according to RFC 9112 "HTTP/1.1", section "3.2 Request Target". -
Redirect HTTP to HTTPS.
server { server_name example.com example.org; listen 80; listen [::]:80; return 301 https://$host$request_uri; }I think the
ifblocks can be omitted because the explicit defaultserverblock in 1. (See also this thread.) -
Handle TLS connections.
server { root /var/www/html; index index.html; server_name example.com example.org; listen [::]:443 ssl ipv6only=on; # managed by Certbot listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot }