前言
如何建立云WAF这篇文章讲述了我构造http的云WAF的经历。最近博客迁移到了https,所以就存在一个「https的WAF环境,应该如何配置」的问题。
如何配置
在手上没有什么资料的前提下,我开始进行大胆猜测。我们知道这个过程一定是:
https://joychou.org
waf.joychou.me
并且端口为443的block,并将流量进行反向代理转发到后端服务器前面2步都没问题,那就只有第三步的可能会有问题了。要转发https://joychou.org
的流量,肯定需要https://joychou.org
网站的https证书和私钥。所以在waf.joychou.me
的server block先配上https://joychou.org
的证书和私钥。
现在的配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
server { listen 443; server_name waf.joychou.me; ssl on; ssl_certificate /usr/local/nginx/ssl/chained.pem; ssl_certificate_key /usr/local/nginx/ssl/domain.key; location / { proxy_buffers 8 16k; proxy_buffer_size 32k; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass https://x.x.x.x; } } |
设置proxy_buffer
,是为了解决一个502的error,可忽略。这个时候,访问https://joychou.org
已经可以正常访问了,WAF也能正常拦截。
但是,一般的云waf的域名,不会让直接访问。比如,我们先ping下www.aliyun.com
,WAF的域名是sh.wagbridge.aliyun.com.gds.alibabadns.com
,直接访问返回404,如果该WAF域名能直接访问,那访问的效果等同于www.aliyun.com
,所以肯定是有问题的,那就需要设置。
不过,只要我们设置下host就能访问了:curl -v -H 'host:www.aliyun.com' 'https://sh.wagbridge.aliyun.com.gds.alibabadns.com/?'
所以,根据host来配置就行。添加以下配置:
1 2 3 |
if ($host != 'joychou.org') { return 404 "Good Game"; } |
配置
443端口配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
server { listen 443; server_name waf.joychou.me; ssl on; ssl_certificate /usr/local/nginx/ssl/chained.pem; ssl_certificate_key /usr/local/nginx/ssl/domain.key; if ($host != 'joychou.org') { return 404 "Good Game"; } location / { proxy_buffers 8 16k; proxy_buffer_size 32k; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass https://x.x.x.x; } } |
这样,配置完成后,访问https://joychou.org
就已经没有问题了。不过还需要对80端口进行一个rewrite到https的操作。
80端口配置:
1 2 3 4 5 6 7 8 |
server { listen 80; server_name waf.joychou.me; if ($host != 'joychou.org') { return 404 "Good Game"; } rewrite ^(.*) https://$host$1 permanent; } |
总结
云WAF就是将所有的域名、IP、端口、协议做成配置化,而不是向我文中的硬编码。
原文链接:https://joychou.org/web/how-to-build-https-cloud-waf.html
本文由 安全周 作者:追梦 发表,转载请注明来源!
您必须[登录] 才能发表留言!