Nginx 总是火

Nginx 总是火

我有两台服务器

server {

    listen 80;
    listen [::]:80;

    root  /var/www/a;

    server_name a.example.com;

    index index.html index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .*\.(js|swf|css|jpg|gif|png|ico|eot|woff|svg|ttf|otf)$ {
        expires 30d;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php7:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

server {

    listen 80;
    listen [::]:80;

    root  /var/www/b;

    server_name b.example.com;

    index index.html index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .*\.(js|swf|css|jpg|gif|png|ico|eot|woff|svg|ttf|otf)$ {
        expires 30d;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php7:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

其中/var/www/aindex.php

i am a

其中/var/www/bindex.php

i am b

我去看看a.example.com,效果i am a还不错。

我去看看b.example.com,效果i am b还不错。

但我去拜访了asdf.example.com,结果确实如此i am a

更重要的是,我访问了werwe.asdf.example.com,它的结果i am a

似乎无论我用*.example.com它访问什么,总会返回i am a

我想知道为什么。

谢谢。


这是我的nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

答案1

这是很正常的,你可以看看 Nginx 文档的描述:http://nginx.org/en/docs/http/server_names.html

When searching for a virtual server by name, if name matches more than one of the specified variants, e.g. both wildcard name and regular expression match, the first matching variant will be chosen, in the following order of precedence:

1.exact name
2.longest wildcard name starting with an asterisk, e.g. “*.example.org”
3.longest wildcard name ending with an asterisk, e.g. “mail.*”
4.first matching regular expression (in order of appearance in a configuration file)

因此,在您的情况下,您的域名与任何服务器名称都不匹配,nginx 将使用第一个见过的服务器块。

为了解决这个问题,您应该创建一个带有的服务器块名称defaultvhostserver_name _;并将所有未知域都在此服务器块(虚拟主机)中提供服务。

希望它有效。

答案2

我在 nginx 文档中找到了答案http://nginx.org/en/docs/http/request_processing.html

nginx 首先决定哪个服务器应该处理请求。让我们从一个简单的配置开始,其中所有三个虚拟服务器都监听端口 *:80:

server {
    listen      80;
    server_name example.org www.example.org;
    ...
}

server {
    listen      80;
    server_name example.net www.example.net;
    ...
}

server {
    listen      80;
    server_name example.com www.example.com;
    ...
}

在此配置中,nginx 仅测试请求的标头字段“Host”来确定应将请求路由到哪个服务器。 如果其值与任何服务器名称都不匹配,或者请求根本不包含此标头字段,则 nginx 会将请求路由到此端口的默认服务器。 在上述配置中,默认服务器是第一个 — 这是 nginx 的标准默认行为。 还可以使用 listen 指令中的 default_server 参数明确设置哪个服务器应为默认服务器:

server {
    listen      80 default_server;
    server_name example.net www.example.net;
    ...
}

所以如果没有server_name匹配并且没有default_server指定,则第一个服务器将被用作default_server

答案3

将此站点添加为已启用站点

# This just prevents Nginx picking a random default server if it doesn't know which
# server block to send a request to
server {
  listen      80 default_server;
  server_name _;
  access_log off; log_not_found off;
  return 444;
}

相关内容