非常教程

Nginx参考手册

ngx_http_ssl_module

ngx_http_ssl_module

  • 示例配置
  • 指令
  • ssl
  • ssl_buffer_size
  • ssl_certificate
  • ssl_certificate_key
  • ssl_ciphers
  • ssl_client_certificate
  • ssl_crl
  • ssl_dhparam
  • ssl_ecdh_curve
  • ssl_password_file
  • ssl_prefer_server_ciphers
  • ssl_protocols
  • ssl_session_cache
  • ssl_session_ticket_key
  • ssl_session_tickets
  • ssl_session_timeout
  • ssl_stapling
  • ssl_stapling_file
  • ssl_stapling_responder
  • ssl_stapling_verify
  • ssl_trusted_certificate
  • ssl_verify_client
  • ssl_verify_depth
  • 错误处理
  • 嵌入式变量

ngx_http_ssl_module模块为HTTPS提供了必要的支持。

该模块不是默认生成的,它应该使用--with-http_ssl_module配置参数启用。

该模块需要OpenSSL库。

示例配置

为了减少处理器负载,建议使用

  • 设置工作进程的数量等于处理器的数量,
  • 启用保持连接的连接,
  • 启用共享会话缓存,
  • 禁用内置会话缓存,
  • 并可能增加会话生存期(默认为5分钟):
worker_processes auto;

