Nginx : thousands of server_name

You can put the server_name directive in a file all by itself and then just include it from your server block:

# example.com.conf
server {
  include /path/to/server_names;
  ...
}

# /path/to/server_names
server_name
  example.com
  domain.com
  foo.com
;

The server_names file just needs server_name at the top and ; at the bottom to be a valid directive.


The server_name declaration in your server {} block can be a Regular expression, this should allow you to match server_name s within a set pattern.

More information on this can be seen here: http://nginx.org/en/docs/http/server_names.html#regex_names