Two website with SSL on NGINX. Sequence and redirect

I have two website with SSL on NGINX.

When

curl -I https://www.1111.com answer: https://1111.com

But when

curl -I https://www.2222.com answer: https://1111.com
curl -I https://2222.com answer https://2222.com

In nginx.conf i try change sequence of /import *.conf, and when i set 2222.com before 1111.com > answer changing the contrary.

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;
...

2222.conf is similar

How can i redirect all https:// www.* and http:// www.* on this website, and not the other ?

Answer

Yeah! Right config:

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 ...

Attribution
Source : Link , Question Author : Yarik Bright , Answer Author : Yarik Bright

Leave a Comment