0

I have several virtual hosts configured with nginx. They are very simiral to each other and for most of them, everything seems to be working fine.

However,

  1. If I hit the server through a url that does not have any virtual host configured, nginx always redirects to one of the existing virtual hosts (seems to be the first one in the list.
  2. Same happens if I simply go there using IP address.

Most of the virtual hosts look like this:

server {
        server_name  iibs.co;
        return 302 $scheme://www.iibs.co$request_uri;
}

server {
    server_name www.iibs.co;
    root /var/www/iibs.co;

    index index.php;

    include global/restrictions.conf;
    client_max_body_size 64M;

    # Additional rules go here.

    location / {
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when using query string
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        include fastcgi.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass php;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
    # Only include one of the files below.
#    include global/wordpress.conf;
#    include global/wordpress-ms-subdir.conf;
#    include global/wordpress-ms-subdomain.conf;
}

I just tried adding another one, very similar just with different domain and root. And that particular one, also getting redirected to the same first vhost on the list.

At this point, I have no idea where and what should I looks for.

Shurik Agulyansky
  • 2,607
  • 2
  • 34
  • 76
  • 1
    When you don't define a default_server nginx is going to choose one. So simply define a vhost with the default_server tag and return 444 or smth like that. – lbueker Jan 07 '18 at 10:20
  • 1
    Possible duplicate of [Why is nginx responding to any domain name?](https://stackoverflow.com/questions/9824328/why-is-nginx-responding-to-any-domain-name) – Richard Smith Jan 07 '18 at 14:12

1 Answers1

0

This is an example of default NGINX config for prevent redirects to the first of the existing virtual hosts .

### Block all illegal host headers. Taken from a discussion on nginx
### forums. Cf. http://forum.nginx.org/read.php?2,3482,3518 following
### a suggestion by Maxim Dounin. Also suggested in
### http://nginx.org/en/docs/http/request_processing.html#how_to_prevent_undefined_server_names.
server {
    listen 80 default_server; # IPv4
    #listen [::]:80 default_server ipv6only=on; # IPv6

    server_name _;
    server_name "";
    return 444;
}
Iryston
  • 2,111
  • 1
  • 14
  • 12