2

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 ?

2 Answers2

1

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

It is little tricky man but ofcourse you can do that.

map $http_host $new {
  'www.abc.com' '1';
  'www.xyz.com' '2';
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    if ($http_x_forwarded_proto != 'https') {
          rewrite ^(.*) https://$host$1 redirect;
    }
}

server {
  if ($new != '1') {
        server_name www.xyz.com;

        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;

        ssl_certificate  /etc/ssl/certs/s2.pem;
        ssl_certificate_key /etc/ssl/certs/s2.key;

        root /var/www/html/site2;

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

        location / {

    }
  }

  if ($new != '2') {
        server_name www.abc.com;

        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;

        ssl_certificate  /etc/ssl/certs/s1.pem;
        ssl_certificate_key /etc/ssl/certs/s1.key;

        root /var/www/html/site1;

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

        location / {

    }
  }
}

This can be the exact answer for more information and R&C read nginx map modules

Ashish Gupta
  • 305
  • 2
  • 6
  • Put listen line out of the condition and check equal to continue to i wrote roughly. – Ashish Gupta Feb 02 '17 at 12:59
  • This is a pretty bad approach to this problem. I doubt that it might not even work. Just use standard approach of using different `server` blocks for different virtual hosts. – Tero Kilkanen Feb 02 '17 at 21:32