【问题标题】:nginx catchall conf file not catching allnginx catchall conf文件没有捕获所有
【发布时间】:2013-04-25 23:08:07
【问题描述】:

我有一个通配符 DNS 条目,因此 *.mydomain.tld 被定向到我的服务器。 我正在使用 nginx 我有 2 个 conf 文件,标题为:

  • 默认
  • myconf.conf

我的 conf 文件如下所示:

默认值:

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/website;
    index index.html index.htm;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html;
    }
}

myconf.conf:

server {
    listen 80; 
    #listen [::]:80 default_server ipv6only=on;

    root /home/me/www/website;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    # orig # server_name localhost;
    server_name me.mydomain.tld;

    access_log /home/me/logs/me.mydomain.tld.access.log;
    error_log /home/me/logs/me.mydomain.tld.error.log warn;

    location / { 
        try_files $uri $uri/ $uri.php?$args;
    }   
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

当我如下浏览域时,这些是加载的 conf 文件。

  • me.mydomain.tld 加载 myconf.conf 中定义的根目录
  • mydomain.tld 加载默认定义的根目录
  • anything.mydomain.tld 加载 myconf.conf 中定义的根目录

默认值不应该是包罗万象的,出了什么问题? any.mydomain.tld 应该在默认的 conf 文件中加载根目录。

【问题讨论】:

    标签: configuration nginx server-configuration catch-all


    【解决方案1】:

    在您的默认配置文件中,您必须在两个listen 行上指定default_server;另外,您需要删除 server_name 行:

    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        root /var/www/website;
        index index.html index.htm;
    
        #server_name _;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html;
        }
    }
    

    您用于server_name 的下划线实际上不是通配符(如果这是您的意图)。来自 nginx Server Names 文档:

    这个名字没有什么特别之处,它只是无数与任何真实姓名不相交的无效域名之一。其他无效名称,如“--”和“!@#”也可以同样使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-23
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多