如何让 nginx 记录使用的 SSL/TLS 协议和密码套件?

ger*_*ijk 28 security ssl nginx logging tls

我的目标是确保客户端连接到我的 nginx 的适当安全性。我正在按照Mozilla 的指南在我的 nginx 安装上正确配置 TLS,但我没有对实践中使用的实际协议/密码套件的概述。

我现在所拥有的:

server {
    listen 443;
    ssl on;
    ssl_certificate /path/to/signed_cert_plus_intermediates;
    ssl_certificate_key /path/to/private_key;
    ssl_dhparam /path/to/dhparam.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'the_long_ciphersuite_listed_there';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:50m;
}
Run Code Online (Sandbox Code Playgroud)

有了这个,我想记录用于连接的 SSL 协议以及在客户端/服务器协商后选择的密码套件。例如:

10.1.2.3 - - [13/Aug/2014:12:34:56 +0200] "GET / HTTP/1.1" 200 1234 "-" "User agent bla"
Run Code Online (Sandbox Code Playgroud)

10.1.2.3 - - [13/Aug/2014:12:34:56 +0200] ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 "GET / HTTP/1.1" 200 1234 "-" "User agent bla"
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我可以快速识别使用不支持 PFS 或其他相关安全启用技术的过时浏览器或自动化机器的客户端。

如何配置 nginx 以记录此信息?

小智 55

添加$ssl_cipher到您的log_format配置中。

有关所有 SSL 相关变量,请参阅http://nginx.org/en/docs/http/ngx_http_ssl_module.html#variables

例子

log_formathttp上下文中定义自定义(例如/etc/nginx/nginx.conf):

log_format combined_ssl '$remote_addr - $remote_user [$time_local] '
                        '$ssl_protocol/$ssl_cipher '
                        '"$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent"';
Run Code Online (Sandbox Code Playgroud)

以上是基于combined带有附加'$ssl_protocol/$ssl_cipher '行的默认格式。

然后在server上下文(启用 SSL)中添加access_log具有自定义日志格式的指令:

server {
  listen 443;
  ssl on;
  access_log /var/log/nginx/access.log combined_ssl;
  [...]
}
Run Code Online (Sandbox Code Playgroud)

重启nginx后,日志显示如下:

10.1.2.3 - - [13/Aug/2014:12:34:56 +0200] TLSv1.2/ECDHE-RSA-AES128-GCM-SHA256 "GET / HTTP/1.1" 200 1234 "-" "User agent bla"
Run Code Online (Sandbox Code Playgroud)