ServerHandbook live check: run log ------------------------------------------------------------------------------ Article https://serverhandbook.com/kb/nginx-redirect-www-non-www-https-ubuntu Check scripts/live/checks/nginx-redirect-www-non-www-https-ubuntu.mjs (sha256 751864d0fdb09add5d6fc84858f31f0abd0db35a744b9fa192535efe42cc8473) Ran at 2026-11-18T23:38:49Z (447 s) Result PASS, all 36 claims held Tested on System Ubuntu 26.04.1 LTS Kernel 7.0.0-30-generic Package nginx 1.28.3-2ubuntu1.12 Package certbot 4.0.0-4 Package python3-certbot-nginx 4.0.0-3 Package curl 8.18.0-1ubuntu2.4 Setup A fresh Ubuntu 26.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 ------------------------------------------------------------------------------ What the steps changed on the server (recorded before the first step and after the last) Packages added (0) Packages upgraded (0) Packages removed (0) Services enabled at boot (0) Services disabled (0) Listening sockets opened (0) Listening sockets closed (0) Files added in /etc (7) /etc/letsencrypt/accounts/acme-staging-v02.api.letsencrypt.org/directory/a16f18d298b77860f18c1668bf869b62/meta.json /etc/letsencrypt/accounts/acme-staging-v02.api.letsencrypt.org/directory/a16f18d298b77860f18c1668bf869b62/private_key.json /etc/letsencrypt/accounts/acme-staging-v02.api.letsencrypt.org/directory/a16f18d298b77860f18c1668bf869b62/regr.json /etc/letsencrypt/archive/example.com/cert2.pem /etc/letsencrypt/archive/example.com/chain2.pem /etc/letsencrypt/archive/example.com/fullchain2.pem /etc/letsencrypt/archive/example.com/privkey2.pem Files changed in /etc (5) /etc/letsencrypt/live/example.com/cert.pem /etc/letsencrypt/live/example.com/chain.pem /etc/letsencrypt/live/example.com/fullchain.pem /etc/letsencrypt/live/example.com/privkey.pem /etc/nginx/sites-available/example.com Files removed from /etc (0) Disk used on / (includes logs written during the run): +0.4 MB ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Audit: testssl ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Check-your-server script (2 items): https://cdn.serverhandbook.com/check/nginx-redirect-www-non-www-https-ubuntu/ubuntu-26.04.1-20261118t233849z.sh SHA-256 3dde059e26e383fe80e2464061dfc0dfa9b54064c3e78659a347163fe4fb3d28 ------------------------------------------------------------------------------ On the stock server, before the first step: FAIL http://www. sends visitors to https:// PASS the nginx config tests clean After the last step: PASS http://www. sends visitors to https:// PASS the nginx config tests clean ------------------------------------------------------------------------------ Claims (36), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 26.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-16. | 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 hostname '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 webpage 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 hostname '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 webpage 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 hostname '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 webpage 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 hostname '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 webpage 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-16 22:41:33+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-16. | 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-16. | 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-16 22:42:29+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; # managed by Certbot | listen [::]:443 ssl ipv6only=on; # 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 48 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 (the config tests clean at the end of the steps) | nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | nginx: configuration file /etc/nginx/nginx.conf test is successful 28. ok (http://www goes to the https main name) | Location: https://example.com/ 29. 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 30. 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 31. 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 32. 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/ 33. ok (tested on) the box reports its OS, kernel and the version of 4 package(s) | PRETTY_NAME="Ubuntu 26.04.1 LTS" | VIRT=none | KERNEL=7.0.0-30-generic | PKG certbot 4.0.0-4 | PKG curl 8.18.0-1ubuntu2.4 | PKG nginx 1.28.3-2ubuntu1.12 | PKG python3-certbot-nginx 4.0.0-3 34. 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 35. 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 36. 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 hostname '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 webpage 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-16 22:41:33+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-16. 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-16 22:42:29+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; # managed by Certbot listen [::]:443 ssl ipv6only=on; # 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 48 $ 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-your-server script, its full screen before the steps ------------------------------------------------------------------------------ ServerHandbook check, nginx-redirect-www-non-www-https-ubuntu (proved 2026-11-18) FAIL http://www. sends visitors to https:// fix: https://serverhandbook.com/kb/nginx-redirect-www-non-www-https-ubuntu#step-4-check PASS the nginx config tests clean 1 passed, 1 failed To fix a FAIL, follow the steps in https://serverhandbook.com/kb/nginx-redirect-www-non-www-https-ubuntu ------------------------------------------------------------------------------ The check-your-server script, its full screen after the steps ------------------------------------------------------------------------------ ServerHandbook check, nginx-redirect-www-non-www-https-ubuntu (proved 2026-11-18) PASS http://www. sends visitors to https:// PASS the nginx config tests clean 2 passed, 0 failed ------------------------------------------------------------------------------ testssl: its full screen before the first step ------------------------------------------------------------------------------ (not run before the steps: no HTTPS yet) ------------------------------------------------------------------------------ testssl: its full screen after the last step ------------------------------------------------------------------------------ Start 2026-11-18 23:43:49 -->> 203.0.113.10:443 (example.com) <<-- rDNS (203.0.113.10): . Service detected: HTTP Testing protocols via sockets except NPN+ALPN SSLv2 not offered (OK) SSLv3 not offered (OK) TLS 1 not offered TLS 1.1 not offered TLS 1.2 offered (OK) TLS 1.3 offered (OK): final NPN/SPDY not offered ALPN/HTTP2 http/1.1 (offered) Testing cipher categories NULL ciphers (no encryption) not offered (OK) Anonymous NULL Ciphers (no authentication) not offered (OK) Export ciphers (w/o ADH+NULL) not offered (OK) LOW: 64 Bit + DES, RC[2,4], MD5 (w/o export) not offered (OK) Triple DES Ciphers / IDEA not offered Obsoleted CBC ciphers (AES, ARIA etc.) not offered Strong encryption (AEAD ciphers) with no FS not offered Forward Secrecy strong encryption (AEAD ciphers) offered (OK) Testing server's cipher preferences Hexcode Cipher Suite Name (OpenSSL) KeyExch. Encryption Bits Cipher Suite Name (IANA/RFC) ----------------------------------------------------------------------------------------------------------------------------- SSLv2 - SSLv3 - TLSv1 - TLSv1.1 - TLSv1.2 (no server order, thus listed by strength) xc02c ECDHE-ECDSA-AES256-GCM-SHA384 ECDH 521 AESGCM 256 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 xcca9 ECDHE-ECDSA-CHACHA20-POLY1305 ECDH 521 ChaCha20 256 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 xc02b ECDHE-ECDSA-AES128-GCM-SHA256 ECDH 521 AESGCM 128 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 TLSv1.3 (no server order, thus listed by strength) x1302 TLS_AES_256_GCM_SHA384 ECDH/MLKEM AESGCM 256 TLS_AES_256_GCM_SHA384 x1303 TLS_CHACHA20_POLY1305_SHA256 ECDH/MLKEM ChaCha20 256 TLS_CHACHA20_POLY1305_SHA256 x1301 TLS_AES_128_GCM_SHA256 ECDH/MLKEM AESGCM 128 TLS_AES_128_GCM_SHA256 Has server cipher order? no (limited sense as client will pick) Testing robust forward secrecy (FS) -- omitting Null Authentication/Encryption, 3DES, RC4 FS is offered (OK) TLS_AES_256_GCM_SHA384 TLS_CHACHA20_POLY1305_SHA256 ECDHE-ECDSA-AES256-GCM-SHA384 ECDHE-ECDSA-CHACHA20-POLY1305 TLS_AES_128_GCM_SHA256 ECDHE-ECDSA-AES128-GCM-SHA256 KEMs offered X25519MLKEM768 Elliptic curves offered: prime256v1 secp384r1 secp521r1 X25519 X448 Finite field group: ffdhe2048 ffdhe3072 TLS 1.2 sig_algs offered: ECDSA+SHA512 ECDSA+SHA384 ECDSA+SHA256 ECDSA+SHA224 TLS 1.3 sig_algs offered: ECDSA+SHA256 Testing server defaults (Server Hello) TLS extensions (standard) "server name/#0" "max fragment length/#1" "supported_groups/#10" "EC point formats/#11" "application layer protocol negotiation/#16" "extended master secret/#23" "supported versions/#43" "key share/#51" "renegotiation info/#65281" Session Ticket RFC 5077 hint no -- no lifetime advertised SSL Session ID support yes Session Resumption Tickets no, ID: yes TLS clock skew Random values, no fingerprinting possible Certificate Compression none Client Authentication none Signature Algorithm ECDSA with SHA384 Server key size EC 256 bits (curve P-256) Server key usage Digital Signature Server extended key usage TLS Web Server Authentication Serial 05A7C6823D2AD97644D4D864D9407DF7177A (OK: length 18) Fingerprints SHA1 1CE39804187384B53108A4661EF62F09F5300CF0 SHA256 71149B8C1F06132F64C51934D9F6F601375309038E94C3303D8B83FEA7686977 Common Name (CN) example.com subjectAltName (SAN) example.com www.example.com Trust (hostname) Ok via SAN and CN (same w/o SNI) Chain of trust Ok EV cert (experimental) no Certificate Validity (UTC) 89 >= 30 days (2026-11-18 22:42 --> 2027-02-16 22:42) ETS/"eTLS", visibility info not present Certificate Revocation List http://ye1.c.lencr.org/123.crl OCSP URI -- OCSP stapling not offered OCSP must staple extension -- DNS CAA RR (experimental) not offered Certificate Transparency yes (certificate extension) Certificates provided 4 Issuer YE1 (Let's Encrypt from US) Intermediate cert validity #1: ok > 40 days (2028-09-02 23:59). YE1 <-- Root YE #2: ok > 40 days (2032-09-02 23:59). Root YE <-- ISRG Root X2 #3: ok > 40 days (2032-09-02 23:59). ISRG Root X2 <-- ISRG Root X1 Intermediate Bad OCSP (exp.) Ok Testing HTTP header response @ "/" HTTP Status Code 200 OK HTTP clock skew 0 sec from localtime Strict Transport Security 365 days=31536000 s, just this domain Public Key Pinning -- Server banner nginx/1.28.3 (Ubuntu) Application banner -- Cookie(s) (none issued at "/") Security headers -- Reverse Proxy banner -- Testing vulnerabilities Heartbleed (CVE-2014-0160) not vulnerable (OK), no heartbeat extension CCS (CVE-2014-0224) not vulnerable (OK) Ticketbleed (CVE-2016-9244), experiment. not vulnerable (OK), no session ticket extension ROBOT Server does not support any cipher suites that use RSA key transport Secure Renegotiation (RFC 5746) supported (OK) Secure Client-Initiated Renegotiation not vulnerable (OK) CRIME, TLS (CVE-2012-4929) not vulnerable (OK) BREACH (CVE-2013-3587) potentially NOT ok, "gzip" HTTP compression detected. - only supplied "/" tested Can be ignored for static pages or if no secrets in the page POODLE, SSL (CVE-2014-3566) not vulnerable (OK), no SSLv3 support TLS_FALLBACK_SCSV (RFC 7507) No fallback possible (OK), no protocol below TLS 1.2 offered SWEET32 (CVE-2016-2183, CVE-2016-6329) not vulnerable (OK) FREAK (CVE-2015-0204) not vulnerable (OK) DROWN (CVE-2016-0800, CVE-2016-0703) not vulnerable on this host and port (OK) no RSA certificate, thus certificate can't be used with SSLv2 elsewhere LOGJAM (CVE-2015-4000), experimental not vulnerable (OK): no DH EXPORT ciphers, no DH key detected with <= TLS 1.2 BEAST (CVE-2011-3389) not vulnerable (OK), no SSL3 or TLS1 LUCKY13 (CVE-2013-0169), experimental not vulnerable (OK) Winshock (CVE-2014-6321), experimental not vulnerable (OK) RC4 (CVE-2013-2566, CVE-2015-2808) no RC4 ciphers detected (OK) Running client simulations (HTTP) via sockets Browser Protocol Cipher Suite Name (OpenSSL) Forward Secrecy ------------------------------------------------------------------------------------------------ Android 7.0 (native) TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 256 bit ECDH (P-256) Android 8.1 (native) TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 253 bit ECDH (X25519) Android 9.0 (native) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Android 10.0 (native) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Android 11/12 (native) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Android 13/14 (native) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Android 15 (native) TLSv1.3 TLS_AES_128_GCM_SHA256 X25519MLKEM768 Chrome 101 (Win 10) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Chromium 137 (Win 11) TLSv1.3 TLS_AES_128_GCM_SHA256 X25519MLKEM768 Firefox 100 (Win 10) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Firefox 137 (Win 11) TLSv1.3 TLS_AES_128_GCM_SHA256 X25519MLKEM768 IE 8 Win 7 No connection IE 11 Win 7 TLSv1.2 ECDHE-ECDSA-AES256-GCM-SHA384 256 bit ECDH (P-256) IE 11 Win 8.1 TLSv1.2 ECDHE-ECDSA-AES256-GCM-SHA384 256 bit ECDH (P-256) IE 11 Win Phone 8.1 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 256 bit ECDH (P-256) IE 11 Win 10 TLSv1.2 ECDHE-ECDSA-AES256-GCM-SHA384 256 bit ECDH (P-256) Edge 15 Win 10 TLSv1.2 ECDHE-ECDSA-AES256-GCM-SHA384 253 bit ECDH (X25519) Edge 101 Win 10 21H2 TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Edge 133 Win 11 23H2 TLSv1.3 TLS_AES_128_GCM_SHA256 X25519MLKEM768 Safari 18.4 (iOS 18.4) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Safari 15.4 (macOS 12.3.1) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Safari 18.4 (macOS 15.4) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Java 7u25 No connection Java 8u442 (OpenJDK) TLSv1.3 TLS_AES_256_GCM_SHA384 253 bit ECDH (X25519) Java 11.0.2 (OpenJDK) TLSv1.3 TLS_AES_128_GCM_SHA256 256 bit ECDH (P-256) Java 17.0.3 (OpenJDK) TLSv1.3 TLS_AES_256_GCM_SHA384 253 bit ECDH (X25519) Java 21.0.6 (OpenJDK) TLSv1.3 TLS_AES_256_GCM_SHA384 253 bit ECDH (X25519) go 1.17.8 TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) LibreSSL 3.3.6 (macOS) TLSv1.3 TLS_CHACHA20_POLY1305_SHA256 253 bit ECDH (X25519) OpenSSL 1.0.2e TLSv1.2 ECDHE-ECDSA-AES256-GCM-SHA384 256 bit ECDH (P-256) OpenSSL 1.1.1d (Debian) TLSv1.3 TLS_AES_256_GCM_SHA384 253 bit ECDH (X25519) OpenSSL 3.0.15 (Debian) TLSv1.3 TLS_AES_256_GCM_SHA384 253 bit ECDH (X25519) OpenSSL 3.5.0 (git) TLSv1.3 TLS_AES_256_GCM_SHA384 X25519MLKEM768 Apple Mail (16.0) TLSv1.2 ECDHE-ECDSA-AES256-GCM-SHA384 256 bit ECDH (P-256) Thunderbird (91.9) TLSv1.3 TLS_AES_128_GCM_SHA256 253 bit ECDH (X25519) Rating (experimental) Rating specs (not complete) SSL Labs's 'SSL Server Rating Guide' (version 2009r from 2025-05-16) Specification documentation https://github.com/ssllabs/research/wiki/SSL-Server-Rating-Guide Protocol Support (weighted) 100 (30) Key Exchange (weighted) 100 (30) Cipher Strength (weighted) 90 (36) Final Score 96 Overall Grade A+ Done 2026-11-18 23:44:51 [ 64s] -->> 203.0.113.10:443 (example.com) <<-- ------------------------------------------------------------------------------ The check's source (scripts/live/checks/nginx-redirect-www-non-www-https-ubuntu.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB: redirect www to non-www and HTTP to HTTPS in Nginx on Ubuntu 26.04 and 24.04 (run per release; proof standard v2 * 2026-11-18: testssl.sh from a second server after the steps) (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 { SCANNER, VPS, VPS_DOMAIN, onScanner } from '../box.mjs' import { bareBox, reportBox } from '../lib.mjs' const STAGING = process.env.LIVE_LE_STAGING === '1' export const meta = { box: 'vps', // needs the rented server (npm run retest puts it in the server lane; real-server skill) saveRun: !STAGING, audits: ['testssl'], // The reader's check-your-server script (plan05 phase 3): proved by the runner on the stock server and after the steps. reader: [ { item: "http://www. sends visitors to https://", cmd: "curl -sI http://www. | grep -i '^location'", expect: "^[Ll]ocation: https://", step: "step-4-check" }, { item: "the nginx config tests clean", cmd: "sudo nginx -t 2>&1", expect: "test is successful", step: "step-4-check" }, ], } const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04' /** 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. */ // "From another network": the outside scanner when there is one (the laptop's own network is not reliable), tried up // to 3 times so one lost packet on the internet does not fail the run. const curlOnce = (url) => { const a = ['-s', ...(STAGING ? ['-k'] : []), '-o', '/dev/null', '-m', '10', '-w', "'%{http_code} %{redirect_url}'", url.split('example.com').join(VPS_DOMAIN)] const raw = SCANNER ? onScanner(`curl ${a.join(' ')}`, 60000).out : (spawnSync('curl', a.map((x) => x.replace(/^'|'$/g, '')), { encoding: 'utf8' }).stdout ?? '') return raw.split(VPS_DOMAIN).join('example.com').trim() } const fromOutside = (url) => { let r = '' for (let i = 0; i < 3 && !/^[1-5]\d\d/.test(r); i++) r = curlOnce(url) return r } 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 ────────────────────────────────────────────────────────── // a public server also gets stray bot requests: if one lands after our POST, send it again (up to 3 times) let log301 for (let tryNo = 0; tryNo < 3; tryNo++) { rc(`curl -s -o /dev/null -L -d 'name=test' http://www.example.com/`, { id: 'post-301' }) log301 = run("sudo tail -n 2 /var/log/nginx/access.log | cut -d'\"' -f2,3", { id: 'log-301' }) const o = log301.out if (/POST \/ HTTP\/1\.1" 301/.test(o) && /GET \/ HTTP\/1\.1" 200/.test(o)) break } 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) // a public server also gets stray bot requests: if one lands after our POST, send it again (up to 3 times) let log308 for (let tryNo = 0; tryNo < 3; tryNo++) { rc(`curl -s -o /dev/null -L -d 'name=test' http://www.example.com/`, { id: 'post-308' }) log308 = run("sudo tail -n 2 /var/log/nginx/access.log | cut -d'\"' -f2,3", { id: 'log-308' }) const o = log308.out if (/POST \/ HTTP\/1\.1" 308/.test(o) && /POST \/ HTTP\/1\.1" 405/.test(o)) break } 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) const tEnd = L('sudo nginx -t 2>&1') ctx.check('(the config tests clean at the end of the steps)', /test is successful/.test(tEnd), tEnd, { reader: { cmd: 'sudo nginx -t 2>&1', expect: 'test is successful' } }) const loc = L("curl -sI http://www.example.com | grep -i '^location'") ctx.check('(http://www goes to the https main name)', /^location: https:\/\/example\.com\//i.test(loc), loc, { reader: { cmd: "curl -sI http://www. | grep -i '^location'", expect: '^[Ll]ocation: https://' }, }) ctx.stepsDone() // the article's main setup ends here (non-www, HTTPS, HSTS); www-as-main, the loop and the undo follow // ── 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 ${RELEASE} 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