>_ IzzyLab

Hands-on task

This task is step 13 of the course “New server: setup from scratch”

All of the shop's HTTP to HTTPS

medium~10 min

The shop already serves HTTPS on 443, but nothing listens on port 80 yet — old links like http://…/catalog/?page=2 lead nowhere. Set nginx up so that port 80 answers 301 to the same address over https://, keeping the host, path and query of the request.

What you'll practise:
  • nginx
  • return 301
  • $host
  • $request_uri
  • https

Topic breakdown

"Forced HTTPS" is a standard trick: port 80 serves nothing real and answers 301 Moved Permanently with a Location header holding the same address but with the https:// scheme. So that old links and bookmarks don't get lost, the redirect must keep the host, the path and the query string. In nginx every listening port is a server block of its own, so the working 443 block is left alone and a small block on 80 with return is added next to it. The target address is built from built-in variables — that way one redirect works for any host and path.

What's checked

  1. port 80 answers 301
  2. the redirect keeps the host, path and query
  3. HTTPS still serves the site

What it looks like