当在配置中找不到时,Nginx 重定向到第一个按字母顺序排列的站点

发布于 2025-01-10 03:58:09 字数 673 浏览 0 评论 0原文

我有几个网站,当我输入不存在的子域时,Nginx 会重定向到 /etc/nginx/sites-enabled 中的第一个可用网站。我的网站配置如下:


server { 
  listen 80; 
  listen [::]:80; 
  server_name *.example.com; 
  return 301 https://$host$request_uri; 
} 

server { 
   listen 443 ssl; 
   server_name *.example.com; 
   
   ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
   ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; 
   include /etc/letsencrypt/options-ssl-nginx.conf; 
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
   
   root /var/www/app_name/build;
   
   index index.html index.htm;
   
   location / {
      try_files $uri /index.html =404;
   }
}

I have a few websites, and when I type in a subdomain that does not exist, Nginx redirects to the first available website in /etc/nginx/sites-enabled. My config is as follows for the websites:


server { 
  listen 80; 
  listen [::]:80; 
  server_name *.example.com; 
  return 301 https://$host$request_uri; 
} 

server { 
   listen 443 ssl; 
   server_name *.example.com; 
   
   ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
   ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; 
   include /etc/letsencrypt/options-ssl-nginx.conf; 
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
   
   root /var/www/app_name/build;
   
   index index.html index.htm;
   
   location / {
      try_files $uri /index.html =404;
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪是无色的血 2025-01-17 03:58:09

完美,Ivan Shatsky 关于 default_server 的评论修复了它。但是我遇到了错误duplicate default_server error,我用以下方法修复了该错误:

cd /etc/nginx/sites-enabled
unlink default

Perfect, Ivan Shatsky's comment about default_server fixed it. However I had an error duplicate default_server error, which I fixed with the following:

cd /etc/nginx/sites-enabled
unlink default
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文