mirror of
https://github.com/Baim0/baim0-learn.git
synced 2026-10-20 02:17:44 +08:00
206 lines
6.9 KiB
Markdown
206 lines
6.9 KiB
Markdown
## 9.HTTPS是大势所趋,必须学会如何配置
|
|||
|
|
|
||
|
|
# \[什么是HTTPS\]
|
||
|
|
|
||
|
|
HTTP咱们已经熟悉了, 一个网址例如这样子 [http://abc.com/](http://abc.com/)
|
||
|
|
|
||
|
|
传统的HTTP站点有一个很大安全隐患,就是所有和用户传输的信息 都是明文 未经过加密 这样子很容易就被截取到重要的信息
|
||
|
|
|
||
|
|
所以 HTTPS应运而生
|
||
|
|
|
||
|
|
#那么什么又是HTTPS呢?
|
||
|
|
|
||
|
|
简单的说 HTTPS就是安全版的HTTP啦, HTTPS是由 HTTP + SSL , 也就是在HTTP上又加了一层处理加密信息的模块 ,所以传输的数据都是加密后的数据
|
||
|
|
|
||
|
|
https协议原理又是怎么回事呢?
|
||
|
|
|
||
|
|
首先,客户端与服务器建立连接,各自生成不同的私钥和公钥。
|
||
|
|
|
||
|
|
服务器返给客户端一个公钥,然后客户端拿着这个公钥把要搜索的东西加密,称之为密文,并连并自己的公钥一起返回给服务器,服务器拿着自己的私钥解密密文,然后把响应到的数据用客户端的公钥加密,返回给客户端,客户端拿着自己的私钥解密密文,把数据呈现出来
|
||
|
|
|
||
|
|
当然了, 具体的加密算法比较复杂 除非是专门搞安全加密的 不然 不需要太细致去了解加密的具体流程
|
||
|
|
|
||
|
|
我们只要知道,HTTPS为了让传送的数据加密,就必须依赖一种可以把数据加密解密 并且还可以保证 不同的用户使用不同的计算方法 ,保证每个用户的安全性 即可
|
||
|
|
|
||
|
|
但是这里有一个问题了
|
||
|
|
|
||
|
|
就算一个网站保证了 跟你之间的传输可以走 加密的模式, 但是 如果这个网站自身就是个钓鱼网站呢? 提供了加密解密又有什么意义,你的个人信息不还是一样 泄露给它们了?
|
||
|
|
|
||
|
|
所以 这就有了一个信任关系的问题了,网站可以提供加密解密,并且还得是一个值得信任的网站 才可以哦
|
||
|
|
|
||
|
|
这就有了另外的一个概念 叫做证书
|
||
|
|
|
||
|
|
证书用于验证一个网站的合法性,通常只能由第三方机构来颁发
|
||
|
|
|
||
|
|
可是,这个“第三方机构”到底是在哪呢?是一个远端服务?不可能吧?如果是个远端服务,整个交互都会慢了。所以,这个第三方机构的验证功能只能放在客户端的本地了。
|
||
|
|
|
||
|
|
当然了,我们在学习的过程中 ,没必要真的去购买一个证书,证书也是可以自签的哦
|
||
|
|
|
||
|
|
# \[重新编译我们的Nginx ,让它支持SSL\]
|
||
|
|
|
||
|
|
咱们之前 一直没讲的nginx自定义安装参数,都是采用默认的
|
||
|
|
|
||
|
|
到这里 我们就得返回头看一下这里了
|
||
|
|
|
||
|
|
首先,我们接着之前课程的nginx配置文件,做出如下这一点改动
|
||
|
|
|
||
|
|
```plain
|
||
|
|
|
||
|
|
location /mynginx/ {
|
||
|
|
proxy_pass https://nginx.org/;
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
其实就是把 [http://nginx.org/](http://nginx.org/) 改成 [https://nginx.org/](https://nginx.org/)
|
||
|
|
|
||
|
|
然后尝试reload一下?
|
||
|
|
|
||
|
|
```plain
|
||
|
|
|
||
|
|
[root@server01 nginx-1.14.1]# /usr/local/nginx/sbin/nginx -s reload
|
||
|
|
nginx: [emerg] https protocol requires SSL support in /usr/local/nginx/conf/nginx.conf:65
|
||
|
|
```
|
||
|
|
|
||
|
|
看到没有?
|
||
|
|
|
||
|
|
咱们都还没开始自己搭建https, 只不过是反向代理 指向到一个 https的URL, nginx就直接报错了
|
||
|
|
|
||
|
|
为啥呢?
|
||
|
|
|
||
|
|
因为咱们的nginx在安装的时候,就没有调用支持SSL的模块哦
|
||
|
|
|
||
|
|
接下来 ,我们得重新编译nginx了, 让它支持SSL才可以
|
||
|
|
|
||
|
|
先把 nginx.conf备份一下,然后跟着下面的步骤走
|
||
|
|
|
||
|
|
查看一下 当前的nginx有没有调用什么编译参数, 如果有的话 就一定都要带上 不然会出问题
|
||
|
|
(这个在工作中非常重要, 千万不要狗熊掰棒子,加入新参数,忘了原有的旧参数)
|
||
|
|
|
||
|
|
```coffeescript
|
||
|
|
[root@server01 nginx-1.14.1]# /usr/local/nginx/sbin/nginx -V
|
||
|
|
nginx version: nginx/1.14.1
|
||
|
|
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
|
||
|
|
configure arguments:
|
||
|
|
```
|
||
|
|
|
||
|
|
好在这里咱们之前 没有加任何编译参数, 所以 我们只需要加入当前需要的就可以了
|
||
|
|
|
||
|
|
```javascript
|
||
|
|
./configure --prefix=/usr/local/nginx --with-http_ssl_module
|
||
|
|
```
|
||
|
|
|
||
|
|
指定安装路径 , 指定启用SSL模块
|
||
|
|
然后,make ; make install 即可
|
||
|
|
|
||
|
|
(请再三确认 pcre & pcre-devel 已经安装在你的linux了哦, 不然报错)
|
||
|
|
|
||
|
|
接下来 咱们试试reload
|
||
|
|
|
||
|
|
```coffeescript
|
||
|
|
[root@server01 nginx]# /usr/local/nginx/sbin/nginx -s reload
|
||
|
|
[root@server01 nginx]#
|
||
|
|
```
|
||
|
|
|
||
|
|
访问也没问题了
|
||
|
|
|
||
|
|
```makefile
|
||
|
|
[root@server01 nginx]# curl -I http://dami01.example.com/mynginx/
|
||
|
|
HTTP/1.1 200 OK
|
||
|
|
Server: nginx/1.14.1
|
||
|
|
Date: Thu, 31 Jan 2019 15:28:03 GMT
|
||
|
|
```
|
||
|
|
|
||
|
|
# \[接下来的任务 就是咱们自己创建HTTPS站点了哦\]
|
||
|
|
|
||
|
|
按照如下的步骤, 先生成密钥,再生成CSR, 最终生成证书 即可
|
||
|
|
|
||
|
|
注意 证书生成的位置,建议放在 /usr/local/nginx/conf/ssl 下面哦
|
||
|
|
|
||
|
|
```perl
|
||
|
|
[root@server01 nginx]# pwd
|
||
|
|
/usr/local/nginx
|
||
|
|
|
||
|
|
[root@server01 nginx]# mkdir conf/ssl ; cd conf/ssl
|
||
|
|
```
|
||
|
|
|
||
|
|
生成CSR文件
|
||
|
|
|
||
|
|
`openssl req -new -key server.key -out server.csr`
|
||
|
|
|
||
|
|
创建服务器证书的申请文件 server.csr
|
||
|
|
|
||
|
|
```php
|
||
|
|
[root@server01 ssl]# openssl req -new -key server.key -out server.csr
|
||
|
|
Enter pass phrase for server.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) []:BJ
|
||
|
|
Locality Name (eg, city) [Default City]:BJ
|
||
|
|
Organization Name (eg, company) [Default Company Ltd]:COMPANY
|
||
|
|
Organizational Unit Name (eg, section) []:
|
||
|
|
Common Name (eg, your name or your server's hostname) []:dami01.example.com
|
||
|
|
Email Address []:admin@admin.com
|
||
|
|
|
||
|
|
Please enter the following 'extra' attributes
|
||
|
|
to be sent with your certificate request
|
||
|
|
A challenge password []:
|
||
|
|
An optional company name []:
|
||
|
|
|
||
|
|
cp server.key server.key.org
|
||
|
|
```
|
||
|
|
|
||
|
|
生成证书文件server.crt
|
||
|
|
|
||
|
|
```css
|
||
|
|
|
||
|
|
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
|
||
|
|
```
|
||
|
|
|
||
|
|
到此为止, 我们的证书生成完毕
|
||
|
|
|
||
|
|
接下来,我们还需要改动nginx的配置文件 让我们的HTTPS可以正常运行起来
|
||
|
|
|
||
|
|
按照如下这样 , 我们新起一个server, 让它工作在默认的443端口(HTTPS默认端口)
|
||
|
|
之后,再指定证书和密钥的位置,指定我们在生成证书中的hostname(server\_name)
|
||
|
|
|
||
|
|
```plain
|
||
|
|
server {
|
||
|
|
|
||
|
|
listen 443;
|
||
|
|
ssl on;
|
||
|
|
|
||
|
|
ssl_certificate ssl/server.crt;
|
||
|
|
ssl_certificate_key ssl/server.key;
|
||
|
|
|
||
|
|
server_name dami01.example.com;
|
||
|
|
|
||
|
|
location / {
|
||
|
|
return 209;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
重启nginx后 我们来测试一下
|
||
|
|
|
||
|
|
```makefile
|
||
|
|
[root@server01 nginx]# netstat -tnlp | grep 443
|
||
|
|
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 40603/nginx
|
||
|
|
|
||
|
|
[root@server01 nginx]# curl -k -I https://dami01.example.com/
|
||
|
|
HTTP/1.1 209
|
||
|
|
Server: nginx/1.14.1
|
||
|
|
Date: Thu, 31 Jan 2019 16:13:21 GMT
|
||
|
|
Content-Type: application/octet-stream
|
||
|
|
Content-Length: 0
|
||
|
|
Connection: keep-alive
|
||
|
|
```
|
||
|
|
|
||
|
|
这样, 我们的HTTPS站点 就建立成功啦 ^\_^
|
||
|
|
|