概述
HTTPS 其实就是 HTTP over SSL,也就是让 HTTP 连接建立在 SSL/TLS 安全连接之上。要保证 web 浏览器到服务器的安全连接,HTTPS 几乎是唯一选择。
SSL/TLS 使用证书来创建安全连接,有两种验证模式:
- 仅客户端验证服务器的证书,客户端自己不提供证书;
- 客户端和服务器都互相验证对方的证书;
显然第二种方式安全性更高,一般用网上银行会这么搞,但是,普通的 web 网站只能采用第一种方式。
客户端如何验证服务器的证书呢?
服务器自己的证书必须经过某“权威”证书的签名,而这个“权威”证书又可能经过更权威的证书签名,这么一级一级追溯上去,最顶层那个最权威的证书就称为根证书。根证书直接内置在浏览器中,这样,浏览器就可以利用自己自带的根证书去验证某个服务器的证书是否有效。
如果要提供一个有效的证书,服务器的证书必须从 VeriSign 这样的证书颁发机构签名,这样,浏览器就可以验证通过,否则,浏览器给出一个证书无效的警告。
申请一个证书签名的费用是一年几十~几百刀不等,所以如果只是出于管理目的,可以创建自签名证书,保证管理员通过浏览器安全连接到服务器。
创建自签名的 SSL/TLS 证书
以 test.aorise.org
为例。
SSL/TLS 证书是通过 openssl 生产,因此,需要保证机器上已安装有 openssl 和 openssl-devel。
$ yum install -y openssl openssl-devel
生产服务器 key。
$ openssl genrsa -des3 -out test.aorise.org.key 1024
Generating RSA private key, 1024 bit long modulus
...................................++++++
........................++++++
e is 65537 (0x10001)
Enter pass phrase for test.aorise.org.key:
Verifying - Enter pass phrase for test.aorise.org.key:
提示输入密码,比如:123456,2次必须一致。
创建签名请求。
$ req -new -key test.aorise.org.key -out test.aorise.org.csr
Enter pass phrase for test.aorise.org.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN#输入国家
State or Province Name (full name) []:HN#地区
Locality Name (eg, city) [Default City]:HH#城市
Organization Name (eg, company) [Default Company Ltd]:Aorise#组织
Organizational Unit Name (eg, section) []:Aorise#组织单位
Common Name (eg, your name or your server's hostname) []:test.aorise.org #可以写你的名字或者域名,如果为了https申请,这个必须和域名一样,即,这里要成域名(localhost、example.com),否则会引发浏览器警报,这里可以用 *.example.com来做泛域名证书
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
移除密码。
因为以后要给 nginx 使用,每次 reload nginx
配置时候都要你验证这个 PAM 密码的,由于生成时候必须输入密码,你可以输入后再删掉。
$ mv test.aorise.org.key test.aorise.org.origin.key
openssl rsa -in test.aorise.org.origin.key -out test.aorise.org.key#除去密码以便reload询问时不需要密码
Enter pass phrase for test.aorise.org.origin.key:
writing RSA key
可以看到重新生成了 localhost.csr。
$ ls
test.aorise.org.csr
test.aorise.org.key
test.aorise.org.origin.key
标记证书。
$ openssl x509 -req -days 3650 -in test.aorise.org.csr -signkey test.aorise.org.key -out test.aorise.org.crt
Signature ok
subject=/C=CN/ST=HN/L=HH/O=Aorise/OU=Aorise/CN=test.aorise.org
Getting Private key
查看 nginx 模块。
$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.11.7
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx
发现无 SSL 模块。
安装 SSL 模块。
$ cd /opt/nginx-1.11.7
$ ./configure --prefix=/usr/local/nginx --with-http_ssl_module
$ make && make install
nginx 配置。
server {
listen 443 ssl;
server_name test.aorise.org;
ssl on;#开启ssl
ssl_session_timeout 5m;
ssl_certificate ~/nginx/cert/localhost.crt;#证书位置
ssl_certificate_key ~/nginx/cert/localhost.key;#私钥位置
ssl_session_cache shared:SSL:1m;
ssl_ciphers HIGH:!aNULL:!MD5;#密码加密方式
ssl_protocols SSLv2 SSLv3 TLSv1;#指定密码为openssl支持的格式
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
#access_log logs/test.aorise.org.access.log main;
error_log logs/test.aorise.org.error.log;
client_max_body_size 1000m;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重启 nginx 即可。
参考地址:
https://www.liaoxuefeng.com/article/0014189023237367e8d42829de24b6eaf893ca47df4fb5e000
https://github.com/michaelliao/itranswarp.js/blob/master/conf/ssl/gencert.sh
http://www.cnblogs.com/saneri/p/5391821.html
http://blog.csdn.net/goolejuck/article/details/54089170