【问题标题】:Nginx with default_server and multiple domains on one ipaddressNginx 在一个 ipaddress 上具有 default_server 和多个域
【发布时间】:2013-04-25 12:50:55
【问题描述】:

我刚刚安装了 nginx 并为 test.example.com 创建了一个新配置。 哪个有效。

但 example.org 也指向我的服务器。现在,如果我转到 example.org,nginx 会将我重定向到 test.example.com。

我了解到您需要创建一个默认服务器条目,例如返回 444; 这就是我所做的。 这是我的网站配置:

server {
    listen       IP:80;
    server_name test.example.com;
    server_tokens off;
    root /nowhere;
    rewrite ^ https://test.example.com$request_uri permanent;
}
server {
    listen IP:443;
    server_name test.example.com;
    server_tokens off;
    root [...];

[...]
}

我之前在nginx.conf中添加的默认服务器条目(也试过之后)“include /etc/nginx/sites-enabled/*;”

server {
    # use default instead for nginx 0.7.x, default_server for 0.8.x+
    listen IP:80 default_server;      

    server_name _;
    return 444;
}

对我来说,这看起来是正确的。但我仍然会从 example.org 重定向到 test.example.com。

【问题讨论】:

  • 由于这与编程无关,因此这里是题外话。它更适合 serverfault.com

标签: nginx


【解决方案1】:

您根本不应该在默认值中设置 server_name。它确实需要在 nginx 配置中的实际服务器块之前。

返回 444 也是一个奇怪的错误。您应该只返回一个 404 来指示未找到服务器,而不是自定义 Nginx 错误代码。

编辑

至于 SSL 不起作用,您似乎没有在配置文件中实际设置 SSL 证书或打开 ssl。

server {
    listen 443 default_server ssl;
    ssl_certificate      /usr/local/nginx/conf/cert.pem;
    ssl_certificate_key  /usr/local/nginx/conf/cert.key;  

    server_name test.example.com;
    server_tokens off;
    root [...];
}

【讨论】:

  • 你需要展示你的整个配置。提供的smaples并没有说太多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2021-05-06
  • 2017-01-10
  • 2020-01-06
  • 1970-01-01
相关资源
最近更新 更多