SoFunction
Updated on 2025-04-20

How to redirect port 80 to port 443 using Nginx configuration

To configure Nginx to redirect HTTP (port 80) requests to HTTPS (port 443), follow these steps:

1. Create or edit Nginx configuration file

Usually the configuration file is located in/etc/nginx/sites-available/, you need to modify or create the corresponding configuration file (such asdefaultor your site configuration).

2. Configure HTTP redirection to HTTPS

Add the following to the configuration file to redirect HTTP requests to HTTPS:

server {
    listen 80;
    listen [::]:80;  # Support IPv6    server_name  ;  # Replace with your domain name
    # Force redirect to HTTPS    return 301 https://$host$request_uri;
}

3. Configure HTTPS server block

Add or modify the configuration of port 443, enable SSL and specify the certificate path:

server {
    listen 443 ssl;
    listen [::]:443 ssl;  # Support IPv6    server_name  ;

    # SSL certificate configuration    ssl_certificate /etc/letsencrypt/live//;  # Replace with your certificate path    ssl_certificate_key /etc/letsencrypt/live//;  # Replace with private key path
    # Recommended SSL configuration (enhanced security)    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    # HSTS (optional but recommended)    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    # Website root directory and other configurations    root /var/www/html;
    index  ;

    location / {
        try_files $uri $uri/ =404;
    }
}

4. Check the firewall settings

Make sure the server firewall allows HTTP (80) and HTTPS (443) ports:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload

5. Test and apply configuration

# Check whether the configuration syntax is correctsudo nginx -t

# Reload Nginx to make the configuration take effectsudo systemctl reload nginx

6. Verify the configuration

access, it should automatically jump to

Use the browser developer tool to check the network request and confirm that the return status code is 301 or 307.

Use SSL detection tools (e.g.SSL Labs) Check HTTPS configuration security.

Things to note

Certificate path:make suressl_certificateandssl_certificate_keyPoint to the correct certificate and private key file (such as Let's Encrypt certificate).

Multi-domain name support: If you need to process multiple domain names,server_nameAdd all relevant domain names, or use wildcards*.

IPv6 support: If the server enables IPv6, it must includelisten [::]:80;andlisten [::]:443 ssl;

HTTP/2: Added in HTTPS configurationhttp2To enable HTTP/2:

listen 443 ssl http2;
listen [::]:443 ssl http2;

Error handling: If you encounter a redirect loop, check whether the SSL configuration is correct, or temporarily comment on the HTTPS configuration to troubleshoot problems.

Method supplement

nginx 80 port redirect to port 443

nginx port 80 redirects to port 443, that is, http access will automatically jump to https

The configuration is as follows:

1. Modify the configuration file according to the following format, and port 80 will be automatically transferred to port 443, which will force encryption using SSL certificate. When accessing http, it will automatically jump to https.

server {
        server_name ;  # Domain Name        listen 80;                         
        rewrite ^(.*) https://$server_name$1 permanent;     
    }  

server {
        listen 443 ssl;
        listen [::]:443 ssl ipv6only=on;

        ssl_certificate /etc/letsencrypt/live//;
        ssl_certificate_key /etc/letsencrypt/live//;
        ssl_trusted_certificate /etc/letsencrypt/live//;

        .... Other configuration information


        
}

Note: ${server_name} can be replaced with $host

2. Restart nginx

3. Example (the following is the configuration we produced)

server {
    listen 80;
    server_name ;
    rewrite ^(.*)$ https://${server_name}$1 permanent; }

server {
    listen 443;
    server_name ;
    ssl on;
    ssl_certificate   /etc/pki/CA/certs/;
    ssl_certificate_key  /etc/pki/CA/certs/;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;   
    index   ;
    error_page 404 /;
    error_page 500 502 503 504 /;

    location ~ \.php {
    root /alidata/www/html;
    fastcgi_pass unix:/tmp/;
    fastcgi_index ;
    include   ;    set $path_info "";    set $fastcgi_script_name_new $fastcgi_script_name;        if ($fastcgi_script_name ~*   "^(.+\.php)(/.+)$"  ) {            set $fastcgi_script_name_new $1;        set $path_info $2;
    }

    fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name_new;
    fastcgi_param   SCRIPT_NAME   $fastcgi_script_name_new;                    
    fastcgi_param   PATH_INFO $path_info;
    }

    location / {
    root /alidata/www/html;
    index   ;    if (!-e  $request_filename){
        rewrite ^(.*)$ /$1 last;
    }
  }
}

This is the article about how to use Nginx configuration to redirect port 80 to port 443. For more information about Nginx port 80 redirect 443, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!