https内容详解

HTTPS安全证书基本概述

1.为什么需要使用HTTPS?

因为HTTP不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露

那么我们在实现https时,需要了解ssl协议,但我们现在使用的更多的是TLS加密协议。
那么TLS是怎么保证明文消息被加密的呢?在OSI七层模型中,应用层是http协议,那么在应用层协议之下,我们的表示层,是ssl协议所发挥作用的一层,他通过(握手、交换秘钥、告警、加密)等方式,是应用层http协议没有感知的情况下做到了数据的安全加密

2.HTTPS证书下发流程
我们首先需要申请证书,先去登记机构进行身份登记,我是谁,我是干嘛的,我想做什么,然后登记机构再通过CSR发给CA,CA中心通过后会生成一堆公钥和私钥,公钥会在CA证书链中保存,公钥和私钥证书我们拿到后,会将其部署在WEB服务器上
1.当浏览器访问我们的https站点时,他会去请求我们的证书
2.Nginx这样的web服务器会将我们的公钥证书发给浏览器
3.浏览器会去验证我们的证书是否合法有效
4.CA机构会将过期的证书放置在CRL服务器,CRL服务的验证效率是非常差的,所以CA又推出了OCSP响应程序,OCSP响应程序可以查询指定的一个证书是否过期,所以浏览器可以直接查询OSCP响应程序,但OSCP响应程序性能还不是很高,所以HTTP比https效率高一些
5.Nginx会有一个OCSP的开关,当我们开启后,Nginx会主动上OCSP上查询,这样大量的客户端直接从Nginx获取证书是否有效

3.证书类型介绍

https内容详解_第1张图片
1)购买证书选择

1.保护一个域名   www.mumusir.com
2.保护多个域名   www.  test.   cdn.  image.   class.
3.保护通配符域名  *.mumusir.com

2)HTTPS证书注意事项

1.https不支持续费,证书到期需要重新申请并进行替换 
2.https不支持三级域名解析,如 test.m.haoda.com 
拓展:一级域名:baidu.com
     二级域名:baidu.123.com
     三级域名:test.z.qwer.com
3.https显示绿色,说明整个网站的url都是https的
	https显示黄色,因为网站代码中包含http的不安全链接
	https显示红色,那么证书是假的或者证书过期。

4.配置单台机器HTTPS证书
1)检查nginx能否使用证书

[root@web01 ~]# nginx -V
--with-http_ssl_module

2)创建存放证书目录

[root@web01 ~]# mkdir /etc/nginx/ssl_key
[root@web01 ~]# cd /etc/nginx/ssl_key/

3)生成证书

[root@web01 ssl_key]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus
..................................................................................................................................+++
...............................................................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:123456 #4-1023位
Verifying - Enter pass phrase for server.key:123456 #确认密码


[root@web01 ssl_key]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
Generating a 2048 bit RSA private key
...................................................+++
.................+++
writing new private key to '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]:China
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:zg    #国家
State or Province Name (full name) []:riben   #省份
Locality Name (eg, city) [Default City]:bali    #城市
Organization Name (eg, company) [Default Company Ltd]:oldboy  #公司
Organizational Unit Name (eg, section) []:oldboy   #组织别名
Common Name (eg, your name or your server's hostname) []:maliao #服务名
Email Address []:[email protected]   #邮箱
You have new mail in /var/spool/mail/root
[root@web01 ssl_key]# 

openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
# req  --> 用于创建新的证书
# new  --> 表示创建的是新证书    
# x509 --> 表示定义证书的格式为标准格式
# key  --> 表示调用的私钥文件信息
# out  --> 表示输出证书文件信息
# days --> 表示证书的有效期

[root@web01 ssl_key]# ll
total 8
-rw-r--r-- 1 root root 1375 Mar  5 15:15 server.crt
-rw-r--r-- 1 root root 1704 Mar  5 15:15 server.key

4)nginx证书配置语法

#启动ssl功能
Syntax:	ssl on | off;
Default:	ssl off;
Context:	http, server

#证书文件
Syntax:	ssl_certificate file;
Default:	—
Context:	http, server

#私钥文件
Syntax:	ssl_certificate_key file;
Default:	—
Context:	http, server

5)配置nginx证书

#配置nginx
[root@web01 ssl_key]# vim /etc/nginx/conf.d/s.linux.com.conf
server {
     
    listen 443 ssl;
    server_name s.linux.com;
    ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
     
        root /code/https;
        index index.html;
    }
}

#重启nginx
[root@web01 ssl_key]# systemctl restart nginx

#配置站点
[root@web01 ssl_key]# echo "test https" > /code/https/index.html

