什么决定了 Nginx 配置中服务器块数量的实际限制?

Sin*_*nür 5 nginx

我试图弄清楚仅由简单服务器块组成的 Nginx 配置是否可行。每个块服务一个子域,并将子域定向到另一个 URL。当然,特定上下文中的最大值取决于参数,因此我对确定实际极限的因素更感兴趣。

例如,额外服务器块的额外成本(就内存开销而言)是否始终不变?分派到特定服务器块以处理请求的成本是一个常数,还是配置中服务器块数量的函数?

一个示例服务器块是:

server {
    server_name subdomain.example.com;
    return 301 http://some.other.example.org/subdomain;
}
Run Code Online (Sandbox Code Playgroud)

例如,每个内核每 GB 内存或其他相关参数,我可以拥有多少个?

谢谢你。

Mic*_*ton 7

影响server_name您可以合理拥有多少s的最大因素是 CPU 的缓存大小(当然还有速度)。

First, nginx stores all of the server_names that you define into three hash tables (depending on whether you used wildcards in the name) per IP/port pair that nginx listens on. The size of these structures is optimized to be a multiple of the CPU's cache line size, and nginx intends to be able to match the server_name for an incoming request entirely from CPU cache without having to go to the (relatively) much slower RAM at all.

Out of the box, nginx sets up hash tables for server names with 512 entries of 32 bytes each. This comes up to 16 KiB and easily fits into a CPU's L1 cache, or at least in the L2 cache. And even if you need to expand it, it ought to still be small enough to fit in the cache most of the time.

This strategy suggests that you should strive to keep the list of names to a minimum.

For instance, even if it may be "slower" to match a wildcard entry such as .example.com, it may be faster on average than attempting to match against several hundred subdomains of example.com which are defined explicitly.

See also the nginx documentation on optimizing server_names.