当 server_name 不匹配时 Nginx 丢弃 [英] Nginx drop when server_name does not match

查看:35
本文介绍了当 server_name 不匹配时 Nginx 丢弃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个虚拟主机:一个在 domain.tld 端口 80 上,另一个在 sub.domain.tld 端口 443 上并启用 SSL.我在我的 DNS 服务器上添加了一个 CNAME 条目,将我的 sub 子域重定向到 domain.tld..一切都按预期工作,但转到 http://sub.domain.tld 与转到 http://domain.tldhttps 的效果相同://domain.tldhttps://sub.domain.tld 相同.我怎样才能防止这种情况?

I have two vhosts : one on domain.tld port 80, the other on sub.domain.tld port 443 with SSL on. I added a CNAME entry on my DNS server that redirects my sub subdomain to domain.tld.. Everything works as expected, but going to http://sub.domain.tld does the same as going to http://domain.tld, and https://domain.tld the same as https://sub.domain.tld. How can I prevent this ?

我的配置:

server {
    listen *:443;
    listen [::]:443;

    server_name www.sub.domain.tld;

    ssl on;
    ssl_certifiate ...;
    ssl_certifiate_key ...;

    root /var/www/sub.domain.tld;
    ...
}

server {
    listen *:80;
    listen [::]:80;

    server_name www.domain.tld;

    root /var/www/domain.tld;
    ...
}

推荐答案

如果这些是你唯一的 server 块,那么它们也是你事实上的 默认服务器 端口块分别是443和80端口.有关详细信息,请参阅本文档.

If these are your only server blocks, then they are also your defacto default server blocks for port 443 and port 80 respectively. See this document for details.

如果你不想这样,你需要声明一个默认的 server 块.极简主义的定义可能是:

If you do not want this, you need to declare a default server block. A minimalist definition might be:

server {
    listen 80 default_server;
    listen 443 default_server;
    deny all;
}

这篇关于当 server_name 不匹配时 Nginx 丢弃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