How to Redirect HTTP to HTTPS in Nginx

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Secure your site by forcing HTTPS with these simple Nginx redirect rules

If your website is still accessible via HTTP, it’s time to fix that. Redirecting HTTP to HTTPS is crucial for website security, SEO, and user trust. Whether you’re using Nginx, the process is straightforward with just a few lines of configuration. This guide shows you exactly how to set it up.

How to redirect HTTP to HTTPS on NGINX

Why Redirect to HTTPS?

Before we dive into configs, here’s why HTTPS matters:

  • Encryption: HTTPS encrypts data between the user and your server.
  • SEO boost: Google gives preference to HTTPS-enabled sites.
  • Trust signals: Modern browsers warn users about non-secure HTTP connections.
  • Compliance: HTTPS is a must for PCI-DSS, GDPR, and other standards.

Redirect HTTP to HTTPS in Nginx

Step-by-Step Nginx HTTPS Redirect

To redirect all HTTP traffic to HTTPS in Nginx, you’ll typically use a server block listening on port 80 to perform the redirect.
Here’s the recommended config:

server {
listen 80;
server_name yourdomain.tld www.yourdomain.tld;
return 301 https://$host$request_uri;
}

What this does:

  • Listens on port 80 (HTTP).
  • Redirects all requests to HTTPS using a 301 (permanent) redirect.
  • Maintains the original host and URI path.

Where to Place This

  • Inside your Nginx config file (commonly at /etc/nginx/nginx.conf or /etc/nginx/sites-available/yourdomain.tld).
  • Make sure your HTTPS server block (port 443) is already set up with a valid SSL certificate.

Restart Nginx

sudo nginx -t
sudo systemctl reload nginx

Common Pitfalls to Avoid

  • Missing SSL certificate: The redirect won’t work if HTTPS isn’t properly configured.
  • Redirect loops: Double-check that you’re not redirecting HTTPS back to HTTP elsewhere.
  • Caching: Browsers might cache redirects. Use private or no-cache headers while testing.

Bonus Tip: Test Your Redirect

Once configured, test your redirect using:

  • Browser – (visit http://yourdomain.tld and verify it redirects to https://.
  • SSL Checker Tool
  • curl command:
    curl -I http://yourdomain.tld/

    Look for:

    HTTP/1.1 301 Moved Permanently
    Location: https://yourdomain.tld/

Final Thoughts

Redirecting HTTP to HTTPS in Nginx is fast, easy, and essential. Use the examples in this guide to force secure connections for all your users and avoid SEO or browser trust issues.

Don’t Have an SSL Certificate Yet?

You’ll need one to serve HTTPS. We recommend starting with a low-cost, reliable option:

Also check out: How to Redirect HTTP to HTTPS in Apache