防止 nginx 中的默认服务器或通配符

Vic*_*tor 1 nginx

我有两个域:staging.abc.comwww.example.com指向我 DNS 中的同一个服务器 IP。我没有为 启用/可用的站点staging.abc.com,但我有一个用于www.example.com. 但是,每当我访问时staging.abc.com,它都指向www.example.com.

这是我conf的。我的/etc/nginx/nginx.conf是默认的。我没有改变任何东西。

我没有sites-available/default。它已被移除。

# sites-available/example
server {
  listen 80;
  server_name example.com;
  return 301 http://www.example.com$request_uri;
}

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

  root /home/deployer/example;
  index index.php index.html index.htm;

  server_name www.example.com;

  location ~* .(jpg|jpeg|png|gif|ico|css|js|woff)$ {
  expires 30d;
  }

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

  error_page 404 /404.html;

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_read_timeout 300;
  }

  include /home/deployer/example/nginx.conf;
}


# nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##
        charset utf-8;
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;
        proxy_read_timeout 300;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;
...
Run Code Online (Sandbox Code Playgroud)

我尝试将其插入http块但无济于事:

server {
    listen      80;
    server_name "";
    return      444;
}
Run Code Online (Sandbox Code Playgroud)

这是我想要实现的目标:

  1. 最好在可用的站点上进行更改。
  2. 服务器不应为尚未分配给任何应用程序的所有域名解析任何连接(返回 444)。

Ric*_*ith 5

除非您明确定义默认服务器,否则对于没有server_name匹配的任何请求,nginx 将使用第一个具有匹配端口的服务器块。有关详细信息,请参阅此文档

您应该创建一个 catch all server 块,例如:

server {
    listen 80 default_server;
    return 444;
}
Run Code Online (Sandbox Code Playgroud)