ipdetecto.com logo
ipdetecto.com
My IPSpeed
Knowledge Hub
HomeKnowledge HubNginx Server Blocks Configuration
© 2026 ipdetecto.com
support@ipdetecto.comAboutContactPrivacyTermsllms.txt
Network Administration
5 MIN READ
Apr 19, 2026

How to Configure Nginx Server Blocks

server_name matching, default_server, listen [::]:443 quals, and map{} for complex host routing.

Each server{} block defines a virtual host: addresses via listen, names via server_name, and TLS settings when applicable. The default_server flag selects which block answers unmatched hosts or bare IPs. For many names, prefer explicit lists over heavy regex—ordering still matters for overlapping patterns. IPv6 listeners often pair with dual-stack [::]:443 plus IPv4 443.

TaskDirectiveNote
Catch-all vhostdefault_serverUse for monitoring or blocking unknown Host headers
HTTP to HTTPSreturn 301 https://$host$request_uriAvoid redirect loops if TLS is not ready
Many hostnamesserver_name listWildcards have specific rules—test SNI matches

Related

reverse proxy, nginx SSL, nginx configuration, Apache virtual hosts (compare)

Frequently Asked Questions

Q.What is default_server used for?

It marks which server block handles requests that do not match other server_name values on the same listen socket.

Q.How does server_name matching work?

Exact names beat wildcards; nginx picks the best match per request Host and SNI—ordering still matters for ambiguous regex.

Q.Why use separate certificates per hostname?

SNI lets one IP serve many TLS sites; each name may need its own cert or a SAN covering the set.

Q.How do I redirect HTTP to HTTPS safely?

Use a dedicated port 80 server block with return 301 to the HTTPS URL, ensuring TLS listeners exist first.

Q.When should I prefer return over rewrite?

return is simpler and faster for redirects; rewrite is for complex URI transforms.

Q.How does SNI relate to server blocks?

Clients send the server name during TLS handshake; nginx picks the matching server block before HTTP parsing completes.

Q.When should I use map for routing?

map helps route by Host, cookie, or header without duplicating large server blocks or abusing if.

Q.Why might the wrong server block answer?

Overlapping listen directives, missing server_name, or TLS defaults on shared IPs—verify default_server and name lists.
TOPICS & TAGS
server_namedefault_servernginx server blocks