nginxnginx-config

Redirect of www and http/s help in nginx


I've been working to understand nginx more from the configuration side. The thing I'm struggling with the most is primary redirect processes. Between handling the https and www redirects, my head is swimming. Ultimately, I'm looking to redirect all requests to https://hostname.com. Below is the planned config that I'm trying to make sure I'm on the right track. Note the comments are there to assist future me. Any help is appreciated!

server {
    # Ports to listen on
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    
    # Server name to listen for
    server_name myhostname.com www.myhostname.com;
    
    # Redirect www to non-www
    return 301 https://myhostname.com$request_uri;
    
    # Path to document root
    root /var/www/html/host_folder;
    
    # File to be used as index
    index index.php;
    
    # Overrides logs defined in nginx.conf, allows per site logs.
    access_log /var/www/html/host_folder/logs/access.log;
    error_log /var/www/html/host_folder/logs/error.log;
    
    # Default server block rules
    include global/server/defaults.conf;
    
    # SSL rules
    include global/server/ssl.conf;
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

server {
    # Redirect non https requests
    listen  80 default_server;
    listen  [::]:80 default_server;
    server_name localhost;
    
    location / {
        return 301 https://$host$request_uri;
    }
}

Solution

  • Nginx processes server blocks based on listen and server_name directives.

    Inside a server block, the return directive is processed before location blocks.

    Please check revised configuration that should achieve your needs :

    
    # Server block for HTTP (port 80) – Handles HTTP to HTTPS redirect
    server {
        listen 80;
        listen [::]:80;
        server_name myhostname.com www.myhostname.com;
    
        return 301 https://$host$request_uri;
    }
    
    # Server block for HTTPS (port 443) – Handles www to non-www redirect and serves content
    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
    
        server_name www.myhostname.com;
    
        return 301 https://myhostname.com$request_uri;
    
        include global/server/ssl.conf;
    }
    
    
    server {
        # This server block is for the final, desired destination: https://myhostname.com
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
    
        server_name myhostname.com; # Only listen for the non-www HTTPS hostname
    
        # Path to document root
        root /var/www/html/host_folder;
    
        index index.php;
    
        access_log /var/www/html/host_folder/logs/access.log;
        error_log /var/www/html/host_folder/logs/error.log;
    
        # Default server block rules
        include global/server/defaults.conf;
    
        # SSL rules
        include global/server/ssl.conf;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    }