Serving a site over plain HTTP in 2026 is a problem for both security and trust. Browsers warn visitors, login forms and cookies travel unprotected, and search engines prefer the HTTPS version of a page. The fix is a redirect: every HTTP request should land on the HTTPS version of the same URL. This guide shows how to redirect HTTP to HTTPS in Nginx with three methods, so you can pick the one that fits your setup.
All three methods assume you already have a working HTTPS server block with a valid certificate. If you do not have one yet, set that up first, then add the redirect. A redirect to an HTTPS site that has no certificate just moves the error from HTTP to HTTPS.
Method 1: Redirect HTTP to HTTPS With a Return Statement
The simplest method is a dedicated HTTP server block that returns a 301 redirect to the HTTPS URL. The block listens on port 80, matches the domain, and sends every request to the same host and path over HTTPS. Because it uses return, Nginx does not need to process rewrites or check files on disk, which makes it fast and easy to read.
This nginx https redirect uses a 301 status, and an nginx server block redirect like this keeps the path intact. A 301 redirect nginx setup tells search engines the move is permanent. A typical block looks like this: listen on port 80, set the server name, then return 301 https://$host$request_uri;. The $host variable keeps the requested domain, and $request_uri keeps the full path and query string, so a visitor who asked for an old blog post over HTTP lands on that same post over HTTPS. Use 301, not 302, because the move is permanent and you want search engines to update their index to the HTTPS URL.
This method is the right default for most sites. It is explicit, it is easy to test with curl, and it fails in an obvious way if the server name is wrong. If you run several domains, you need one HTTP block per domain or a block that covers them, which leads to the server-block approach covered in the multiple-domains guide: setting up Nginx server blocks on Ubuntu.
Method 2: Redirect HTTP to HTTPS With a Rewrite Rule
An older method uses rewrite inside the HTTP server block to send requests to HTTPS. A common form checks the scheme and rewrites the URL to its HTTPS equivalent with a permanent flag. It works, but it is less direct than return because Nginx evaluates the rewrite as a regular expression before issuing the redirect.
There is one situation where rewrite still earns its place: when the redirect logic is conditional. If only some paths should move to HTTPS, or if the redirect depends on a variable that return alone cannot express, a rewrite with a condition gives you that control. For a plain whole-site redirect, prefer return. It does the same job with less processing and a config that is easier for the next person to understand.
Whichever method you pick, test the redirect before you call it done. Run curl -I http://example.com/page and check that the response is 301 with a Location header pointing at the HTTPS URL. A redirect chain, where HTTP redirects to HTTP again before reaching HTTPS, wastes a round trip and can confuse search engines, so check the full chain, not just the first hop.
Method 3: Let Certbot Add the Redirect for You
If you obtained your certificate with Certbot, you may not need to write the redirect by hand. When Certbot asks whether to redirect HTTP traffic to HTTPS, choosing yes adds a redirect to your Nginx config automatically. This is a good option when you want a working result fast and you are comfortable letting a tool edit your config.
The trade-off is control. Certbot writes its own redirect block, and if you later restructure your server blocks by hand, you need to check that the generated redirect still matches your layout. Read the generated block after Certbot runs so you know what changed. Our Certbot guide for Nginx walks through the certificate setup that this method builds on, including automatic renewal.
Redirect Loops and How to Avoid Them
The most common failure after adding a redirect is a loop: the browser reports too many redirects and the page never loads. A loop usually means one layer redirects HTTP to HTTPS while another layer redirects HTTPS back to HTTP, or the HTTPS block itself contains a redirect to HTTP. Cloudflare users hit a related version of this when the SSL mode is set to Flexible, because Cloudflare talks to the origin over HTTP while telling the browser the connection is HTTPS.
To diagnose a loop, run curl against the HTTPS URL and follow the Location headers. If the HTTPS response redirects back to HTTP, the problem is in your HTTPS block or in a proxy layer in front of it, not in the HTTP block. Our guide to the Nginx too many redirects error lists the usual causes in order, including proxy SSL settings and duplicate redirect blocks.
Another loop source is a redirect that drops the path. If every HTTP request lands on the homepage instead of the requested page, check that the redirect uses $request_uri and not a fixed homepage URL. Preserving the path matters for users with bookmarks and for search engines that indexed deep pages.
Testing and Keeping the Redirect Healthy
After any config change, run nginx -t before reloading. A syntax error in one server block stops the whole reload, and testing first keeps a typo from taking down every site on the server. Then reload with systemctl reload nginx and test both the homepage and a deep page over HTTP.
For long-term health, add the Strict-Transport-Security header on the HTTPS site so browsers remember to use HTTPS for future visits. HSTS is a separate step from the redirect, and it only helps after the first HTTPS visit, but it closes the gap for repeat visitors. Our Nginx security headers guide shows the header setup alongside the other headers worth adding. Finally, check certificate renewal still works after you add the redirect, because some renewal setups need HTTP access to a challenge path. The automatic renewal guide covers the challenge path detail that trips people up here.
Pick method one for a clean manual setup, method two only when you need conditional logic, and method three when Certbot already manages your certificates. Any of the three, tested with curl and kept free of loops, gives visitors the HTTPS site they expect.