여러 도메인이 있는 서버에 https를 강제 적용

여러 도메인이 있는 서버에 https를 강제 적용

우분투에서 nginx를 사용하고 있는데 문제가 있습니다. SSL 인증서(Let's Encrypt)에 여러 도메인이 있습니다. .com.br 도메인으로 웹 사이트에 액세스하면 사용자는 https를 사용해야 하지만 다른 도메인에서도 마찬가지입니다.

이 줄을 활성화하면 모든 도메인이 .com.br 도메인으로 리디렉션됩니다.

return 301 https://www.$server_name$request_uri;

이 문제를 어떻게 해결할 수 있나요?

내 nginx 구성 파일은 다음과 같습니다.

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        client_max_body_size 100M;

        root /var/www/robbu.com.br/public;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name domain.com.br www.domain.com.br domain.com.ar www.domain.com.ar domain.global www.domain.global domain.net www.domain.net domain.solutions www.$

        #return 301 https://www.$server_name$request_uri;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/robbu.com.br/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/robbu.com.br/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
}

답변1

자체 https 도메인으로 전달하려는 모든 도메인에 대해 서버 블록을 생성해야 합니다. 각 도메인에 대해 이 두 서버 세트를 반복합니다.

# This server simply redirects the requested to the https version of the page
server {
  listen 80;
  server_name www.example.com example.com;

  # Let's Encrypt certificates with Acmetool. Not sure if required on http or https (you can't connect to https server before there's a certificate) so do both.
  location /.well-known/acme-challenge/ {
    alias /var/www/.well-known/acme-challenge/;
  }

  location / {
    return 301 https://www.example.com$request_uri;
  }
}

server {
  listen 443 ssl http2;
  server_name example.com;

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;
  ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;

  # Set up preferred protocols and ciphers. TLS1.2 is required for HTTP/2
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
  ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0

  access_log  /var/log/nginx/access.log main buffer=32k flush=1m if=$log_ua;

  return 301 https://www.example.com$request_uri;
}

관련 정보