所有 Nginx 虚拟主机都可以共享相同的 ssl_session_cache 吗?
Can all Nginx vhosts share the same ssl_session_cache?
对我来说,the Nginx docs about how ssl_session_cache
works,有点不清楚。我想知道是否:
ssl_session_cache shared:SSL:10m;
在 http
块或 each server
(即虚拟主机)块中声明,导致 1) 一个名为SSL,10 MB 大。或 2) 在每台服务器一个 10 MB 缓存中,所有缓存的总大小 = num servers x 10 MB。
文档:
shared
a cache shared between all worker processes. The cache size is
specified in bytes; one megabyte can store about 4000 sessions. Each
shared cache should have an arbitrary name. A cache with the same name
can be used in several virtual servers.
如果只有一个缓存,那么我想将其大小乘以服务器数量。所以,如果我有 5 个服务器(即 5 个虚拟主机),那么我会在 http
块中放置 ssl_session_cache
并且:
ssl_session_cache shared:SSL:50m; # 10 * 5 = 50
那么,问题是:ssl_session_cache shared:SSL:10m;
是为每台服务器创建一个 10 MB 缓存,还是为所有服务器创建一个 10 MB 缓存?
如果是每台服务器,那么有没有办法为所有服务器配置一个全局缓存? (如果不可能,那为什么不呢,万一有人知道呢?)在我看来,这似乎会导致更有效的内存使用。 (因为一台服务器有很多客户端,然后可以使用内存,否则这些内存将专用于目前可能有零客户端的其他服务器。)
查看 ngx_http_ssl_session_cache 在 nxg_http_ssl_module.c it creates one shared memory zone named "SSL", i.e. one ssl session cache. Any subsequent call to ssl_session_cache retrieves the previously configured shared memory zone named "SSL" instead of creating a new one (cmp. ngx_shared_memory_add in ngx_cycle.c 中对 ssl_session_cache 的实现。
这可以通过为相同的名称配置不同的大小来轻松验证,如下所示:
...
ssl_session_cache shared:SSL:4m;
server {
...
ssl_session_cache shared:SSL:50m;
}
这会导致错误消息,例如:
[emerg] the size 52428800 of shared memory zone "SSL" conflicts with already declared size 4194304 in /etc/nginx/nginx.conf:37
详细信息(添加了 KajMagnus)
添加了共享内存区域here:
sscf->shm_zone = ngx_shared_memory_add(cf, &name, n,
&ngx_http_ssl_module);
如您所见,不同的 name
会导致创建不同的缓存。因此,一个人可以拥有许多不同的共享内存缓存,每一个都有自己唯一的名称。但是,每个服务器只能使用一个共享的 SSL 内存区域 — 在 ngx_http_ssl_srv_conf_t *sscf
结构上,每个 SSL 服务器配置只有一个 shm_zone
。
tl;博士
SSL 会话缓存是在 http 级别还是在服务器级别声明并不重要。只要为缓存分配相同的名称,就使用相同的缓存。为防止同名缓存出现错误消息,必须始终使用相同大小。
对我来说,the Nginx docs about how ssl_session_cache
works,有点不清楚。我想知道是否:
ssl_session_cache shared:SSL:10m;
在 http
块或 each server
(即虚拟主机)块中声明,导致 1) 一个名为SSL,10 MB 大。或 2) 在每台服务器一个 10 MB 缓存中,所有缓存的总大小 = num servers x 10 MB。
文档:
shared
a cache shared between all worker processes. The cache size is specified in bytes; one megabyte can store about 4000 sessions. Each shared cache should have an arbitrary name. A cache with the same name can be used in several virtual servers.
如果只有一个缓存,那么我想将其大小乘以服务器数量。所以,如果我有 5 个服务器(即 5 个虚拟主机),那么我会在 http
块中放置 ssl_session_cache
并且:
ssl_session_cache shared:SSL:50m; # 10 * 5 = 50
那么,问题是:ssl_session_cache shared:SSL:10m;
是为每台服务器创建一个 10 MB 缓存,还是为所有服务器创建一个 10 MB 缓存?
如果是每台服务器,那么有没有办法为所有服务器配置一个全局缓存? (如果不可能,那为什么不呢,万一有人知道呢?)在我看来,这似乎会导致更有效的内存使用。 (因为一台服务器有很多客户端,然后可以使用内存,否则这些内存将专用于目前可能有零客户端的其他服务器。)
查看 ngx_http_ssl_session_cache 在 nxg_http_ssl_module.c it creates one shared memory zone named "SSL", i.e. one ssl session cache. Any subsequent call to ssl_session_cache retrieves the previously configured shared memory zone named "SSL" instead of creating a new one (cmp. ngx_shared_memory_add in ngx_cycle.c 中对 ssl_session_cache 的实现。
这可以通过为相同的名称配置不同的大小来轻松验证,如下所示:
...
ssl_session_cache shared:SSL:4m;
server {
...
ssl_session_cache shared:SSL:50m;
}
这会导致错误消息,例如:
[emerg] the size 52428800 of shared memory zone "SSL" conflicts with already declared size 4194304 in /etc/nginx/nginx.conf:37
详细信息(添加了 KajMagnus)
添加了共享内存区域here:
sscf->shm_zone = ngx_shared_memory_add(cf, &name, n,
&ngx_http_ssl_module);
如您所见,不同的 name
会导致创建不同的缓存。因此,一个人可以拥有许多不同的共享内存缓存,每一个都有自己唯一的名称。但是,每个服务器只能使用一个共享的 SSL 内存区域 — 在 ngx_http_ssl_srv_conf_t *sscf
结构上,每个 SSL 服务器配置只有一个 shm_zone
。
tl;博士 SSL 会话缓存是在 http 级别还是在服务器级别声明并不重要。只要为缓存分配相同的名称,就使用相同的缓存。为防止同名缓存出现错误消息,必须始终使用相同大小。