nginx 多個伺服器與 catchall 總是觸發 catchall

nginx 多個伺服器與 catchall 總是觸發 catchall

我想在 1 台伺服器上託管 2 個網站(為了省錢,我的伺服器在任何時候都只使用大約 2% 的 CPU)。一個月前我遇到了一個問題,谷歌在搜尋結果中顯示我的 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;
    }
}

相關內容