Skip to main content
In this lesson you’ll configure NGINX to redirect all plain HTTP traffic to HTTPS using the return directive. This forces clients connecting on port 80 to be redirected to port 443 so all requests are encrypted. We have a simple “Diner” app served from port 80 on the host. The steps covered here:
  • Check firewall rules.
  • Allow inbound HTTPS (port 443).
  • Create a single NGINX config containing two server blocks:
    • one to redirect HTTP → HTTPS (301),
    • one to serve the site over HTTPS with TLS certificates.
  • Enable the site, validate and reload NGINX.
  • Verify the redirect using curl and a browser.
An illustration of a desktop computer and a web page with a padlock icon between them and the caption "It's the secure version of HTTP." It represents an encrypted HTTPS connection protecting data between a browser and a website.

1. Confirm current firewall status

Check which ports are allowed so you can open port 443 before enabling HTTPS:
Port 443 is not listed, so HTTPS requests will fail until it is allowed. Enable HTTPS (TCP 443):

2. Understand what happens if port 443 is open but NGINX isn’t serving HTTPS

If port 443 is allowed but NGINX has no TLS server block for the site, the browser may show a connection error or an upstream proxy might return a 502 Bad Gateway. Example browser output:
A browser window showing a "502 Bad Gateway" error page. The page is mostly blank and displays "nginx/1.27.2" under the error message.

3. Create the combined NGINX configuration

We keep the configuration DRY by putting two server blocks in the same file:
  • A lightweight port 80 server block that issues a permanent 301 redirect to the same host and URI on https://.
  • A port 443 server block that enables TLS and serves the app files.
Create or edit /etc/nginx/sites-available/diner-https with the following:
Notes about this configuration:
  • The return 301 https://$host$request_uri; preserves the hostname, path and query string so http://diner.com/some/path?x=1 becomes https://diner.com/some/path?x=1.
  • The second server block enables ssl, and points to the certificate and key files used for TLS.
  • The example certificate/key paths are present for this exercise. In production obtain valid certificates (for example via Let’s Encrypt) and reference them here.
Using a 301 Moved Permanently response will cause clients and search engines to cache the redirect. Use 302 Found during testing if you expect to change behavior later, then switch to 301 once everything is final.

4. Enable the site and reload NGINX

Create the symlink in sites-enabled:
Validate the NGINX configuration and reload:

5. Verify the redirect and HTTPS behavior

Test the HTTP → HTTPS redirect locally with curl. Check only headers (should show 301 and Location header):
A plain curl will show the HTML 301 page:
With port 443 allowed and NGINX serving your TLS server block, visiting https://<your-host> in a browser should load the site over HTTPS (no 502). If a user visits http://diner.com/some/path, the return 301 https://$host$request_uri; will redirect them to https://diner.com/some/path, preserving the full path and query string.
Make sure the TLS certificate and key are valid for the server_name you use. An invalid certificate will produce browser warnings even if the redirect is correct.

Quick checklist

Recap

  • Use a simple server block on port 80 to issue a 301 redirect to HTTPS.
  • Serve the site on port 443 with TLS configured.
  • Ensure the firewall allows port 443 before relying on HTTPS.
  • Validate the NGINX config and reload before testing to avoid downtime.
Further reading:

Watch Video