Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
you should accept kolbijack answer, if it is the one that helped. –  Oneiroi Jul 10 '12 at 13:37

2 Answers 2

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.

share|improve this answer
    
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 '12 at 12:07
    
@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 '12 at 1:15
    
60Mb for 1 000 000 and Request rate: 34716.8 req/s (0.0 ms/req) –  VBart Jul 11 '12 at 1:22
    
That's considerably better than Apache managed, at least with the prefork MPM. Handy to know. –  Ladadadada Jul 11 '12 at 12:36

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

share|improve this answer
    
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 '12 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 '12 at 11:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.