F
F
First Name2018-06-20 08:58:56
Nginx
First Name, 2018-06-20 08:58:56

How to set up a redirect from www to without www?

Good day to all, I read a lot of manuals on Google, but none of them work, I hope for your help) I
installed an ssl certificate for nginx via ppa: python-certbot-nginx, after installation and automatic configuration, the config became like this.
Now there is a redirect from http to https, but how to make it redirect from www to https without www, no idea, no matter how I abuse the config, it doesn’t want to work :C

/etc/nginx/sites-available/site.ru

The server is on DigitalOcean
server {
         listen 443 ssl;
 
        root /var/www/SonyTwo;
        ssl_certificate /etc/letsencrypt/live/site.ru/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/site.ru/privkey.pem; # managed by Certbot
        ssl_dhparam /etc/nginx/dhparam.pem;

        index index.html index.htm index.nginx-debian.html;

        server_name site.ru; 

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

server {
   if ($host = www.site.ru) {
       return 301 https://$host$request_uri;
    }
    if ($host = site.ru) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        listen 80;
        listen [::]:80;

    server_name site.ru;
    return 405; # managed by Certbot
}

Thank you in advance for your answer
PS Only html files on the server, php is not installed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
webfln, 2018-06-20
@maxxtweek

It is better not to use if constructs. For your case:
server {
server_name www.site.ru;
listen 80 ;
return 301 https://$host$request_uri;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question