首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安装nginx以要求除给定源IP之外的所有位置的特定条件。

安装nginx以要求除给定源IP之外的所有位置的特定条件。
EN

Server Fault用户
提问于 2021-06-30 00:27:42
回答 1查看 889关注 0票数 1

我正在寻找一个设置,在这里,除了一个源IP之外,我希望拥有所有SSL客户端证书。

我的想法是

代码语言:javascript
复制
 ssl_verify_client optional;

并在地点添加详细的if语句。然而,我不知道如何写这样一个if语句。

代码语言:javascript
复制
  # 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,我不想验证客户端证书。

我需要的是

代码语言:javascript
复制
    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地址进行有条件过滤无关。

EN

回答 1

Server Fault用户

回答已采纳

发布于 2021-06-30 09:18:32

if语句的Nginx不支持多个条件。因此,作为解决方案和客户端IP在我们的使用中评估,解决方案可以实现类似于它是如何在https://www.nginx.com/blog/rate-limiting-nginx/#Advanced-Configuration-Examples上实现的。基本上,解决办法是这样的.

代码语言:javascript
复制
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的实际值。

替代答案

代码语言:javascript
复制
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
    }
}
票数 1
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1068212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档