http {

    ...

    server {
        listen              443 ssl;
        keepalive_timeout   70;

        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
        ssl_certificate     /usr/local/nginx/conf/cert.pem;
        ssl_certificate_key /usr/local/nginx/conf/cert.key;
        ssl_session_cache   shared:SSL:10m;
        ssl_session_timeout 10m;

        ...
    }

指令

句法:

ssl 开启 | 关闭;

默认:

ssl 关闭;

语境:

http,服务器

为给定的虚拟服务器启用HTTPS协议。

建议使用ssllisten指令的参数而不是此指令。

句法:

ssl_buffer_size大小;

默认:

ssl_buffer_size 16k;

语境:

http,服务器

设置用于发送数据的缓冲区的大小。

默认情况下,缓冲区大小为16k,这对应于发送大响应时的最小开销。为了最小化第一个字节的时间,使用较小的值可能是有益的,例如:

ssl_buffer_size 4k;

句法:

ssl_certificate文件;

默认:

语境:

http,服务器

file为给定的虚拟服务器指定一个带有PEM格式的证书。如果除了主要证书之外还应指定中间证书,则应按照以下顺序在同一文件中指定它们:主要证书为先,然后为中间证书。PEM格式的密钥可以放在同一个文件中。

从版本1.11.0开始,可以多次指定此指令以加载不同类型的证书,例如RSA和ECDSA:

server {
    listen              443 ssl;
    server_name         example.com;

    ssl_certificate     example.com.rsa.crt;
    ssl_certificate_key example.com.rsa.key;

    ssl_certificate     example.com.ecdsa.crt;
    ssl_certificate_key example.com.ecdsa.key;

    ...
}

只有OpenSSL 1.0.2或更高版本支持不同证书的单独证书链。对于旧版本,只能使用一个证书链。

应该记住的是,由于HTTPS协议限制了最大的互操作性,虚拟服务器应该监听不同的IP地址。

句法:

ssl_certificate_key文件;

默认:

语境:

http,服务器

指定file给定虚拟服务器的PEM格式密钥。

enginenameid可以指定代替file(1.7.9),它加载与指定的密钥id从OpenSSL的发动机name

句法:

ssl_ciphers密码;

默认:

ssl_ciphers HIGH:!aNULL:!MD5;

语境:

http,服务器

指定启用的密码。密码以OpenSSL库理解的格式指定,例如:

ssl_ciphers ALL:!aNULL:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

完整列表可以使用“ openssl ciphers”命令查看。

以前版本的nginx默认使用不同的密码。

句法:

ssl_client_certificate文件;

默认:

语境:

http,服务器

file如果启用了ssl_stapling,则指定一个包含PEM格式的可信CA证书,用于验证客户端证书和OCSP响应。

证书列表将发送给客户。如果不需要,可以使用ssl_trusted_certificate指令。

句法:

ssl_crl文件;

默认:

语境:

http,服务器

指定file用于验证客户端证书的PEM格式的带撤销证书(CRL)。

句法:

ssl_dhparam文件;

默认:

语境:

http,服务器

指定具有DHE密码的DH参数的文件。

句法:

ssl_ecdh_curve curve;

默认:

ssl_ecdh_curve auto;

语境:

http,服务器

指定一个用于ECDHE密码的curve

使用OpenSSL 1.0.2或更高版本时,可以指定多条曲线(1.11.0),例如:

ssl_ecdh_curve prime256v1:secp384r1;

特殊值auto(1.11.0)指示nginx在使用OpenSSL 1.0.2或更高prime256v1版本时使用内置于OpenSSL库中的列表,或使用旧版本。

在版本1.11.0之前,默认使用prime256v1曲线。

句法:

ssl_password_file文件;

默认:

语境:

http,服务器

指定file密码的密码,其中每个密码在单独的行上指定。加载密钥时会依次尝试密码。

例:

http {
    ssl_password_file /etc/keys/global.pass;
    ...

    server {
        server_name www1.example.com;
        ssl_certificate_key /etc/keys/first.key;
    }

    server {
        server_name www2.example.com;

        # named pipe can also be used instead of a file
        ssl_password_file /etc/keys/fifo;
        ssl_certificate_key /etc/keys/second.key;
    }
}

句法:

ssl_prefer_server_ciphers 开启 | 关闭;

默认:

ssl_prefer_server_ciphers 关闭;

语境:

http,服务器

指定在使用SSLv3和TLS协议时,服务器密码应优于客户端密码。

句法:

ssl_protocols SSLv2 TLSv1 TLSv1.2;

默认:

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

语境:

http,服务器

启用指定的协议。

TLSv1.1TLSv1.2参数(1.1.13,1.0.12)的OpenSSL 1.0.1或更高时才使用工作。TLSv1.3只有在使用支持TLSv1.3的OpenSSL 1.1.1时,参数(1.13.0)才有效。

句法:

ssl_session_cache off | none | builtin[:size];

默认:

ssl_session_cache none;

语境:

http,服务器

设置存储会话参数的高速缓存的类型和大小。缓存可以是以下任何一种类型:

off严禁使用会话缓存:nginx明确告诉客户端会话可能不会被重用。

none会话缓存的使用被轻轻禁止:nginx告诉客户端会话可能会被重用,但实际上并不会将会话参数存储在缓存中。

builtin在OpenSSL中构建的缓存; 仅由一个工作进程使用。缓存大小在会话中指定。如果没有给出大小,则等于20480个会话。使用内置高速缓存可能导致内存碎片。

shared所有工作进程之间共享的缓存。缓存大小以字节为单位指定; 一兆字节可以存储大约4000个会话。每个共享缓存都应该有一个任意名称。具有相同名称的缓存可以用于多个虚拟服务器。

两种缓存类型可以同时使用,例如:

ssl_session_cache builtin:1000 shared:SSL:10m;

但只使用没有内置缓存的共享缓存应该更高效。

句法:

ssl_session_ticket_key文件;

默认:

语境:

http,服务器

file使用用于加密和解密TLS会话票据的密钥设置a 。如果必须在多个服务器之间共享相同的密钥,则该指令是必需的。默认情况下,使用随机生成的密钥。

如果指定了多个键值,则仅使用第一个键值来加密TLS会话票据。这允许配置键值旋转,例如:

ssl_session_ticket_key current.key;
ssl_session_ticket_key previous.key;

file必须含有80或48个字节的随机数据,并且可以使用下面的命令创建:

openssl rand 80 > ticket.key

根据文件大小,AES256(80字节密钥,1.11.8)或AES128(48字节密钥)用于加密。

句法:

ssl_session_tickets开启| 关闭;

默认:

ssl_session_tickets on;

语境:

http,服务器

通过TLS会话票据启用或禁用会话恢复。

句法:

ssl_session_timeout时间;

默认:

ssl_session_timeout 5m;

语境:

http,服务器

指定客户端可以重新使用会话参数的时间。

句法:

ssl_stapling开启|关闭;

默认:

ssl_stapling关闭;

语境:

http,服务器

启用或禁用服务器对OCSP响应的装订。例:

ssl_stapling on;
resolver 192.0.2.1;

要使OCSP装订工作,应该知道服务器证书颁发者的证书。如果ssl_certificate文件不包含中间证书,则服务器证书颁发者的证书应存在于ssl_trusted_certificate文件中。

对于OCSP响应者主机名的解析,还应指定解析器指令。

句法:

ssl_stapling_file文件;

默认:

语境:

http,服务器

设置时,装订的OCSP响应将取自file指定的服务器,而不是查询服务器证书中指定的OCSP响应器。

该文件应该是由“ openssl ocsp”命令产生的DER格式。

句法:

ssl_stapling_responder url;

默认:

语境:

http,服务器

覆盖“ 授权信息访问 ”证书扩展中指定的OCSP响应者的URL 。

仅支持http://OCSP响应者:

ssl_stapling_responder http://ocsp.example.com/;

句法:

ssl_stapling_verify on | 关闭;

默认:

ssl_stapling_verify off;

语境:

http,服务器

启用或禁用服务器验证OCSP响应。

要使验证生效,应使用ssl_trusted_certificate指令将服务器证书颁发者,根证书和所有中间证书的证书配置为可信。

句法:

ssl_trusted_certificate文件;

默认:

语境:

http,服务器

如果启用了ssl_stapling,则指定一个包含PEM格式可信CA证书的文件,用于验证客户端证书和OCSP响应。

与由ssl_client_certificate设置的证书相比,这些证书的列表不会发送给客户端。

句法:

ssl_verify_client开启|关闭|可选| optional_no_ca;

默认:

ssl_verify_client 关闭;

语境:

http,服务器

启用客户端证书的验证。验证结果存储在$ ssl_client_verify变量中。

optional参数(0.8.7+)请求的客户端证书,并验证它证书是否存在。

optional_no_ca参数(1.3.8,1.2.5)要求客户端证书,但不要求它由受信任的CA证书进行签名。这适用于nginx外部的服务执行实际证书验证的情况。证书的内容可以通过$ ssl_client_cert变量访问。

句法:

ssl_verify_depth数字;

默认:

ssl_verify_depth 1;

语境:

http,服务器

设置客户端证书链中的验证深度。

错误处理

ngx_http_ssl_module模块支持几个可用于使用error_page指令重定向的非标准错误代码:

495客户端证书验证期间发生错误; 496客户没有提交所需的证书; 497定期请求已发送到HTTPS端口。

重定向发生后,该请求被完全解析和变量,如$request_uri$uri$args等,都可用。

嵌入式变量

ngx_http_ssl_module模块支持多个嵌入式变量:

$ssl_cipher返回用于建立SSL连接的密码串; $ssl_ciphers返回客户端支持的密码列表(1.11.7)。已知密码按名称列出,未知以十六进制显示,例如:

AES128-SHA:AES256-SHA:0x00ff

只有在使用OpenSSL版本1.0.2或更高版本时才支持该变量。对于旧版本,该变量仅适用于新会话,并只列出已知密码。

$ssl_client_escaped_cert以建立的SSL连接(1.13.5)返回PEM格式的客户端证书(urlencoded); $ssl_client_cert以建立的SSL连接的PEM格式返回客户端证书,除第一行之外的每行均预先加上制表符; 这旨在用于proxy_set_header指令中;

该变量已被弃用,$ssl_client_escaped_cert应该使用该变量。

$ssl_client_fingerprint为已建立的SSL连接(1.7.1)返回客户端证书的SHA1指纹; $ssl_client_i_dn根据RFC 2253(1.11.6)返回已建立的SSL连接的客户端证书的“颁发者DN”字符串; $ssl_client_i_dn_legacy为已建立的SSL连接返回客户端证书的“颁发者DN”字符串;

在版本1.11.6之前,变量名是$ssl_client_i_dn

$ssl_client_raw_cert以建立的SSL连接的PEM格式返回客户端证书; $ssl_client_s_dn根据RFC 2253(1.11.6)返回已建立的SSL连接的客户端证书的“主题DN”字符串; $ssl_client_s_dn_legacy为建立的SSL连接返回客户端证书的“主题DN”字符串;

在版本1.11.6之前,变量名是$ssl_client_s_dn

$ssl_client_serial为已建立的SSL连接返回客户端证书的序列号; $ssl_client_v_end返回客户端证书的结束日期(1.11.7); $ssl_client_v_remain返回客户端证书过期的天数(1.11.7); $ssl_client_v_start返回客户证书的开始日期(1.11.7); $ssl_client_verify返回客户端证书验证结果:“ SUCCESS”,“ FAILED:reason”和“ NONE”如果证书不存在;

在版本1.11.7之前,“ FAILED”结果不包含reason字符串。

$ssl_curves返回客户端支持的曲线列表(1.11.7)。已知曲线按名称列出,未知以十六进制显示,例如:

0x001d:prime256v1:secp521r1:secp384r1

只有在使用OpenSSL版本1.0.2或更高版本时才支持该变量。对于旧版本,变量值将是一个空字符串。该变量仅适用于新会话。

$ssl_protocol返回建立的SSL连接的协议; $ssl_server_name通过SNI(1.7.0)返回请求的服务器名称; $ssl_session_id返回建立的SSL连接的会话标识符; 如果SSL会话被重用,则$ssl_session_reused返回“ r”;否则返回“ ” .(1.5.11)。

ngx_http_ssl_module
Nginx

Nginx是一款轻量级的 Web 服务器/反向代理服务器及电子邮件代理服务器,可在 BSD-like 协议下发行。其特点是占有内存少,并发能力强。

主页 https://nginx.org/
源码 http://hg.nginx.org/nginx
发布版本 1.13.6

Nginx目录

1.指南 | Guides
2.核心 | Core
3.ngx_google_perftools_module
4.ngx_http_access_module
5.ngx_http_addition_module
6.ngx_http_api_module
7.ngx_http_auth_basic_module
8.ngx_http_auth_jwt_module
9.ngx_http_auth_request_module
10.ngx_http_autoindex_module
11.ngx_http_browser_module
12.ngx_http_charset_module
13.ngx_http_core_module
14.ngx_http_dav_module
15.ngx_http_empty_gif_module
16.ngx_http_f4f_module
17.ngx_http_fastcgi_module
18.ngx_http_flv_module
19.ngx_http_geoip_module
20.ngx_http_geo_module
21.ngx_http_gunzip_module
22.ngx_http_gzip_module
23.ngx_http_gzip_static_module
24.ngx_http_headers_module
25.ngx_http_hls_module
26.ngx_http_image_filter_module
27.ngx_http_index_module
28.ngx_http_js_module
29.ngx_http_keyval_module
30.ngx_http_limit_conn_module
31.ngx_http_limit_req_module
32.ngx_http_log_module
33.ngx_http_map_module
34.ngx_http_memcached_module
35.ngx_http_mirror_module
36.ngx_http_mp4_module
37.ngx_http_perl_module
38.ngx_http_proxy_module
39.ngx_http_random_index_module
40.ngx_http_realip_module
41.ngx_http_referer_module
42.ngx_http_rewrite_module
43.ngx_http_scgi_module
44.ngx_http_secure_link_module
45.ngx_http_session_log_module
46.ngx_http_slice_module
47.ngx_http_spdy_module
48.ngx_http_split_clients_module
49.ngx_http_ssi_module
50.ngx_http_ssl_module
51.ngx_http_status_module
52.ngx_http_stub_status_module
53.ngx_http_sub_module
54.ngx_http_upstream_conf_module
55.ngx_http_upstream_hc_module
56.ngx_http_upstream_module
57.ngx_http_userid_module
58.ngx_http_uwsgi_module
59.ngx_http_v2_module
60.ngx_http_xslt_module
61.ngx_mail_auth_http_module
62.ngx_mail_core_module
63.ngx_mail_imap_module
64.ngx_mail_pop3_module
65.ngx_mail_proxy_module
66.ngx_mail_smtp_module
67.ngx_mail_ssl_module
68.ngx_stream_access_module
69.ngx_stream_core_module
70.ngx_stream_geoip_module
71.ngx_stream_geo_module
72.ngx_stream_js_module
73.ngx_stream_limit_conn_module
74.ngx_stream_log_module
75.ngx_stream_map_module
76.ngx_stream_proxy_module
77.ngx_stream_realip_module
78.ngx_stream_return_module
79.ngx_stream_split_clients_module
80.ngx_stream_ssl_module
81.ngx_stream_ssl_preread_module
82.ngx_stream_upstream_hc_module
83.ngx_stream_upstream_module