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 / 问题 / 630413
Accepted
weeheavy
weeheavy
Asked: 2014-09-23 12:31:30 +0800 CST2014-09-23 12:31:30 +0800 CST 2014-09-23 12:31:30 +0800 CST

Nginx/Apache:仅当 X-Forwarded-Proto 为 https 时才设置 HSTS

  • 772

我得到了以下设置:

Internet => nginx[public:80 +443, SSL termination)
=> Varnish[localhost:81] => Apache[localhost:82]

现在有些网站只能通过 HTTPS 和有效的 SSL 证书访问。对于这几个例外,我想在 nginx(首选,或在 Apache)上激活 HSTS。

问题:

  • 在 nginx 上,我需要一些逻辑 à laif Host = foo.tld然后 set Strict-Transport-Security xxx,但根据http://wiki.nginx.org/IfIsEvil不应该if在 alocation
  • 在 Apache 上,我需要类似的东西if X-Forwarded-Proto 443 set Strict-Transport-Security xxx,但我似乎无法使用SetEnvIf(Apache 2.2)构建它

我的逻辑有问题吗?方法的另一个想法?

这是当前活动的配置:

nginx

服务器 {
        server_tokens 关闭;

        听 xx.xx.xxx.xxx:80;
        server_name 本地主机;

        地点 / {
                proxy_pass http://127.0.0.1:81;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Forwarded-Port 80;
                proxy_set_header 主机 $host;
                add_header X-XSS-Protection "1; mode=block";
        }
}

服务器 {
        server_tokens 关闭;

        听 xx.xx.xxx.xxx:443 ssl;
        server_name 本地主机;

        开启ssl;
        ssl_certificate /etc/ssl/foo.crt;
        ssl_certificate_key /etc/ssl/private/foo.key;

        ssl_session_timeout 10m;

        # http://blog.ivanristic.com/2013/08/configuring-apache-nginx-and-openssl-for-forward-secrecy.html
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers 开启;
        ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS";

        地点 / {
                proxy_pass http://127.0.0.1:81;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Forwarded-Port 443;
                proxy_set_header 主机 $host;
                add_header X-XSS-Protection "1; mode=block";
        }
}

漆

无特殊配置。

阿帕奇

<VirtualHost *:82>
[...] nothing special
</VirtualHost>
apache-2.2
  • 2 2 个回答
  • 6230 Views

2 个回答

  • Voted
  1. Best Answer
    Alexey Ten
    2014-09-24T23:51:29+08:002014-09-24T23:51:29+08:00

    您可以有多个服务器块。因此,只需为需要 HSTS 的域添加新的服务器块。

    server {
        listen xx.xx.xxx.xxx:443 ssl default_server;
    
        # all ssl stuff
        # and other directives
    }
    
    server {
        listen xx.xx.xxx.xxx:443 ssl;
        server_name example.com other.example.com;
    
        # all ssl stuff
        # and other directives with HSTS enabled
    }
    

    example.com这里第一个块将处理除and之外的所有 https 连接other.example.com。

    如果你有标志,你不需要ssl on指令。ssllisten

    编辑

    还有另一种解决方案,只有一个服务器块:

    map $scheme:$host $hsts_header {
        default "";
        https:example.com "max-age=31536000";
        https:other.example.com "max-age=31536000";
    }
    
    server {
        server_tokens off;
    
        listen xx.xx.xxx.xxx:80;
        listen xx.xx.xxx.xxx:443 ssl;
    
        ssl_certificate /etc/ssl/foo.crt;
        ssl_certificate_key /etc/ssl/private/foo.key;
    
        ssl_session_timeout 10m;
    
        # ... other ssl stuff
    
        location / {
            proxy_pass http://127.0.0.1:81;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_set_header Host $host;
            add_header X-XSS-Protection "1; mode=block";
            add_header Strict-Transport-Security $hsts_header;
        }
    }
    

    我们使用map定义 HSTS 标头值并使用事实,而不是 nginx 不会添加具有空值的标头。

    • 6
  2. nicoo
    2015-09-11T14:07:14+08:002015-09-11T14:07:14+08:00

    您还可以无条件地添加 HSTS 标头。

    事实上,它只有在连接成功、通过 TLS 且没有证书警告的情况下才会生效。参见 RFC6797的第 5.1 段。

    • -1

相关问题

  • Apache Django Mod_Wsgi - 自动重新加载应用程序

  • Apache:对多个虚拟主机使用相同的目录指令

  • Apache 上的子域不工作 - 找不到服务器

  • PHP 作为 CGI 还是 Apache 模块?

  • 避免将某些丢失的文件记录到 Apache2 错误日志中

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