Nginx serving off the wrong root

Solution 1:

Briefly: you haven't told Nginx which server {...} to choose from - so it chose the first one.

Nginx passes requests to the default server. To do this, it matches the Host header against the server_name. If an IP address is used, the Host header will contain the IP address (which means it can be used to match against the server_name).

In the case where no server_name matches the Host header, nginx will choose the server in the following way:

  1. The server explicitly set as default.
    • This is done (in the case of port 80) with listen 80 default_server
  2. If there is no default_server, the first server listed in the configuration is used.
    • It is important to note that, when using wildcards, the order of file inclusion in nginx is indeterminate

To quote from the Nginx wiki:

If the directive has the default_server parameter, then the enclosing server {...} block will be the default server for the address:port pair. This is useful for name-based virtual hosting where you wish to specify the default server block for hostnames that do not match any server_name directives. If there are no directives with the default_server parameter, then the default server will be the first server block in which the address:port pair appears. The default_server parameter appeared in version 0.8.21 thus deprecating the parameter default.

Recommended reading:

  • Nginx Documentation: Server Names
  • Nginx Documentation: How Nginx Processes a Request

Solution 2:

root can also be defined under http scope before server. You can set your default root there.

html {
    root /default/root

    server {
        root /site/root
    }
}