configuration - nginx catchallconf 文件未捕获所有内容

标签 configuration nginx server-configuration catch-all

我有一个通配符 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 中定义的根目录

默认值没有成为应有的包罗万象,这是出了什么问题? everything.mydomain.tld 应该加载默认conf 文件中的根目录。

最佳答案

在默认配置文件中,您必须在两个 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文档:

There is nothing special about this name, it is just one of a myriad of invalid domain names which never intersect with any real name. Other invalid names like “--” and “!@#” may equally be used.

关于configuration - nginx catchallconf 文件未捕获所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16326839/

相关文章:

c# - NGINX 反向代理背后的 ASP.NET MVC

java - 如何使用 Spring Boot 配置属性对属性进行分组

java - Spring Cloud DataFlow自动化运行

linux - 如何在nginx配置中通过域和子域动态root

regex - fail2ban 定期在 nginx 中查找 403 请求

django - nginx 失败成功启动错误 pread() "/etc/nginx/sites-enabled/sites-available"failed (21 : Is a directory)

macos - 要从 OSX 中完全删除 MAMP 安装,需要做什么?

java - mvc :resources - Spring 的注解配置替换

ruby-on-rails - 每当 gem 时,schedule.rb 中的配置文件与 Rails 一起使用?