当前位置首页 > Nginx知识

利用nginx搭建https服务器

阅读次数:241 次  来源:admin  发布时间:

一、HTTPS简介

HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据。

首先,客户端与服务器建立连接,各自生成私钥和公钥,是不同的。服务器返给客户端一个公钥,然后客户端拿着这个公钥把要搜索的东西加密,称之为密文,并连并自己的公钥一起返回给服务器,服务器拿着自己的私钥解密密文,然后把响应到的数据用客户端的公钥加密,返回给客户端,客户端拿着自己的私钥解密密文,把数据呈现出来

二、生成证书和私钥

说明:这个只是说明怎么创建秘钥,但是自己创建的秘钥不会受浏览器验证。如果需要搭建https服务器,那么需要去买秘钥。

① 创建存放的目录,并进入到目录,一般生成的目录,应该放在nginx/conf/ssl目录: cd /usr/local/nginx/conf/ssl/

② 创建服务器证书密钥文件 server.key: openssl genrsa -des3 -out server.key 1024

利用nginx搭建https服务器

注意记住输入的密码,后面需要用到。

③ 创建服务器证书的申请文件 server.csr

 1 [root@origalom ssl]# openssl req -new -key server.key -out server.csr
 2 Enter pass phrase for server.key:  ← 输入前面创建的密码
 3 You are about to be asked to enter information that will be incorporated
 4 into your certificate request.
 5 What you are about to enter is what is called a Distinguished Name or a DN.
 6 There are quite a few fields but you can leave some blank
 7 For some fields there will be a default value,
 8 If you enter '.', the field will be left blank.
 9 -----
10 Country Name (2 letter code) [XX]:CN  ← 国家代号,中国输入CN 
11 State or Province Name (full name) []:BeiJing ← 省的全名,拼音 
12 Locality Name (eg, city) [Default City]:BeiJing ← 市的全名,拼音 
13 Organization Name (eg, company) [Default Company Ltd]:MyCompany Corp.   ← 公司英文名 
14 Organizational Unit Name (eg, section) []:  ← 可以不输入 
15 Common Name (eg, your name or your server's hostname) []:  ← 可以不输入 
16 Email Address []:1430156396@qq.com  ←  电子邮箱 
17 
18 Please enter the following 'extra' attributes
19 to be sent with your certificate request
20 A challenge password []:     ← 可以不输入 
21 An optional company name []:    ← 可以不输入 

④ 备份服务器秘钥文件: cp server.key server.key.bak

⑤ 去除文件口令: openssl rsa -in server.key.bak -out server.key

⑥ 生成证书文件server.crt: openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

三、配置nginx

① 配置nginx配置文件: vim /usr/local/nginx/conf/nginx.conf

② 在配置文件中添加https的配置,使nginx支持htt

 1 server {
 2         listen       443 ssl;
 3         server_name  www.origal.cn;     # 域名或者ip
 4 
 5         ssl_certificate      /usr/local/nginx/conf/ssl/214324938610703.pem;
 6         ssl_certificate_key  /usr/local/nginx/conf/ssl/214324938610703.key;
 7 
 8         ssl_session_cache    shared:SSL:1m;
 9         ssl_session_timeout  5m;
10 
11         ssl_ciphers  HIGH:!aNULL:!MD5;
12         ssl_prefer_server_ciphers  on;
13 
14         location / {
15             root   html;
16             index  index.html index.htm;
17         }
18     }

③ 然后使用https进行访问就可以访问了

上一篇:VirtualBox:ResizeaFedora,CentOS,orWindowsDynamicGuestVirtualDisk
下一篇:转启用IIS的Gzip压缩