nginx 多台服务器使用 catchall 总是触发 catchall

nginx 多台服务器使用 catchall 总是触发 catchall

我想在 1 台服务器上托管 2 个网站(为了省钱,我的服务器在任何时候都只使用 2% 的 CPU)。一个月前我遇到了一个问题,谷歌在搜索结果中显示我的 IP 而不是域名,所以我添加了一个 catchall 重定向。当我尝试托管我的 example2.com 时,catchall 总是触发并重定向到 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;
    }
}

相关内容