キャッチオール付きの nginx 複数サーバーが常にキャッチオールをトリガーする

キャッチオール付きの nginx 複数サーバーが常にキャッチオールをトリガーする

1 台のサーバーで 2 つのサイトをホストしたいと考えています (コストを節約するため、私のサーバーは常時 2% 程度の CPU しか使用していません)。1 か月前に、Google の検索結果にドメインではなく IP が表示されるという問題が発生したため、キャッチオール リダイレクトを追加しました。example2.com をホストしようとすると、キャッチオールが常にトリガーされ、example1.com にリダイレクトされます。

example1.com の方が重要です。example2.com はサイド プロジェクトのようなものなので、すべてを example1.com にリダイレクトしたいと考えています。問題は、example2.com (http および https) も example1.com にリダイレクトされることです。

# Catchall configuration - redir to the domain for bare and invalid domain requests
server {
    listen 80 default_server;

    server_name _;

    return 301 https://example1.com$request_uri;
}

# HTTPS for example1.com
server {
  listen       443 ssl;
  server_name  example1.com;

  ssl_certificate  /etc/letsencrypt/live/example1.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example1.com/privkey.pem;

  location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

# example2
server {
    listen 80 ;
    server_name example2.com www.example2.com;

    return 301 https://example2.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example2.com www.example2.com;

    ssl_certificate /etc/letsencrypt/live/example2.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example2.com/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

    location / {
      proxy_pass http://localhost:4000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
    }
}

関連情報