概述
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