我正在寻找一个设置,在这里,除了一个源IP之外,我希望拥有所有SSL客户端证书。
我的想法是
ssl_verify_client optional;并在地点添加详细的if语句。然而,我不知道如何写这样一个if语句。
# this requires ssl client certificates for all locations
location / {
if ($ssl_client_verify != "SUCCESS") {
return 403;
...
}
# now what to write to require ssl certs except if source IP is e.g. 1.2.3.4
location /two {
if (?????) {
return 403;
...
}Edit:附加信息
值为optional的开关D3告诉客户端,他们可以但不必发送客户端证书。
因此,通过检查变量$var_ssl_客户端_验证,我可以看到是否提供了客户端证书和有效证书(SUCCESS)。此规则适用于所有没有给定源IP的客户端。对于一个特定的源IP,我不想验证客户端证书。
我需要的是
if ($ssl_client_verify != "SUCCESS" and source_ip != 1.2.3.4 ) {
return 403;
}Edit 2:我将标题从setup nginx to require client certs for all but a given source IP改为setup nginx to require certain conditions of a location for all but a given source IP,因为我真正挣扎的与客户端证书无关,而是与合并if语句和对源IP地址进行有条件过滤无关。
发布于 2021-06-30 09:18:32
if语句的Nginx不支持多个条件。因此,作为解决方案和客户端IP在我们的使用中评估,解决方案可以实现类似于它是如何在https://www.nginx.com/blog/rate-limiting-nginx/#Advanced-Configuration-Examples上实现的。基本上,解决办法是这样的.
geo $limit {
default 1;
1.2.3.4 0; #please replace the example IP with the actual IP.
}
ssl_verify_client optional;
ssl_client_certificate /path/to/cert.pem;
map $limit $limit_key {
0 "SUCCESS";
1 $ssl_client_verify;
}
server {
# ... other directives
location / {
if ( $limit_key != 'SUCCESS' ) { return 403; }
# ... other directives
}
}基本上,我们给特定IP的变量赋值"SUCCESS“。对于每一个其他IP,我们分配$ssl_client_verify的实际值。
ssl_verify_client optional;
ssl_client_certificate /path/to/cert.pem;
server {
# ... other directives
# initial value of $variable is the actual value of $ssl_client_verify
set $variable $ssl_client_verify;
# here we re-assign $variable with "SUCCESS" for our specific IP
# please replace 1.2.3.4 with the actual IP
if ( $remote_addr = "1.2.3.4" ) {
set $variable "SUCCESS";
}
location / {
if ( $variable != 'SUCCESS' ) { return 403; }
# ... other directives
}
}https://serverfault.com/questions/1068212
复制相似问题