Score:0

How to solve non-www redirection to www Nginx?

fr flag

This is my NGinx Web Server configuration

    server {
        if ($host ~ ^[^.]+\.betafox\.net$) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = www.betafox.net) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = betafox.net) {
            return 301 https://$host$request_uri; 
        } # managed by Certbot
    
        listen 80;
        listen [::]:80;
    
        #server_name _;
        root /var/www/html;
    
    server_name betafox.net *.betafox.net;
        #return 301 https://$host$request_uri;
        index index.php index.html index.htm;
        location / {
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
            proxy_pass  https://betafox.net/;
            proxy_redirect  https://betafox.net/ $host;
            proxy_set_header Accept-Encoding "";
            proxy_ssl_server_name on;
        }
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            #fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass unix:/run/php/php8.0-fpm.sock;
            fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
     }
    
    
    }
    
    server {
    
    listen 443 ssl default_server;
            listen [::]:443 ssl default_server;
    
            root /var/www/html;
            index index.php index.html index.htm;
    
          # server_name _;
            server_name betafox.net *.betafox.net;
            # Maximum file upload size is 4MB - change accordingly if needed
            client_max_body_size 512M;
            client_body_buffer_size 128k;
            include snippets/letsencrypt-nginx-certs.conf;
            include snippets/letsencrypt-nginx-route.conf;
    
            location / {
                    # try_files $uri $uri/ =404;
                    try_files $uri $uri/ /index.php?q=$uri&$args;
            }
    error_page 404 /404.html;
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                    root /usr/share/nginx/html;
            }
    
            location ~ \.php$ {
                    include snippets/fastcgi-php.conf;
                    #fastcgi_pass 127.0.0.1:9000;
                    #fastcgi_pass unix:/var/run/php8.0-fpm.sock;
                    fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
            }
        ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot
    
    }

Most part of it was modified automatically by Certbot when I installed SSL Certificates for both my FQDN and Subdomains. The issue I'm facing is about URL redirection. The original URL is www.betafox.net, when user types betafox.net is redirected to https://betafoxnet.www.betafox.net/ and there's message that says: The site you were looking for, does not exist.

I only want that all users that type betafox.net are forwaded to www.betafox.net. I believe Nginx could do that. How can I achieve such thing?

djdomi avatar
za flag
remove the fiirst regex part i am really confused that certbot still uses if else instead a basic redirect
AtomX avatar
fr flag
Won't that affect subdomains SSL Cert? I've Wildcard SSL Certificate active
Score:1
us flag

Unfortunately Certbot creates the nginx redirects using if with the $host variable which is problematic.

It is best to have the redirect in a separate server section as follows.

# Redirect all requests to betafox.net URLs to corresponding www.betafox.net URLs
server {
    listen 80;
    listen 443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot

    server_name betafox.net;

    return 301 https://www.betafox.net$request_uri;
}

# Redirect all other subdomain HTTP requests to HTTPS.
server {
    listen 80;

    server_name *.betafox.net;

    return 301 https://$http_host$request_uri;:
}

# Removed the server block for port 80, it looked meaningless

server {
    # Removed the default_server, default_server should not be the actual website
    listen 443 ssl;
    listen [::]:443 ssl;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name betafox.net *.betafox.net;
    # Maximum file upload size is 4MB - change accordingly if needed
    client_max_body_size 512M;
    client_body_buffer_size 128k;
    include snippets/letsencrypt-nginx-certs.conf;
    include snippets/letsencrypt-nginx-route.conf;

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/etc/alternatives/php-fpm.sock;
    }
    ssl_certificate /etc/letsencrypt/live/betafox.net-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/betafox.net-0001/privkey.pem; # managed by Certbot
}
AtomX avatar
fr flag
Well, it works fine though, but there's `nginx: [warn] conflicting server name "betafox.net" on 0.0.0.0:443, ignored` warning.
AtomX avatar
fr flag
BTW, something is causing NS_ERROR_REDIRECT_LOOP
AtomX avatar
fr flag
That error is not found in root of site, but in my network admin section. Nginx is returning 302 response code there.
AtomX avatar
fr flag
Sorry... When I renamed my database, I forgot to rename wp_blog's data and that was the cause of many redirects...
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.