ServerHandbook live check: run log ------------------------------------------------------------------------------ Check scripts/live/checks/nginx-redirect-www-non-www-https-ubuntu-24-04.mjs (sha256 b00e2f5ef027b357291072b9d5762e6a180e1465ece045352854f5c091131657) Ran at 2026-11-12T08:23:39Z (280 s) Result PASS, all 34 claims held Tested on System Ubuntu 24.04.4 LTS Kernel 6.8.0-138-generic Package nginx 1.24.0-2ubuntu7.19 Package certbot 2.9.0-1 Package python3-certbot-nginx 2.9.0-1 Package curl 8.5.0-2ubuntu10.13 Setup A fresh Ubuntu 24.04 cloud server from a hosting provider (its stock image, a public IPv4 address), with a domain and its www name pointed at it, ufw on, Nginx serving one site and a Let’s Encrypt certificate from certbot, as the earlier articles leave it; reached over SSH as a sudo user. The certificates are real ones from Let’s Encrypt, and the redirects were also checked from another network. Every version above was read from the server at run time. Passwords used on the throwaway test server are shown as [password]. How we test: https://serverhandbook.com/how-we-test ------------------------------------------------------------------------------ Claims (34), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 24.04 server box (web-server) starts 2. ok (setup) Nginx serves example.com and www.example.com, certbot got a certificate for example.com only, as many servers start | Saving debug log to /var/log/letsencrypt/letsencrypt.log | Account registered. | Requesting a certificate for example.com | | Successfully received certificate. | Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem | Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem | This certificate expires on 2027-02-10. | These files will be updated when the certificate renews. | Certbot has set up a scheduled task to automatically renew this certificate in the background. | | Deploying certificate | Successfully deployed certificate for example.com to /etc/nginx/sites-enabled/example.com | Congratulations! You have successfully enabled HTTPS on https://example.com | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | If you like Certbot, please consider supporting our work by: | * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate | * Donating to EFF: https://eff.org/donate-le | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 3. ok http://example.com → 301 https://example.com/ (certbot’s redirect) | http://example.com 301 https://example.com/ | http://www.example.com 404 | https://example.com 200 | curl: (60) SSL: no alternative certificate subject name matches target host name 'www.example.com' | More details here: https://curl.se/docs/sslcerts.html | | curl failed to verify the legitimacy of the server and therefore could not | establish a secure connection to it. To learn more about this situation and | how to fix it, please visit the web page mentioned above. | https://www.example.com 000 4. ok https://example.com → 200 | http://example.com 301 https://example.com/ | http://www.example.com 404 | https://example.com 200 | curl: (60) SSL: no alternative certificate subject name matches target host name 'www.example.com' | More details here: https://curl.se/docs/sslcerts.html | | curl failed to verify the legitimacy of the server and therefore could not | establish a secure connection to it. To learn more about this situation and | how to fix it, please visit the web page mentioned above. | https://www.example.com 000 5. ok http://www.example.com → 404: certbot’s port-80 block answers 404 for a name it did not get a certificate for | http://example.com 301 https://example.com/ | http://www.example.com 404 | https://example.com 200 | curl: (60) SSL: no alternative certificate subject name matches target host name 'www.example.com' | More details here: https://curl.se/docs/sslcerts.html | | curl failed to verify the legitimacy of the server and therefore could not | establish a secure connection to it. To learn more about this situation and | how to fix it, please visit the web page mentioned above. | https://www.example.com 000 6. ok https://www.example.com fails: the certificate does not name www (curl 60) | http://example.com 301 https://example.com/ | http://www.example.com 404 | https://example.com 200 | curl: (60) SSL: no alternative certificate subject name matches target host name 'www.example.com' | More details here: https://curl.se/docs/sslcerts.html | | curl failed to verify the legitimacy of the server and therefore could not | establish a secure connection to it. To learn more about this situation and | how to fix it, please visit the web page mentioned above. | https://www.example.com 000 7. ok certbot certificates: "Domains: example.com" (no www) | Saving debug log to /var/log/letsencrypt/letsencrypt.log | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Found the following certs: | Certificate Name: example.com | Serial Number: | Key Type: ECDSA | Domains: example.com | Expiry Date: 2027-02-10 07:26:24+00:00 (VALID: 89 days) | Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem | Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 8. ok certbot asks to (E)xpand the existing certificate; E | Saving debug log to /var/log/letsencrypt/letsencrypt.log | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | You have an existing certificate that contains a portion of the domains you | requested (ref: /etc/letsencrypt/renewal/example.com.conf) | | It contains these names: example.com | | You requested these names for the new certificate: example.com, | www.example.com. | | Do you want to expand and replace this existing certificate with the new | certificate? | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | (E)xpand/(C)ancel: E | Renewing an existing certificate for example.com and www.example.com | | Successfully received certificate. | Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem | Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem | This certificate expires on 2027-02-10. | These files will be updated when the certificate renews. | Certbot has set up a scheduled task to automatically renew this certificate in the background. | | Deploying certificate | Successfully deployed certificate for example.com to /etc/nginx/sites-enabled/example.com | Successfully deployed certificate for www.example.com to /etc/nginx/sites-enabled/example.com | Your existing certificate has been successfully renewed, and the new certificate has been installed. | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | If you like Certbot, please consider supporting our work by: | * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate | * Donating to EFF: https://eff.org/donate-le | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 9. ok …and deploys the new certificate for both names | Saving debug log to /var/log/letsencrypt/letsencrypt.log | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | You have an existing certificate that contains a portion of the domains you | requested (ref: /etc/letsencrypt/renewal/example.com.conf) | | It contains these names: example.com | | You requested these names for the new certificate: example.com, | www.example.com. | | Do you want to expand and replace this existing certificate with the new | certificate? | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | (E)xpand/(C)ancel: E | Renewing an existing certificate for example.com and www.example.com | | Successfully received certificate. | Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem | Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem | This certificate expires on 2027-02-10. | These files will be updated when the certificate renews. | Certbot has set up a scheduled task to automatically renew this certificate in the background. | | Deploying certificate | Successfully deployed certificate for example.com to /etc/nginx/sites-enabled/example.com | Successfully deployed certificate for www.example.com to /etc/nginx/sites-enabled/example.com | Your existing certificate has been successfully renewed, and the new certificate has been installed. | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | If you like Certbot, please consider supporting our work by: | * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate | * Donating to EFF: https://eff.org/donate-le | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 10. ok certbot certificates: "Domains: example.com www.example.com" | Saving debug log to /var/log/letsencrypt/letsencrypt.log | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Found the following certs: | Certificate Name: example.com | Serial Number: | Key Type: ECDSA | Domains: example.com www.example.com | Expiry Date: 2027-02-10 07:26:51+00:00 (VALID: 89 days) | Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem | Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 11. ok with certbot’s config, http://www → https://www (still www), and https://www answers 200: two copies of the site | http://example.com 301 https://example.com/ | http://www.example.com 301 https://www.example.com/ | https://example.com 200 | https://www.example.com 200 12. ok (certbot’s file: if ($host = …) return 301 https://$host… in a port-80 block) | server { | server_name example.com www.example.com; | root /var/www/example.com; | index index.html; | | 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 = www.example.com) { | return 301 https://$host$request_uri; | } # managed by Certbot | | | if ($host = example.com) { | return 301 https://$host$request_uri; | } # managed by Certbot | | | listen 80; | listen [::]:80; | server_name example.com www.example.com; | return 404; # managed by Certbot | | | | | } 13. ok the new file is written (tee prints nothing with > /dev/null) 14. ok nginx -t is successful | nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | nginx: configuration file /etc/nginx/nginx.conf test is successful 15. ok http://example.com, http://www and https://www → 301 https://example.com/; https://example.com → 200 | http://example.com 301 https://example.com/ | http://www.example.com 301 https://example.com/ | https://example.com 200 | https://www.example.com 301 https://example.com/ 16. ok the path and the query string are kept: Location: https://example.com/docs/?page=2 | Location: https://example.com/docs/?page=2 17. ok one hop: 301 to https://example.com/, then 200 | HTTP/1.1 301 Moved Permanently | Location: https://example.com/ | HTTP/1.1 200 OK 18. ok (from another network) the same four answers | 301 https://example.com/ | 301 https://example.com/ | 301 https://example.com/ | 200 19. ok certbot renew --dry-run succeeds with the redirect-everything port-80 block | Saving debug log to /var/log/letsencrypt/letsencrypt.log | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Processing /etc/letsencrypt/renewal/example.com.conf | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Account registered. | Simulating renewal of an existing certificate for example.com and www.example.com | | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | Congratulations, all simulated renewals succeeded: | /etc/letsencrypt/live/example.com/fullchain.pem (success) | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 20. ok (the dry run left the file as we wrote it) | diff=0 21. ok with 301, the POST comes back as a GET (curl and browsers change the method) | POST / HTTP/1.1" 301 178 | GET / HTTP/1.1" 200 34 22. ok nginx -t is successful with 308 | nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | nginx: configuration file /etc/nginx/nginx.conf test is successful 23. ok with 308 the POST stays a POST (and a plain page answers it 405) | POST / HTTP/1.1" 308 180 | POST / HTTP/1.1" 405 166 24. ok nginx -t is successful with the HSTS line | nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | nginx: configuration file /etc/nginx/nginx.conf test is successful 25. ok https://example.com sends Strict-Transport-Security: max-age=31536000 | Strict-Transport-Security: max-age=31536000 26. ok (the sed added the line once, to the main HTTPS block only) | 1 27. ok nginx -t is successful with www as the main name | nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | nginx: configuration file /etc/nginx/nginx.conf test is successful 28. ok www main: three addresses → 301 https://www.example.com/, https://www → 200 | http://example.com 301 https://www.example.com/ | http://www.example.com 301 https://www.example.com/ | https://example.com 301 https://www.example.com/ | https://www.example.com 200 29. ok nginx -t PASSES with the loop: it cannot see it | nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | nginx: configuration file /etc/nginx/nginx.conf test is successful 30. ok https://example.com redirects to itself until curl stops: "Maximum (5) redirects followed" | curl: (47) Maximum (5) redirects followed | HTTP/1.1 301 Moved Permanently | Location: https://example.com/ | HTTP/1.1 301 Moved Permanently | Location: https://example.com/ | HTTP/1.1 301 Moved Permanently | Location: https://example.com/ | HTTP/1.1 301 Moved Permanently | Location: https://example.com/ | HTTP/1.1 301 Moved Permanently | Location: https://example.com/ | HTTP/1.1 301 Moved Permanently | Location: https://example.com/ 31. ok (tested on) the box reports its OS, kernel and the version of 4 package(s) | PRETTY_NAME="Ubuntu 24.04.4 LTS" | VIRT=none | KERNEL=6.8.0-138-generic | PKG certbot 2.9.0-1 | PKG curl 8.5.0-2ubuntu10.13 | PKG nginx 1.24.0-2ubuntu7.19 | PKG python3-certbot-nginx 2.9.0-1 32. ok undo: certbot’s file back, nginx -t successful | nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | nginx: configuration file /etc/nginx/nginx.conf test is successful 33. ok undo: http://www → https://www again (certbot’s redirect) | http://example.com 301 https://example.com/ | http://www.example.com 301 https://www.example.com/ | https://example.com 200 | https://www.example.com 200 34. ok (cleanup) both certificates the run got are revoked | Saving debug log to /var/log/letsencrypt/letsencrypt.log | Congratulations! You have successfully revoked the certificate that was located at /etc/letsencrypt/archive/example.com/cert1.pem. | Saving debug log to /var/log/letsencrypt/letsencrypt.log | Deleted all files relating to certificate example.com. | Congratulations! You have successfully revoked the certificate that was located at /etc/letsencrypt/live/example.com/cert.pem. ------------------------------------------------------------------------------ Commands and what they printed (33) ------------------------------------------------------------------------------ $ for u in http://example.com http://www.example.com https://example.com https://www.example.com; do curl -sS -o /dev/null -w "$u %{http_code} %{redirect_url}\n" $u; done http://example.com 301 https://example.com/ http://www.example.com 404 https://example.com 200 curl: (60) SSL: no alternative certificate subject name matches target host name 'www.example.com' More details here: https://curl.se/docs/sslcerts.html curl failed to verify the legitimacy of the server and therefore could not establish a secure connection to it. To learn more about this situation and how to fix it, please visit the web page mentioned above. https://www.example.com 000 $ sudo certbot certificates Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Found the following certs: Certificate Name: example.com Serial Number: Key Type: ECDSA Domains: example.com Expiry Date: 2027-02-10 07:26:24+00:00 (VALID: 89 days) Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $ sudo certbot --nginx -d example.com -d www.example.com Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - You have an existing certificate that contains a portion of the domains you requested (ref: /etc/letsencrypt/renewal/example.com.conf) It contains these names: example.com You requested these names for the new certificate: example.com, www.example.com. Do you want to expand and replace this existing certificate with the new certificate? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (E)xpand/(C)ancel: E Renewing an existing certificate for example.com and www.example.com Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem This certificate expires on 2027-02-10. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. Deploying certificate Successfully deployed certificate for example.com to /etc/nginx/sites-enabled/example.com Successfully deployed certificate for www.example.com to /etc/nginx/sites-enabled/example.com Your existing certificate has been successfully renewed, and the new certificate has been installed. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - If you like Certbot, please consider supporting our work by: * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate * Donating to EFF: https://eff.org/donate-le - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $ sudo certbot certificates Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Found the following certs: Certificate Name: example.com Serial Number: Key Type: ECDSA Domains: example.com www.example.com Expiry Date: 2027-02-10 07:26:51+00:00 (VALID: 89 days) Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $ for u in http://example.com http://www.example.com https://example.com https://www.example.com; do curl -sS -o /dev/null -w "$u %{http_code} %{redirect_url}\n" $u; done http://example.com 301 https://example.com/ http://www.example.com 301 https://www.example.com/ https://example.com 200 https://www.example.com 200 $ sudo cp /etc/nginx/sites-available/example.com ~/example.com.certbot $ cat /etc/nginx/sites-available/example.com server { server_name example.com www.example.com; root /var/www/example.com; index index.html; 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 = www.example.com) { return 301 https://$host$request_uri; } # managed by Certbot if ($host = example.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; listen [::]:80; server_name example.com www.example.com; return 404; # managed by Certbot } $ sudo tee /etc/nginx/sites-available/example.com > /dev/null <<'EOF' $ # 1. HTTP, both names: go to https://example.com $ server { $ listen 80; $ listen [::]:80; $ server_name example.com www.example.com; $ return 301 https://example.com$request_uri; $ } $ $ # 2. HTTPS, www: go to https://example.com $ server { $ listen 443 ssl; $ listen [::]:443 ssl; $ server_name www.example.com; $ ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; $ ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; $ include /etc/letsencrypt/options-ssl-nginx.conf; $ ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; $ return 301 https://example.com$request_uri; $ } $ $ # 3. HTTPS, example.com: the site $ server { $ listen 443 ssl; $ listen [::]:443 ssl; $ server_name example.com; $ ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; $ ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; $ include /etc/letsencrypt/options-ssl-nginx.conf; $ ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; $ root /var/www/example.com; $ index index.html; $ } $ EOF $ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ sudo systemctl reload nginx $ for u in http://example.com http://www.example.com https://example.com https://www.example.com; do curl -sS -o /dev/null -w "$u %{http_code} %{redirect_url}\n" $u; done http://example.com 301 https://example.com/ http://www.example.com 301 https://example.com/ https://example.com 200 https://www.example.com 301 https://example.com/ $ curl -sI 'http://www.example.com/docs/?page=2' | grep -i '^location' Location: https://example.com/docs/?page=2 $ curl -sIL http://www.example.com | grep -iE '^(HTTP|location)' HTTP/1.1 301 Moved Permanently Location: https://example.com/ HTTP/1.1 200 OK $ sudo certbot renew --dry-run Saving debug log to /var/log/letsencrypt/letsencrypt.log - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/example.com.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Account registered. Simulating renewal of an existing certificate for example.com and www.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations, all simulated renewals succeeded: /etc/letsencrypt/live/example.com/fullchain.pem (success) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - $ curl -s -o /dev/null -L -d 'name=test' http://www.example.com/ $ sudo tail -n 2 /var/log/nginx/access.log | cut -d'"' -f2,3 POST / HTTP/1.1" 301 178 GET / HTTP/1.1" 200 34 $ sudo sed -i 's/return 301/return 308/' /etc/nginx/sites-available/example.com $ sudo nginx -t && sudo systemctl reload nginx nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ curl -s -o /dev/null -L -d 'name=test' http://www.example.com/ $ sudo tail -n 2 /var/log/nginx/access.log | cut -d'"' -f2,3 POST / HTTP/1.1" 308 180 POST / HTTP/1.1" 405 166 $ sudo sed -i 's/return 308/return 301/' /etc/nginx/sites-available/example.com && sudo systemctl reload nginx $ sudo sed -i '/server_name example.com;/a\ add_header Strict-Transport-Security "max-age=31536000" always;' /etc/nginx/sites-available/example.com $ sudo nginx -t && sudo systemctl reload nginx nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ curl -sI https://example.com | grep -i strict Strict-Transport-Security: max-age=31536000 $ sudo tee /etc/nginx/sites-available/example.com > /dev/null <<'EOF' $ # 1. HTTP, both names: go to https://www.example.com $ server { $ listen 80; $ listen [::]:80; $ server_name example.com www.example.com; $ return 301 https://www.example.com$request_uri; $ } $ $ # 2. HTTPS, the bare name: go to https://www.example.com $ server { $ listen 443 ssl; $ listen [::]:443 ssl; $ server_name example.com; $ ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; $ ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; $ include /etc/letsencrypt/options-ssl-nginx.conf; $ ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; $ return 301 https://www.example.com$request_uri; $ } $ $ # 3. HTTPS, www.example.com: the site $ server { $ listen 443 ssl; $ listen [::]:443 ssl; $ server_name www.example.com; $ ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; $ ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; $ include /etc/letsencrypt/options-ssl-nginx.conf; $ ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; $ root /var/www/example.com; $ index index.html; $ } $ EOF $ sudo nginx -t && sudo systemctl reload nginx nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ for u in http://example.com http://www.example.com https://example.com https://www.example.com; do curl -sS -o /dev/null -w "$u %{http_code} %{redirect_url}\n" $u; done http://example.com 301 https://www.example.com/ http://www.example.com 301 https://www.example.com/ https://example.com 301 https://www.example.com/ https://www.example.com 200 $ sudo tee /etc/nginx/sites-available/example.com > /dev/null <<'EOF' $ server { $ listen 80; $ listen [::]:80; $ server_name example.com www.example.com; $ return 301 https://example.com$request_uri; $ } $ $ server { $ listen 443 ssl; $ listen [::]:443 ssl; $ server_name example.com www.example.com; $ ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; $ ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; $ include /etc/letsencrypt/options-ssl-nginx.conf; $ ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; $ return 301 https://example.com$request_uri; $ root /var/www/example.com; $ index index.html; $ } $ EOF $ sudo nginx -t && sudo systemctl reload nginx nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ curl -sSIL --max-redirs 5 https://example.com 2>&1 | grep -iE '^(HTTP|location|curl)' curl: (47) Maximum (5) redirects followed HTTP/1.1 301 Moved Permanently Location: https://example.com/ HTTP/1.1 301 Moved Permanently Location: https://example.com/ HTTP/1.1 301 Moved Permanently Location: https://example.com/ HTTP/1.1 301 Moved Permanently Location: https://example.com/ HTTP/1.1 301 Moved Permanently Location: https://example.com/ HTTP/1.1 301 Moved Permanently Location: https://example.com/ $ sudo cp ~/example.com.certbot /etc/nginx/sites-available/example.com $ sudo nginx -t && sudo systemctl reload nginx nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ for u in http://example.com http://www.example.com https://example.com https://www.example.com; do curl -sS -o /dev/null -w "$u %{http_code} %{redirect_url}\n" $u; done http://example.com 301 https://example.com/ http://www.example.com 301 https://www.example.com/ https://example.com 200 https://www.example.com 200 ------------------------------------------------------------------------------ The check's source (scripts/live/checks/nginx-redirect-www-non-www-https-ubuntu-24-04.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB: redirect www to non-www and HTTP to HTTPS in Nginx on Ubuntu 24.04 (one canonical address, * https://example.com): see what the four addresses answer now, make sure the certificate covers www (and add it * when it does not), replace certbot's redirect with three plain server blocks, check every address and a path with * a query string ends at https://example.com in ONE hop (from the server and from another network), renewal still * works, the opposite direction (www as the main name), 301 or 308 for forms, HSTS, the redirect loop * ("too many redirects") and how it happens, and undo. * * Runs ONLY on a real rented server (LIVE_VPS=root@
LIVE_VPS_DOMAIN=, box.mjs `vps`): HTTPS on the * www name needs a real certificate for it, from a public certificate authority that reaches port 80. Commands use * example.com; box.mjs swaps the real name in on the way out and back on the way in. * * Hidden setup, as the earlier articles leave the server: the sudo user `deploy`, ufw on with SSH and 'Nginx Full' * allowed, Nginx with a server block for example.com and www.example.com, and a certificate from * `certbot --nginx -d example.com` (the bare name ONLY: the common start that this article's step 2 fixes). * * LIVE_LE_STAGING=1 sends every certbot request to Let's Encrypt's STAGING server (adds --test-cert to the command * sent, never to the command shown) while the check is written. Such a run is never saved (meta.saveRun false). * At the end (hidden) every certificate the run got is revoked, so none stays valid for a name we do not use. */ import { spawnSync } from 'node:child_process' import { VPS, VPS_DOMAIN } from '../box.mjs' import { bareBox, reportBox } from '../lib.mjs' const STAGING = process.env.LIVE_LE_STAGING === '1' export const meta = { saveRun: !STAGING } /** The page's command → what is sent: with LIVE_LE_STAGING, certbot asks the staging server. */ const le = (cmd) => (STAGING ? cmd.replace(/^sudo certbot (\S+)/, 'sudo certbot $1 --test-cert') : cmd) /** curl -k only for staging (its certificates are not trusted); the page never shows -k. */ const K = STAGING ? ' -k' : '' /** From another network (this machine), over the internet: " ", '000' = no answer. */ const fromOutside = (url) => ( spawnSync( 'curl', ['-s', ...(STAGING ? ['-k'] : []), '-o', '/dev/null', '-m', '10', '-w', '%{http_code} %{redirect_url}', url.split('example.com').join(VPS_DOMAIN)], { encoding: 'utf8' }, ).stdout ?? '' ) .split(VPS_DOMAIN) .join('example.com') .trim() const FOUR = 'for u in http://example.com http://www.example.com https://example.com https://www.example.com; do curl -sS -o /dev/null -w "$u %{http_code} %{redirect_url}\\n" $u; done' const ORIGINAL_BLOCK = `server { listen 80; listen [::]:80; server_name example.com www.example.com; root /var/www/example.com; index index.html; }` const SSL = ` ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;` const CANONICAL = `# 1. HTTP, both names: go to https://example.com server { listen 80; listen [::]:80; server_name example.com www.example.com; return 301 https://example.com$request_uri; } # 2. HTTPS, www: go to https://example.com server { listen 443 ssl; listen [::]:443 ssl; server_name www.example.com; ${SSL} return 301 https://example.com$request_uri; } # 3. HTTPS, example.com: the site server { listen 443 ssl; listen [::]:443 ssl; server_name example.com; ${SSL} root /var/www/example.com; index index.html; }` const WWW_MAIN = `# 1. HTTP, both names: go to https://www.example.com server { listen 80; listen [::]:80; server_name example.com www.example.com; return 301 https://www.example.com$request_uri; } # 2. HTTPS, the bare name: go to https://www.example.com server { listen 443 ssl; listen [::]:443 ssl; server_name example.com; ${SSL} return 301 https://www.example.com$request_uri; } # 3. HTTPS, www.example.com: the site server { listen 443 ssl; listen [::]:443 ssl; server_name www.example.com; ${SSL} root /var/www/example.com; index index.html; }` /** The loop: one HTTPS block for both names that redirects to one of them, so the main name redirects to itself. */ const LOOP = `server { listen 80; listen [::]:80; server_name example.com www.example.com; return 301 https://example.com$request_uri; } server { listen 443 ssl; listen [::]:443 ssl; server_name example.com www.example.com; ${SSL} return 301 https://example.com$request_uri; root /var/www/example.com; index index.html; }` const write = (body) => `sudo tee /etc/nginx/sites-available/example.com > /dev/null <<'EOF'\n${body}\nEOF` const lines = (out) => Object.fromEntries(out.split('\n').map((l) => l.trim().split(/\s+/)).map(([u, c, r]) => [u, `${c} ${r ?? ''}`.trim()])) const check = (ctx) => { if (!VPS) { ctx.check('this check runs on a real rented server (LIVE_VPS and LIVE_VPS_DOMAIN set)', false, 'LIVE_VPS is not set') return } return bareBox(ctx, async ({ sudoUser }) => { const { run, ask, L } = sudoUser() /** A command with curl: the page shows it as written; a staging run sends curl -k. */ const rc = (cmd, opts = {}) => run(cmd.replace(/curl /g, `curl${K} `), { show: cmd, ...opts }) const four = (id) => { const r = rc(FOUR, { id }) return { ...r, map: lines(r.out) } } L('sudo true') // ── hidden: as the UFW, Nginx and Let's Encrypt articles leave the server (certificate for the bare name only) ─ L('sudo ufw allow OpenSSH && sudo ufw --force enable') L('sudo apt-get update -qq && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq nginx certbot python3-certbot-nginx', 900000) L("sudo ufw allow 'Nginx Full'") L("sudo mkdir -p /var/www/example.com && echo '

example.com works

' | sudo tee /var/www/example.com/index.html") L(`${write(ORIGINAL_BLOCK)}\nsudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ && sudo systemctl reload nginx`) const first = L(le('sudo certbot --nginx -d example.com --non-interactive --agree-tos -m admin@example.com --no-eff-email'), 300000) ctx.check( '(setup) Nginx serves example.com and www.example.com, certbot got a certificate for example.com only, as many servers start', /Successfully deployed certificate for example\.com/.test(first) && !/www\.example\.com/.test(first), first, ) // ── step 1: what the four addresses answer now ────────────────────────────────────────── const before = four('four-before') ctx.check('http://example.com → 301 https://example.com/ (certbot’s redirect)', before.map['http://example.com'] === '301 https://example.com/', before.out) ctx.check('https://example.com → 200', before.map['https://example.com'] === '200', before.out) ctx.check('http://www.example.com → 404: certbot’s port-80 block answers 404 for a name it did not get a certificate for', before.map['http://www.example.com'] === '404', before.out) ctx.check('https://www.example.com fails: the certificate does not name www (curl 60)', /curl: \(60\)/.test(before.out) && /www\.example\.com/.test(before.out.split('curl: (60)')[1] ?? ''), before.out) // ── step 2: the certificate must cover www ────────────────────────────────────────────── const certs1 = run('sudo certbot certificates', { id: 'certificates-before' }) ctx.check('certbot certificates: "Domains: example.com" (no www)', /Domains: example\.com\s*\n/.test(certs1.out), certs1.out) const show = 'sudo certbot --nginx -d example.com -d www.example.com' const exp = ask(le(show), [['\\(E\\)xpand/\\(C\\)ancel', 'E']], { show, id: 'expand', timeout: 300, publicReplies: true }) ctx.check('certbot asks to (E)xpand the existing certificate; E', /\(E\)xpand\/\(C\)ancel/.test(exp.out), exp.out) ctx.check('…and deploys the new certificate for both names', exp.exit === 0 && /Successfully deployed certificate for www\.example\.com/.test(exp.out), exp.out) const certs2 = run('sudo certbot certificates', { id: 'certificates-after' }) ctx.check('certbot certificates: "Domains: example.com www.example.com"', /Domains: example\.com www\.example\.com/.test(certs2.out), certs2.out) const mid = four('four-certbot') ctx.check('with certbot’s config, http://www → https://www (still www), and https://www answers 200: two copies of the site', mid.map['http://www.example.com'] === '301 https://www.example.com/' && mid.map['https://www.example.com'] === '200', mid.out) // ── step 3: back up, write the three blocks ───────────────────────────────────────────── run('sudo cp /etc/nginx/sites-available/example.com ~/example.com.certbot', { id: 'backup' }) const cb = run('cat /etc/nginx/sites-available/example.com', { id: 'cat-certbot' }) ctx.check('(certbot’s file: if ($host = …) return 301 https://$host… in a port-80 block)', /if \(\$host = www\.example\.com\)/.test(cb.out), cb.out) const w = run(write(CANONICAL), { id: 'write-canonical' }) ctx.check('the new file is written (tee prints nothing with > /dev/null)', w.exit === 0 && w.out === '', w.out) const t1 = run('sudo nginx -t', { id: 'test-canonical' }) ctx.check('nginx -t is successful', t1.exit === 0 && /test is successful/.test(t1.out), t1.out) run('sudo systemctl reload nginx', { id: 'reload-canonical' }) // ── step 4: check it ──────────────────────────────────────────────────────────────────── const after = four('four-after') ctx.check('http://example.com, http://www and https://www → 301 https://example.com/; https://example.com → 200', after.map['http://example.com'] === '301 https://example.com/' && after.map['http://www.example.com'] === '301 https://example.com/' && after.map['https://www.example.com'] === '301 https://example.com/' && after.map['https://example.com'] === '200', after.out) const q = rc(`curl -sI 'http://www.example.com/docs/?page=2' | grep -i '^location'`, { id: 'path-query' }) ctx.check('the path and the query string are kept: Location: https://example.com/docs/?page=2', /Location: https:\/\/example\.com\/docs\/\?page=2/i.test(q.out) , q.out) const hops = rc(`curl -sIL http://www.example.com | grep -iE '^(HTTP|location)'`, { id: 'hops' }) ctx.check('one hop: 301 to https://example.com/, then 200', /301[\s\S]*Location: https:\/\/example\.com\/[\s\S]*200/i.test(hops.out) && (hops.out.match(/^HTTP/gm) ?? []).length === 2 , hops.out) const outside = ['http://example.com/', 'http://www.example.com/', 'https://www.example.com/', 'https://example.com/'].map(fromOutside) ctx.check('(from another network) the same four answers', outside[0] === '301 https://example.com/' && outside[1] === '301 https://example.com/' && outside[2] === '301 https://example.com/' && outside[3] === '200', outside.join(' | ')) // ── step 5: renewal still works ───────────────────────────────────────────────────────── const dry = run('sudo certbot renew --dry-run', { id: 'dry-run', timeout: 300000 }) ctx.check('certbot renew --dry-run succeeds with the redirect-everything port-80 block', dry.exit === 0 && /Congratulations, all simulated renewals succeeded/.test(dry.out), dry.out) const unchanged = L('diff /etc/nginx/sites-available/example.com - <<\'EOF\'\n' + CANONICAL + '\nEOF\necho "diff=$?"') ctx.check('(the dry run left the file as we wrote it)', /diff=0$/.test(unchanged), unchanged) // ── forms and APIs: 301 or 308 ────────────────────────────────────────────────────────── rc(`curl -s -o /dev/null -L -d 'name=test' http://www.example.com/`, { id: 'post-301' }) const log301 = run("sudo tail -n 2 /var/log/nginx/access.log | cut -d'\"' -f2,3", { id: 'log-301' }) ctx.check('with 301, the POST comes back as a GET (curl and browsers change the method)', /POST \/ HTTP\/1\.1" 301/.test(log301.out) && /GET \/ HTTP\/1\.1" 200/.test(log301.out), log301.out) run("sudo sed -i 's/return 301/return 308/' /etc/nginx/sites-available/example.com", { id: 'sed-308' }) const t308 = run('sudo nginx -t && sudo systemctl reload nginx', { id: 'reload-308' }) ctx.check('nginx -t is successful with 308', t308.exit === 0, t308.out) rc(`curl -s -o /dev/null -L -d 'name=test' http://www.example.com/`, { id: 'post-308' }) const log308 = run("sudo tail -n 2 /var/log/nginx/access.log | cut -d'\"' -f2,3", { id: 'log-308' }) ctx.check('with 308 the POST stays a POST (and a plain page answers it 405)', /POST \/ HTTP\/1\.1" 308/.test(log308.out) && /POST \/ HTTP\/1\.1" 405/.test(log308.out), log308.out) run("sudo sed -i 's/return 308/return 301/' /etc/nginx/sites-available/example.com && sudo systemctl reload nginx", { id: 'sed-301' }) // ── HSTS ──────────────────────────────────────────────────────────────────────────────── run( `sudo sed -i '/server_name example.com;/a\\ add_header Strict-Transport-Security "max-age=31536000" always;' /etc/nginx/sites-available/example.com`, { id: 'hsts-add' }, ) const th = run('sudo nginx -t && sudo systemctl reload nginx', { id: 'reload-hsts' }) ctx.check('nginx -t is successful with the HSTS line', th.exit === 0, th.out) const hsts = rc(`curl -sI https://example.com | grep -i strict`, { id: 'hsts-check' }) ctx.check('https://example.com sends Strict-Transport-Security: max-age=31536000', /Strict-Transport-Security: max-age=31536000/i.test(hsts.out) , hsts.out) const hstsCount = L('grep -c Strict-Transport /etc/nginx/sites-available/example.com') ctx.check('(the sed added the line once, to the main HTTPS block only)', hstsCount === '1', hstsCount) // ── the opposite: www as the main name ────────────────────────────────────────────────── run(write(WWW_MAIN), { id: 'write-www' }) const tw = run('sudo nginx -t && sudo systemctl reload nginx', { id: 'reload-www' }) ctx.check('nginx -t is successful with www as the main name', tw.exit === 0, tw.out) const wfour = four('four-www') ctx.check('www main: three addresses → 301 https://www.example.com/, https://www → 200', wfour.map['http://example.com'] === '301 https://www.example.com/' && wfour.map['http://www.example.com'] === '301 https://www.example.com/' && wfour.map['https://example.com'] === '301 https://www.example.com/' && wfour.map['https://www.example.com'] === '200', wfour.out) // ── mistake: the redirect loop ────────────────────────────────────────────────────────── run(write(LOOP), { id: 'write-loop' }) const tl = run('sudo nginx -t && sudo systemctl reload nginx', { id: 'reload-loop' }) ctx.check('nginx -t PASSES with the loop: it cannot see it', tl.exit === 0 && /test is successful/.test(tl.out), tl.out) const loop = rc(`curl -sSIL --max-redirs 5 https://example.com 2>&1 | grep -iE '^(HTTP|location|curl)'`, { id: 'loop' }) ctx.check('https://example.com redirects to itself until curl stops: "Maximum (5) redirects followed"', /Maximum \(5\) redirects followed/.test(loop.out), loop.out) reportBox( ctx, L, 'A fresh Ubuntu 24.04 cloud server from a hosting provider (its stock image, a public IPv4 address), with a domain and its www name pointed at it, ufw on, Nginx serving one site and a Let’s Encrypt certificate from certbot, as the earlier articles leave it; reached over SSH as a sudo user. The certificates are real ones from Let’s Encrypt, and the redirects were also checked from another network.', ['nginx', 'certbot', 'python3-certbot-nginx', 'curl'], ) // ── undo: certbot's own file back ─────────────────────────────────────────────────────── run('sudo cp ~/example.com.certbot /etc/nginx/sites-available/example.com', { id: 'undo-copy' }) const tu = run('sudo nginx -t && sudo systemctl reload nginx', { id: 'undo-reload' }) ctx.check('undo: certbot’s file back, nginx -t successful', tu.exit === 0, tu.out) const ufour = four('four-undo') ctx.check('undo: http://www → https://www again (certbot’s redirect)', ufour.map['http://www.example.com'] === '301 https://www.example.com/', ufour.out) // ── hidden: revoke every certificate the run got ──────────────────────────────────────── const rv1 = L(le('sudo certbot revoke --cert-path /etc/letsencrypt/archive/example.com/cert1.pem --non-interactive --no-delete-after-revoke'), 300000) const rv2 = L(le('sudo certbot revoke --cert-name example.com --non-interactive --delete-after-revoke'), 300000) ctx.check('(cleanup) both certificates the run got are revoked', /revoked/i.test(rv1) && /revoked/i.test(rv2), `${rv1}\n${rv2}`) }) } export default check