12

how can I configure Nginx restrict to thousands of different server_name without creating thousands of external config files or writing a very long server_name line ?

Indeed, I would like Nginx be a reverse-proxy only for server1.com, server2.com, etc...

The best solution for me would be to have a single external config file containing my restricted server_names : server1.com, server2.com, etc...

Is it possible ?


Ok, thank you for your answers, it works without reg. expressions. Just a big server_name list as explained by kolbijack. Thx.

1
  • you should accept kolbijack answer, if it is the one that helped.
    – Oneiroi
    Jul 10, 2012 at 13:37

2 Answers 2

20

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.

4
  • Beware of latency/lookup delays/RAM usage if you do this. I've done some testing with large lists (around 5,000 ServerAlias directives and up to 16 million Deny from directives) in Apache. 5,000 domains produces a measurable but small delay. When increasing the number, I found the delay started becoming noticeable at around 50,000. With Apache (and probably also with nginx), the list is held in RAM which meant that with the largest set I tried, I could only start one child process within my available RAM.
    – Ladadadada
    Jul 6, 2012 at 12:07
  • 1
    @Ladadadada LOL. I've just checked. Nginx uses 30Mb of RAM for 100 000 domains in the server_name directive and respond Request rate: 36900.6 req/s (0.0 ms/req)
    – VBart
    Jul 11, 2012 at 1:15
  • 1
    60Mb for 1 000 000 and Request rate: 34716.8 req/s (0.0 ms/req)
    – VBart
    Jul 11, 2012 at 1:22
  • That's considerably better than Apache managed, at least with the prefork MPM. Handy to know.
    – Ladadadada
    Jul 11, 2012 at 12:36
7

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

4
  • Thank you, but I cannot declare my custom domains list in a single regular expression. server_name [zeus.domain.com|poseidon.domain.com|etc...]; It would be too long.
    – Fred
    Jul 6, 2012 at 10:38
  • 1
    @fred Can you give an example data set? unless no one domain is like the other at all I'd be fairly confident that a Regex pattern would resolve you issue.
    – Oneiroi
    Jul 6, 2012 at 11:07
  • @Oneiroi: example use case = user generated subdomains. Imagine a software-as-a-service product where each user gets their own subdomain, and you want to use Nginx to route them to different servers. In that case, no one domain is like the other at all.
    – m.stenta
    May 18, 2017 at 2:00
  • @m.stenta ah, fair point. Unless there's some commonality in the subdomains then yeah regex is not going to work. Or you're going to need to tie in the creation of regex derived from the domain itself which imho defeats the purpose.
    – Oneiroi
    Jun 12, 2017 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .