Mehrere Nginx-Server mit Catchall lösen immer Catchall aus

Mehrere Nginx-Server mit Catchall lösen immer Catchall aus

Ich möchte 2 Websites auf einem Server hosten (um Geld zu sparen, mein Server nutzt ohnehin nur etwa 2 % der CPU-Leistung). Vor einem Monat hatte ich ein Problem, bei dem Google in den Suchergebnissen meine IP-Adresse statt meiner Domain anzeigte, also habe ich eine Catchall-Weiterleitung hinzugefügt. Wenn ich versuche, meine example2.com zu hosten, wird die Catchall-Weiterleitung immer ausgelöst und zu example1.com weitergeleitet.

example1.com ist wichtiger; example2.com ist eher ein Nebenprojekt, daher möchte ich, dass der Catchall zu example1.com führt. Das Problem ist, dass example2.com (http und https) auch zu example1.com umgeleitet wird.

# 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;
    }
}

verwandte Informationen