V
V
Vitaly2020-11-03 23:44:00
Nginx
Vitaly, 2020-11-03 23:44:00

Where did I go wrong in nginx config (redirect not working)?

It is necessary that www.domain.ru redirects to domain.ru for both http and https.
Now domain.ru redirects to https://domain.ru , but redirecting from www doesn't work.
www.domain.ru opens a web server stub, https://www.domain.ru swears that the ssl certificate does not match the domain (without www), which is logical.

Here is my config. Can't find an error.

server {
  server_name domain.ru www.domain.ru;
  access_log /var/log/nginx/domain.ru-access.log;
  error_log /var/log/nginx/domain.ru-error.log;

  index index.html index.htm;

  if ($host = www.domain.ru) {
        return 301 https://domain.ru$request_uri;
  }

  location / {
    proxy_pass http://localhost:8090;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.ru/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.ru/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 = domain.ru) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

  listen 80;
  listen [::]:80;
  server_name domain.ru;
    return 404; # managed by Certbot
}


Thank you for your help :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2020-11-03
@vitalysokolov

server {
  server_name domain.ru www.domain.ru;
  listen 80;

  location / {
        return 301 https://$server_name$request_uri;
  }
}

The easiest way is unconditional forwarding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question