Why use the server_name directive?
Nginx will try the server_name to see if it matches the “Host” header in the client request. If the server_name matches then the server block will be used.
Server_name does not have to be simply plain text. It can be a regular expression with capture blocks. I’ve successfully implemented vanity urls using this method.
Wrinkles to watch for
Multiple servernames can be specified in a single line
- You may want to specify both the www and non www form of the domain name
- The first name in the list becomes the primary server name.
- Capture blocks do not persist throughout the configuration file. They don’t propagate into location context blocks.
How to troubleshoot?
Insights from others
Watch this space.
What directives are typically seen alongside this?
server
listen
location
root