From the Amazon docs:
To ensure that your load balancer can scale properly, verify that each subnet for your load balancer has a CIDR block with at least a /27 bitmask (for example, 10.0.0.0/27) and has at least 8 free IP addresses. Your load balancer uses these IP addresses to establish connections with the instances.
This makes it sound like the ELB health check is made via AWS private IP rather than domain name. From what I know of Amazon, that's how they prefer to do things when they can (for obvious performance reasons, no need for DNS lookup, can be handled by closer routers, etc.)
If that's the case, then the host header (unless it's blank, I'm not 100% sure how host headers work when you're making a request to a bare IP) will be the private AWS address of the EC2 instance (I'm assuming it's EC2) running the webserver. This impacts the way Nginx chooses what virtual server should handle the request.
Since Nginx has two
server blocks, it has to decide which one gets any given request. The process by which it chooses server blocks (details here) is as follows:
First it looks for servers listening on the port on which the request came in. In this case, both servers are listening on the same port, so this doesn't help any.
Then it looks at the host header of the request, and looks for servers with a
server_name that matches the host header. In this case, none of them will match, because the host header is the private AWS IP assigned to the instance (or blank, as I said I'm not sure.)
Finally, if neither of those yields a match, it just goes with whichever server is the default. In the absence of an explicit default declaration, the server which is defined first in the site config is chosen as the default.
If your redirect server comes before the main server config (as I would imagine it does) then this is probably your problem. You need to do one of three things:
Specify that you want the other server (the main server) to be the default by adding
default_server to the
listen directive, i.e.
listen 80 default_server;
location /test block to the other server block, or
Reverse the order in which the server blocks are defined.
The first option seems like the way to go, since it makes sense for the
default_server directive to be explicit rather than implicit, and it makes sense for the main server to be the default rather than the you-had-one-job redirect server.
If I'm correct in my hypothesizing, that should solve it.