0%

https 的使用和在 Nginx 下启用 https

随着web的发展,开发者对传输过程中的信息安全也应该更为重视。https可以通过ssl协议来保证信息传输过程中的数据安全,成为了一些敏感操作的必备选择。

什么是https

超文本传输安全协议(英语:Hypertext Transfer Protocol Secure,缩写:HTTPS,也被称为HTTP over TLS,HTTP over SSL或HTTP Secure)是一种网络安全传输协议。在计算机网络上,HTTPS经由超文本传输协议进行通信,但利用SSL/TLS来对数据包进行加密。HTTPS开发的主要目的,是提供对网络服务器的身份认证,保护交换数据的隐私与完整性。(来源:维基百科)

https证书的获取

由于可信的证书颁发机构只有那么几家,所以必须要从他们那里获取或者购买。我的https证书是从腾讯云那里免费获取的,毕竟刚毕业没钱(网址:https://console.qcloud.com/ssl)。通过之后下载下来就可以了。

nginx上启用https

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {                                                                      
listen 443 ssl;
server_name www.****.com;#域名
ssl_certificate ssl_certs/1_www.****.com_cert.crt;#证书路径
ssl_certificate_key ssl_certs/2_www.****.com.key;#key路径

ssl_session_cache shared:SSL:1m; #s储存SSL会话的缓存类型和大小
ssl_session_timeout 5m; #会话过期时间
ssl_ciphers HIGH:!aNULL:!MD5; #为建立安全连接,服务器所允许的密码格式列表
ssl_prefer_server_ciphers on; #依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码

location / {
root /var/www/;
index index.html index.htm index.php;
}

location ~ \.php$ {
root /var/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

http访问自动跳转到https

1
2
3
4
server{                                                     
listen 80 www.****.com;
rewrite ^/(.*)$ https://www.****.com/$1 permanent;
}