Score:0

How to rewrite www to non www in nginx

et flag

I found a couple of other serverfault questions where rewrite www to non-www in nginx was answered, however it seems like with the nginx config my webdock.io server has the solutions I have tried seem to always cause problems or simply don't work. I suspect it has to do with the order or a conflict with what is already there.

Here is the current config

server {

root /var/www/html;
client_max_body_size 256M;

# Add index.php to the list if you are using PHP
index index.html index.htm index.php;

#Which domain names will this vhost respond to
server_name my-clients-domain.com www.my-clients-domain.com;

location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt  { access_log off; log_not_found off; }
access_log /var/www/logs/access.log;
error_log  /var/www/logs/error.log error;
error_page 404 /index.php;

location ~ \.php$ {
  add_header X-Powered-By "Webdock.io";
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
  fastcgi_index index.php;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_intercept_errors off;
  fastcgi_buffer_size 16k;
  fastcgi_buffers 4 16k;
  fastcgi_connect_timeout 600;
  fastcgi_send_timeout 600;
  fastcgi_read_timeout 600;
}

# Necessary for Let's Encrypt Domain Name ownership validation. Place any other deny rules after this
location ~ /.well-known {
allow all;
}

# Deny access to .htaccess or .htpasswd files
location ~ /\.ht {
deny all;
}

    # Deny access to any git repository
    location ~ /\.git {
        deny all;
    }

    # Deny access to xmlrpc.php - a common brute force target against Wordpress
    location = /xmlrpc.php {
        deny all;
        access_log off;
        log_not_found off;
        return 444;
    }

    # Webdock: Do not delete the following End Of File marker if editing this file by hand
    #EOWDSLBLK

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/my-clients-domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my-clients-domain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server { if ($host = www.my-clients-domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = my-clients-domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen 80 default_server;
        listen [::]:80 default_server;
        server_name my-clients-domain.com  www.my-clients-domain.com;
        return 404; # managed by Certbot
}

I am wanting all https://www.my-clients-domain.com to rewrite to https://my-clients-domain.com

I have tried adding the following server block at different points in the config file but each time it either does not work as expected.

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

    server_name www.my-clients-domain.com;
    return 301 https://my-clients-domain.com$request_uri;

    ssl_certificate /etc/letsencrypt/live/my-clients-domain.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/my-clients-domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
}

If I type enter https://www.my-clients-domain.com the "www" remains in the url. Am I going about this the wrong way?

Score:0
in flag

Please be aware that a 301 is intended to be a permanent and should be cached by your browser. When testing a change in your configuration: after reloading the nginx configuration test from a new private/incognito browser window.


I am wanting all https://www.my-clients-domain.com to rewrite to https://my-clients-domain.com

Then I would for starters ensure that when you redirect from plain HTTP to HTTPS, that your visitors immediately get redirected to https://my-clients-domain.com and don't get redirected first from http://www.my-clients-domain.com to https://www.my-clients-domain.com where the immediately get a second redirect to https://my-clients-domain.com

Rather than a redirect with a $host parameter use the desired domain and have a concise:

server { 
        listen 80; 
        server_name my-clients-domain.com  www.my-clients-domain.com;
        return 301 https://my-clients-domain.com$request_uri;
}

Your SSL server block looks OK already.

I would expect a second block for the bare domain, that holds your web content: (Assuming that the certificate /etc/letsencrypt/live/my-clients-domain.com/ is also valid for www.my-clients-domain.com )

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

    server_name www.my-clients-domain.com;
    return 301 https://my-clients-domain.com$request_uri;

    ssl_certificate /etc/letsencrypt/live/my-clients-domain.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/my-clients-domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
}
server {
    listen [::]:443 ssl ipv6only=on; 
    listen 443 ssl; 

    server_name my-clients-domain.com;
    root  /var/www/default/htdocs;

    ssl_certificate /etc/letsencrypt/live/my-clients-domain.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/my-clients-domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
}

Or slightly more concise:

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

    server_name my-clients-domain.com www.my-clients-domain.com;
    root  /var/www/default/htdocs;

    if ($host = www.my-clients-domain.com) {
       return 301 https://my-clients-domain.com$request_uri;
    }
    ssl_certificate /etc/letsencrypt/live/my-clients-domain.com/fullchain.pem; 
    ssl_certificate_key /etc/letsencrypt/live/my-clients-domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
}
skribe avatar
et flag
Great Thanks a million!
I sit in a Tesla and translated this thread with Ai:

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.