W
W
winDeD2017-02-02 12:38:42
Nginx
winDeD, 2017-02-02 12:38:42

Two SSL sites on NGINX. Why does www redirect to a neighboring site?

Good afternoon, there are 2 sites with SSL certificates.
Problem:
If the request is:

curl -I https://www.1111.com ответ правильный: https://1111.com

But if the same request to the second site:
curl -I https://www.2222.com ответ: 
(curl: (51) SSL: no alternative certificate subject name matches target host name 'www.1111.com')

curl -I http://www.2222.com ответ: https://1111.com

If you change the order of loading configs in nginx.conf - then the answers are reversed and this error occurs on the 1111.com site.
1111.conf
server {
    listen  80;
    server_name 1111.com www.1111.com;
    return 301 https://1111.com;$request_uri;
}

server {
    listen 443 ssl http2;
    server_name 1111.com;
    root /usr/share/nginx/1111.com;
    index index.php;
...

In 2222.conf the settings are the same.
How to configure the server so that requests from: https://www.* | http://www.* - redirected to https://*.com ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
winDeD, 2017-02-02
@winDeD

Here is the correct setting:

server {
      listen   80;
      server_name site.ru www.site.ru;
      rewrite  ^(.*) https://$server_name$1 permanent;
    }
    server {
      listen   443   ssl http2;
      include ssl/ssl_site.ru;
      server_name  www.site.ru;
      rewrite ^(.*) https://site.ru$1 permanent;
    }
    server {
      listen   443   ssl http2;
      server_name site.ru;
      include ssl/ssl_site.ru;

    ... other config string ...

Y
Yuri Chudnovsky, 2017-02-02
@Frankenstine

nginx.org/ru/docs/http/configuring_https_servers.html

D
Denis Semenov, 2017-02-18
@denisemenov

Or like this:

server {
    listen      %ip%:%web_port%;
    server_name %domain_idn% %alias_idn%;
    return 301  https://%domain_idn%$request_uri;
}

server {
    listen      %ip%:%web_ssl_port% http2 ssl;
    server_name %alias_idn%;
    return 301  https://%domain_idn%$request_uri;
    
    ssl         on;
    ssl_certificate      %ssl_pem%;
    ssl_certificate_key  %ssl_key%;
}

server {
    listen      %ip%:%web_ssl_port% http2 ssl;
    server_name %domain_idn%;
    root        %sdocroot%;
    index       index.php index.html index.htm;

    ssl         on;
    ssl_certificate      %ssl_pem%;
    ssl_certificate_key  %ssl_key%;

    location / {

    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question