AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1068212
Accepted
gelonida
gelonida
Asked: 2021-06-30 16:27:42 +0800 CST2021-06-30 16:27:42 +0800 CST 2021-06-30 16:27:42 +0800 CST

设置 nginx 以要求除给定源 IP 之外的所有位置的某些条件

  • 772

我正在寻找一种设置,我想为除一个源 IP 之外的所有 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; 
    ...
  }

编辑:附加信息

带有该值的开关ssl_verify_clientoptional告诉客户端,他们可以但不必发送客户端证书。

因此,通过检查变量$var_ssl_client_verify我可以查看是否提供了客户端证书并且是否有效(SUCCESS)。此规则应适用于没有给定源 IP 的所有客户端。对于一个特定的源 IP,我不想验证客户端证书。

我需要的是类似

    if ($ssl_client_verify != "SUCCESS" and source_ip != 1.2.3.4 ) { 
       return 403; 
    }

编辑 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 地址进行过滤。

nginx ssl-certificate
  • 1 1 个回答
  • 610 Views

1 个回答

  • Voted
  1. Best Answer
    Pothi Kalimuthu
    2021-07-01T01:18:32+08:002021-07-01T01:18:32+08:00

    ifNginx for语句不支持多个条件。因此,作为一种解决方法并在我们的用例中评估客户端 IP,该解决方案的实现方式类似于https://www.nginx.com/blog/rate-limiting-nginx/#Advanced-Configuration-例子。基本上,解决方案是这样的......

    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
        }
    }
    

    基本上,我们将值“SUCCESS”分配给特定 IP 的变量。对于每个其他 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
        }
    }
    
    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve