T
T
tgarl2022-04-14 12:09:35
Nginx
tgarl, 2022-04-14 12:09:35

What could be the reason that the redirect in nginx does not work?

I open the file /etc/nginx/sites-enabled/for editing, I
prescribe the domain before the current server, I tried after the current one

server {
        listen 80;
        listen [::]:80;
    server_name www.домен;
    return 301 $scheme://домен$request_uri;
}
server {
        listen 80;
  listen [::]:80;
    server_name www.домен домен;


does not work

tried options with conditions to prescribe
if ($host ~ "^www\.(.*)$") {
                return 301 https://домен$request_uri;
        }
if ($host = 'www.домен' ) {
              rewrite ^(.*)$ https://домен$1 permanent;
        }


no way, nginx restarted after making changes

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tgarl, 2022-04-14
@tgarl

All figured out.
With all these redirects and certificates. Total.
1. we check the certificate itself, whether the domain and www.domain are included in it, if something is missing, add it, since checking the certificate is more important than the nginx settings, redirects work, but you don’t see it until you allow you to go to the next page without a certificate ( recent browser updates may not allow this, IE gives me a choice, but chrome doesn’t)
if cerbot is used, then the command where we list all the necessary domains
certbot -d domen -d www.domen --expand
2. leave in the main config

server {
       ...
        server_name домен;
       ...
}

and also add additional redirects for www
server {
  listen 80; #для http редирект сразу на https и без www
  server_name www.домен;
  return 301 https://домен$request_uri;
}
server {
  listen 443; #для https
  server_name www.домен;
  return 301 $scheme://домен$request_uri;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question