基于nginx结合openssl实现https

在未使用SSL证书对服务器数据进行加密认证的情况下,用户的数据将会以明文的形式进行传输,这样一来使用抓包工具是可以获取到用户密码信息的,非常危险。而且也无法验证数据一致性和完整性,不能确保数据在传输过程中没被改变。所以网站如果有涉及用户账户等重要信息的情况下通常要配置使用SSL证书,实现https协议。

在生产环境中的SSL证书都需要通过第三方认证机构购买,分为专业版OV证书(浏览器地址栏上不显示企业名称)和高级版EV(可以显示企业名称)证书,证书所保护的域名数不同也会影响价格(比如只对www认证和通配*认证,价格是不一样的),且不支持三级域名。测试中可以自己作为证书颁发机构来制作证书,浏览器会显示为红色,代表证书过期或者无效,如果是黄色的话代表网站有部分连接使用的仍然是http协议。

不管使用哪种方法,在拿到证书后对Nginx的配置都是一样的,所以这里以搭建OpenSSL并制作证书来进行完整说明:

[root@localhost ~]# openssl genrsa -out local.key 2048      /生成CA私钥    

Generating RSA private key, 2048 bit long modulus
................................................+++
...+++
e is 65537 (0x10001)

[root@localhost ~]# openssl req -new -key local.key -out local.csr     /生成CA证书请求

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) []:BJ
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:handian
Organizational Unit Name (eg, section) []:test
Common Name (eg, your name or your server's hostname) []:test
Email Address []:test@test.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:tian
An optional company name []:tian

root@localhost ~]# openssl x509 -req -in local.csr -extensions v3_ca -signkey local.key -out local.crt      /生成CA根证书
-in:用之前的申请文件作为输入
3
-x509:证书格式
4
-key:私钥文件
5
-out:产出的证书文件
6
-days:证书有效期

Signature ok
subject=/C=CN/ST=BJ/L=beijing/O=handian/OU=test/CN=test/emailAddress=test@test.com
Getting Private key

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf     /修改配置文件
server {
listen 443 ssl;
server_name www.test.com;

ssl_certificate /root/local.crt;      /指定数字证书文件
ssl_certificate_key /root/local.key;   /指定数字证书私钥文件

ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root html;
index index.html index.htm;
}
}

  

 效果图

原文地址:https://www.cnblogs.com/CAPF/p/11529440.html