HTTPS encrypts communication between a visitor’s browser and your web server.
If your website supports HTTPS, visitors who open the HTTP version should generally be redirected to the secure version.
For websites using Nginx, this can be accomplished with a simple server configuration.
In this guide, you’ll learn how to configure an HTTP-to-HTTPS redirect, test it, and avoid common mistakes.
Why Redirect HTTP to HTTPS?
HTTPS provides several important benefits:
- Encrypts data transmitted between browsers and servers
- Helps protect information from interception or modification in transit
- Enables secure browser features
- Provides a consistent preferred URL for your website
- Avoids maintaining separate HTTP and HTTPS versions of the same content
Google also recommends using HTTPS for websites.
However, HTTPS alone does not guarantee higher search rankings.
Step 1: Make Sure HTTPS Works
Before configuring a redirect, confirm that your website already has a valid TLS certificate.
Open:
If the website loads successfully without a certificate warning, you can proceed.
If HTTPS is not working, install and configure a certificate first.
Let’s Encrypt provides free TLS certificates, and Certbot is one common tool for managing them.
Your hosting control panel may also offer automated certificate installation.
Step 2: Find Your Nginx Configuration
On many Linux distributions, Nginx configuration files are stored in:
/etc/nginx/
Ubuntu and Debian installations often use:
/etc/nginx/sites-available/
Other installations may use:
/etc/nginx/conf.d/
To see the active configuration, run:
sudo nginx -T
Be aware that the output may include sensitive configuration values. Do not publish the full output without reviewing it.
Locate the server block for your domain.
Step 3: Add an HTTP-to-HTTPS Redirect
For a standard website, you can configure a dedicated HTTP server block:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
Replace example.com with your domain.
This configuration listens for HTTP requests and redirects them to HTTPS.
For example:
http://example.com/about/
becomes:
The $request_uri variable preserves the requested path and query string.
The $host variable uses the normalized request hostname. Make sure the accepted hostnames are explicitly configured and supported by your HTTPS certificate.
For a website with one canonical hostname, using that hostname directly in the redirect is often clearer.
For example:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
This redirects both hostnames to the non-www HTTPS version.
Step 4: Test the Nginx Configuration
Before applying your changes, run:
sudo nginx -t
If Nginx reports that the configuration is valid, reload the service:
sudo systemctl reload nginx
If the test fails, correct the reported error before reloading.
Step 5: Verify the Redirect
You can use curl to inspect the HTTP response:
curl -I http://example.com
A successful permanent redirect should return a response similar to:
HTTP/1.1 301 Moved Permanently
Location: https://example.com/
You can also test a specific path:
curl -I http://example.com/contact/
Confirm that the Location header points to the correct HTTPS URL.
Step 6: Update WordPress URLs
If your website uses WordPress, open:
Settings → General
Check these fields:
- WordPress Address (URL)
- Site Address (URL)
Both should normally use HTTPS when your WordPress installation is configured to serve the site securely.
For example:
Before changing these settings, ensure HTTPS is working and you have a recovery method in case the dashboard becomes inaccessible.
If your site contains old HTTP links, update them carefully using a WordPress-aware migration tool rather than running unsafe text replacements on serialized database data.
Common Problems
Too Many Redirects
Redirect loops can occur when Nginx, a CDN, or WordPress repeatedly redirects requests between HTTP and HTTPS.
Check your reverse proxy settings and ensure WordPress correctly recognizes secure connections.
HTTPS Certificate Warning
A redirect does not fix an invalid certificate.
Make sure the certificate covers the hostname visitors are redirected to.
Mixed Content Warnings
Mixed content occurs when an HTTPS page requests resources over HTTP.
Update resource URLs to HTTPS where supported.
Redirect Works for One Hostname Only
Check the server_name directive and confirm that all intended hostnames are covered.
If your website supports IPv6, also verify the relevant IPv6 listener configuration.
Should You Use a 301 or 302 Redirect?
A 301 redirect indicates that a resource has permanently moved.
A 302 redirect indicates a temporary move.
For a permanent HTTP-to-HTTPS migration, a 301 redirect is generally appropriate.
A 308 redirect is another permanent redirect option and explicitly preserves the HTTP method.
For a typical website migration, 301 is widely supported.
Frequently Asked Questions
Does Nginx automatically redirect HTTP to HTTPS?
No. You generally need to configure a redirect yourself or use a hosting tool that configures it for you.
Do I need a WordPress plugin for HTTPS redirects?
Not necessarily. If you control Nginx, a server-level redirect is usually sufficient.
Will HTTP-to-HTTPS redirects affect SEO?
A properly configured permanent redirect helps search engines consolidate signals on the preferred HTTPS URL. Indexing and ranking changes can take time.
Conclusion
Redirecting HTTP to HTTPS in Nginx is straightforward once a valid TLS certificate is installed.
Create an HTTP server block, return a permanent redirect, test the configuration, and verify the response using curl.
Finally, check your website’s internal links, canonical URLs, and WordPress settings to ensure they consistently use HTTPS.
Leave a Reply