6)配置hosts访问测试

10.0.0.7 discuz.linux.com jc.linux.com s.linux.com

4.全站https

https内容详解_第2张图片2.配置web服务器(两台)

[root@web01 conf.d]# vim s.linux.com.conf 
server {
     
    listen 80;
    server_name s.linux.com;

    location / {
     
        root /code/https;
        index index.html;
    }
}
[root@web01 conf.d]# systemctl restart nginx

#同步配置文件
[root@web01 conf.d]# scp s.linux.com.conf 172.16.1.8:/etc/nginx/conf.d/

#配置站点目录文件
[root@web01 conf.d]# mkdir /code/https
[root@web01 conf.d]# echo "https1111" > /code/https/index.html
[root@web02 conf.d]# mkdir /code/https
[root@web02 conf.d]# echo "https2222" > /code/https/index.html
[root@web01 conf.d]# chown -R www.www /code/https/
[root@web02 conf.d]# chown -R www.www /code/https/

3.推送、上传证书文件

[root@web01 conf.d]# scp -r /etc/nginx/ssl_key 172.16.1.4:/etc/nginx/

4.配置负载均衡机器nginx

[root@lb01 conf.d]# vim s.linux.com.conf
upstream webserver {
     
    server 172.16.1.7:80;
    server 172.16.1.8:80;
}

server {
     
    listen 443 ssl;
    server_name s.linux.com;
    ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
     
        proxy_pass http://webserver;
        proxy_set_header host $http_host;
    }
}

server {
     
    listen 80;
    server_name s.linux.com;
    return 302 https://$server_name$request_uri;
}

5.配置hosts,访问测试

5.项目全站https

1.配置web端博客nginx配置文件

[root@web01 conf.d]# vim blog.linux.com.conf 
server {
     
    listen 80;
    server_name blog.linux.com;

    location / {
     
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
     
        root /code/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

[root@web01 conf.d]# scp blog.linux.com.conf 172.16.1.8:/etc/nginx/conf.d/

2.配置web端知乎的配置文件

[root@web01 conf.d]# vim zh.linux.com.conf 
server {
     
    listen 80;
    server_name zh.linux.com;

    location / {
     
        root /code/wecenter;
        index index.php;
    }

    location ~* \.php$ {
     
        root /code/wecenter;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

[root@web01 conf.d]# scp zh.linux.com.conf 172.16.1.8:/etc/nginx/conf.d/

3.配置负载均衡

[root@lb01 conf.d]# vim proxy_https.conf
upstream web {
     
    server 172.16.1.7:80;
    server 172.16.1.8:80;
}

server {
     
    listen 443 ssl;
    server_name blog.linux.com;
    ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
     
        proxy_pass http://web;
        include proxy_params;
    }
}

server {
     
    listen 80;
    server_name blog.linux.com;
    return 302 https://$server_name$request_uri;
}

server {
     
    listen 443 ssl;
    server_name zh.linux.com;
 	ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
     
        proxy_pass http://web;
        include proxy_params;
    }
}

server {
     
    listen 80;
    server_name zh.linux.com;
    return 302 https://$server_name$request_uri;
}

[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl restart nginx

4.配置hosts访问测试

#页面格式混乱,代理到php的时候开启HTTPS模式
server {
     
	... ...

    location ~* \.php$ {
     
        root /code/wecenter;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #开启https模式
        fastcgi_param HTTPS on;
        include fastcgi_params;
    }
}

5.配置web端phpmyadmin

[root@web01 conf.d]# vim phpmyadmin.conf 
server {
     
    listen 80;
    server_name php.linux.com;

    location / {
     
        root /code/phpmyadmin;
        index index.php;
    }

    location ~ \.php$ {
     
        root /code/phpmyadmin;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

[root@web01 conf.d]# scp phpmyadmin.conf 172.16.1.8:/etc/nginx/conf.d/

6.配置负载均衡phpmyadmin

[root@lb01 conf.d]# vim phpmyadmin_proxy.conf 
upstream phpmyadmin {
     
    server 10.0.0.7;
    server 10.0.0.8;
}

server {
     
    listen 443 ssl;
    server_name php.linux.com;
    ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
     
        proxy_pass http://phpmyadmin;
        include proxy_params;
    }
}

server {
     
    listen 80;
    server_name php.linux.com;
    return 302 https://$server_name$request_uri;
}

[root@lb01 conf.d]# systemctl restart nginx

6.云服务器配置https

1.购买云主机
2.解析域名
3.申请域名对应的https证书
4.将https证书部署到服务器

你可能感兴趣的:(Linux架构篇)