When I create an HTML page:
sudo wo site create site.tld --html -le --dns=dns_cf --hsts

It is available both at site.tld and at www.site.tld. This doesn't happen when we have WP installed.
I need to redirect www to non-www. I tried modifying this file:
sudo nano /etc/nginx/sites-available/site.tld

...but then I had errors when I ran sudo nginx -t

What's the easiest way to set it up?

What was the error?

In sudo nano /etc/nginx/sites-available/site.tld I changed:

server {
    listen 80;
    listen [::]:80;
    server_name site.tld www.site.tld;

    access_log /var/log/nginx/site.tld.access.log;
    error_log /var/log/nginx/site.tld.error.log;

    root /var/www/site.tld/htdocs;

    index index.html index.htm;

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

    include common/locations-wo.conf;
    include /var/www/site.tld/conf/nginx/*.conf;
}

to the following code:

server {
    listen 80;
    listen [::]:80;
    server_name www.site.tld.pl;

    return 301 http://site.tld$request_uri;
}

server {
    listen 80;
    listen [::]:80;
    server_name site.tld www.site.tld;

    access_log /var/log/nginx/site.tld.access.log;
    error_log /var/log/nginx/site.tld.error.log;

    root /var/www/site.tld/htdocs;

    index index.html index.htm;

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

    include common/locations-wo.conf;
    include /var/www/site.tld/conf/nginx/*.conf;
}

and now sudo nginx -t returns warnings:

nginx: [warn] conflicting server name "www.site.tld" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "site.tld" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "www.site.tld" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "www.site.tld" on [::]:80, ignored
nginx: [warn] conflicting server name "site.tld" on [::]:80, ignored
nginx: [warn] conflicting server name "www.site.tld" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Redirection doesn't work.

Maybe I need to change another file in WordOps?

    wo site edit <sitename> will edit the relevant conf file:
    https://docs.wordops.net/commands/site/#site-edit
    But you're getting the 'conflicting server name' errors because you're declaring the the same port/site twice. Remove the 'www.site.tld' from the second server block.

    Torwald45 If you want to redirect www.site.tld to site.tld, your exemple is almost good but you are using www.site.tld twice.

    Here the proper code :

    server {
        listen 80;
        listen [::]:80;
        server_name www.site.tld;
    
        return 301 http://site.tld$request_uri;
    }
    
    server {
        listen 80;
        listen [::]:80;
        server_name site.tld;
    
        access_log /var/log/nginx/site.tld.access.log;
        error_log /var/log/nginx/site.tld.error.log;
    
        root /var/www/site.tld/htdocs;
    
        index index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        include common/locations-wo.conf;
        include /var/www/site.tld/conf/nginx/*.conf;
    }

    After making these changes and switching the browser to the address from www.site.tld, nginx asks for a login and password.

    sudo nginx -t returns:

    nginx: [warn] conflicting server name "www.site.tld" on 0.0.0.0:80, ignored
    nginx: [warn] conflicting server name "site.tld" on 0.0.0.0:80, ignored
    nginx: [warn] conflicting server name "www.site.tld" on [::]:80, ignored
    nginx: [warn] conflicting server name "site.tld" on [::]:80, ignored
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

      Yes, I have an API connection to Cloudflare. Please take a look at my first post.

      18 days later

      Finally, I moved the static pages to Cloudflare Pages and thus the redirection problem was solved using Cloudflare Page Rules.

      Hosted by VirtuBox