Support > About cybersecurity > How to Use Forced HTTPS Redirection in Nginx
How to Use Forced HTTPS Redirection in Nginx
Time : 2025-04-17 15:14:10
Edit : Jtti

Modern browsers warn of dangers in HTTP plaintext requests, making forced HTTPS no longer an advanced option, but a basic configuration for websites. If you use Nginx to build a website, how to elegantly and securely configure HTTPS automatic redirection?

As a high-performance reverse proxy server, Nginx can listen to different ports and protocols through server blocks. Its redirection logic usually listens to HTTP requests on port 80, detects whether the domain name access is non-HTTPS, and permanently redirects to the HTTPS address with 301. This practice is called HTTP→HTTPS forced redirection, which is beneficial to SEO and improves user access experience.

Before formal configuration, make sure you have completed the following tasks:

Standard configuration method: Forced HTTPS redirection in one step

Assume that your website domain name is example.com and the SSL certificate is ready. The following is a standard two-section configuration.

HTTP request listening and redirection configuration

server {
    listen 80;
    server_name example.com www.example.com;

    # Redirect all HTTP requests to HTTPS (permanent redirect)
    return 301 https://$host$request_uri;
}

This code means that all requests to port 80 (whether the root directory or a sub-path) will be permanently redirected to the corresponding HTTPS address.

HTTPS service configuration (SSL enabled)

server {
    listen 80;
    server_name example.com www.example.com;

    # Redirect all HTTP requests to HTTPS (permanent redirect)
    return 301 https://$host$request_uri;
}

After configuration is complete:

http://example.com → automatically redirects to https://example.com

Users do not need to remember ports and protocols, and automatically enter encrypted connections

Multiple domain names, multiple ports, and HSTS settings

Scenario 1: Unified redirection of multiple domain names

server {
    listen 80 default_server;
    server_name _;
    return 301 https://example.com$request_uri;
}

It can be used to redirect all "unknown domain name" requests to the HTTPS page of the main domain name.

Scenario 2: Add HSTS security header (force browser to use HTTPS)

add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

max-age=63072000: Force HTTPS for 2 years

includeSubDomains: also applies to subdomains

preload: can be submitted to the browser HSTS list (verification required)

Note: once HSTS is enabled, it cannot be rolled back in the short term, so make sure that the SSL certificate remains valid.

Configuration verification and testing method

After completing the Nginx configuration, use the following method to test whether it is correct:

Local curl to check redirection:

curl -I http://example.com

The output should contain:

HTTP/1.1 301 Moved Permanently
Location: https://example.com/

Browser access:

Visit http://example.com directly to see if it automatically redirects and displays a security lock.

Enabling HTTPS in Nginx is an important step to protect user privacy and website data integrity. By simply adjusting the configuration file, you can gain browser trust, improve SEO weight, prevent man-in-the-middle attacks, and build a professional and credible website image. As a developer or operation and maintenance engineer, mastering HTTPS redirection is not only a skill, but also a responsibility to users.

Relevant contents

How to implement gRPC forwarding in Nginx configuration What are the selection criteria for an e-commerce website server Methods to determine whether an IP address is a native IP address Transit node definition and analysis of its influence on network connection quality How to use local SSH configuration in SSH connection What is the relationship between native IP and virtual IP? 10 Root Causes of IP Access Failure but Routable and Actual Diagnosis Why is Game Shield the core strategic value of the game industry Cause Analysis of remote server Blue Screen of Death and actual Defense guide How to set IP whitelist and geoblock in CDN security policy
Go back

24/7/365 support.We work when you work

Support