Nginx Listen Sockets and Default Servers
Two websites, one Nginx process
A virtual server is a server configuration block, not another machine. A single Nginx process group can answer requests for several websites on the same local address and port. It first considers where the connection arrived, then identifies the intended website from the request. The following two server blocks are a partial HTTP configuration. They return short labels so you can see which site was selected.
server { listen 127.0.0.1:18087; server_name shop.test; return 200 "shop\n";}server { listen 127.0.0.1:18087; server_name docs.test; return 200 "docs\n";}With both blocks on 127.0.0.1:18087, an ordinary HTTP request for shop.test selects the shop block; docs.test selects the docs block. If neither name matches, the first declared server for that address and port is the fallback unless a different default is specified. The full lab below adds an explicit fallback.
Address and port come before the name
listen 127.0.0.1:18087;# Alternative: IPv6 loopback, if supportedlisten [::1]:18087;A socket is an operating-system communication endpoint. The address and port identify the local listener; 127.0.0.1 is IPv4 loopback and ::1 is IPv6 loopback. A port-only listener commonly binds all IPv4 interfaces, not just loopback. IPv6 and wildcard binding options require platform-specific review. Use explicit loopback in these labs. A request to a different listening port is not routed to the original port merely because its Host field matches.
Make fallback behavior intentional
server { listen 127.0.0.1:18087 default_server; server_name ""; return 404 "unknown host\n";}The default belongs to the address/port group. This block returns a standard 404 for an otherwise unmatched valid host, rather than accidentally serving a real website. The empty name is not a wildcard; it can match requests without a host name when those requests are valid. A malformed HTTP/1.1 request without a required Host field can be rejected before your fallback response. Only one explicit default is allowed per address/port.
curl --noproxy '*' --max-time 2 -i -H 'Host: shop.test' http://127.0.0.1:18087/curl --noproxy '*' --max-time 2 -i -H 'Host: absent.test' http://127.0.0.1:18087/Expected in the full Chapter 7 lab: 200 with shop on the first request and 404 with unknown host on the second. The Host override changes the name Nginx sees without modifying DNS; the client still connects to loopback. These are predicted results, not captured output.
server_name _; is just a literal name. Use an explicit default listener and a deliberate response. Do not bind a public address for this lab.
Practice
Predict what happens if a third named site is added before the shop block. With the explicit fallback unchanged, an unmatched host still goes to that fallback. Complete the socket selection exercise, then continue to name matching.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Listener selection
Apply the lesson to original routing scenarios.
Two server blocks listen on the same local address and port. What distinguishes them for a normal HTTP/1.1 request?
An explicit default server is second in the configuration. Where does a valid unmatched host go?
Which listener confines IPv4 access to the local machine?
A request arrives on port 18088 but its Host matches a site configured only on 18087. What follows?
Why set a deliberate 404 fallback?