首页 > 技术文章 > Https实践

gshelldon 2020-07-17 21:23 原文

https实践

常用端口

ssh			22
telnet		23
ftp			21
rsync		873
http		80
mysql		3306
redis		6379
https		443
dns			53
php			9000
tomcat		8080

https介绍

  1. 什么是https?

    http是超文本传输协议。(不安全)

    https是加密的传输协议。(数据加密传输更安全,不加密网站容易被篡改)

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

  2. 如何使用https,配置

证书类型

  1. 单域名
  2. 多域名
  3. 通配域名

注意事项

  1. https证书不支持续费,到期之后需要重新申请。
  2. https不支持三级域名,如:test.gong.xxx.com

http三种颜色

  • 红色:网页中有不安全的链接http
  • 黄色:代码中包含http不安全的连接
  • 绿色:安全的链接

证书类型介绍

对比 域名型 DV 企业型 OV 增强型 EV
绿色地址栏 img小锁标记+https img小锁标记+https img小锁标记+企业名称+https
一般用途 个人站点和应用; 简单的https加密需求 电子商务站点和应用; 中小型企业站点 大型金融平台; 大型企业和政府机构站点
审核内容 域名所有权验证 全面的企业身份验证; 域名所有权验证 最高等级的企业身份验证; 域名所有权验证
颁发时长 10分钟-24小时 3-5个工作日 5-7个工作日
单次申请年限 1年 1-2年 1-2年
赔付保障金 —— 125-175万美金 150-175万美金

单台实现https

https的实现需要有ngx_http_ssl_module的支持,安装方法

This module is not built by default, it should be enabled with the --with-http_ssl_module configuration

# 使用nginx -V 可查看是否安装了这个模块。

语法格式.官方

    server {
    	# 开启443端口
        listen              443 ssl;
		
		# 证书的位置
        ssl_certificate     /usr/local/nginx/conf/cert.pem;
        ssl_certificate_key /usr/local/nginx/conf/cert.key;
    }

配置

# 1、创建放置证书的目录
[root@web01 ~]# mkdir /etc/nginx/ssl

# 使用openssl命令充当CA权威机构创建证书(生产不使用此方式生成证书,不被互联网认可的黑户证书)
[root@web01 /etc/nginx/ssl]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus
........................................................................+++
......................+++
e is 65537 (0x10001)
# 输入一个密码最小4位,只是配置过程中用
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:

[root@web01 /etc/nginx/ssl]# 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]:CN
State or Province Name (full name) []:chongqing
Locality Name (eg, city) [Default City]:dazu
Organization Name (eg, company) [Default Company Ltd]:better
Organizational Unit Name (eg, section) []:yunxingweihubu
Common Name (eg, your name or your server's hostname) []:gong.com
Email Address []:123@qq.com
# 这几步根据提示的做就好,在域名那里,老师说的是,要和绑定的域名的顶级域相同,实际在配置的过程中没有使用这个域名也能够成功访问

# req  --> 用于创建新的证书
# new  --> 表示创建的是新证书    
# x509 --> 表示定义证书的格式为标准格式
# key  --> 表示调用的私钥文件信息
# out  --> 表示输出证书文件信息
# days --> 表示证书的有效期

[root@web01 /etc/nginx/conf.d]# vi https.conf
server {
		# 监听443端口
        listen 443 ssl;
        server_name www.shelldon.com;
		# 防止证书和公钥的位置
        ssl_certificate   ssl/server.crt;
        ssl_certificate_key  ssl/server.key;

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

server {
        listen 80;
        server_name www.shelldon.com;
        # 用户访问http的时候强制跳转443
        return 302 https://$server_name$request_uri;
}

[root@web01 ~]# mkdir /website/https
[root@web01 ~]# echo https website > /website/https/index.html

多台实现https

负载均衡上的配置,在实际过程中,只需要在负载均衡上配置https就可以了,内网通信使用http。

[root@lb01 ~]# vi /etc/nginx/conf.d/upstream.conf
upstream blog {
        server 172.16.1.7;
        server 172.16.1.8 down;
        server 172.16.1.9 down;
}

# 80做的都是http强转https
server {
        listen 80;
        server_name wp.gong.com;
        return 302 https://$server_name$request_uri;
}

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

server {
        listen 443 ssl;
        server_name wp.gong.com;
        ssl_certificate   ssl/server.crt;
        ssl_certificate_key  ssl/server.key;
        location / {
                proxy_pass http://blog;
                include proxy_params;
        }
}

server {
        listen 443 ssl;
        server_name zh.gong.com;
        ssl_certificate   ssl/server.crt;
        ssl_certificate_key  ssl/server.key;
        location / {
                proxy_pass http://blog;
                include proxy_params;
        }
}

web中的配置

[root@web01 ~]# vi /etc/nginx/conf.d/wp.conf
server {
        listen 80;
        server_name wp.gong.com;
        root /website/wp;
        index index.php;

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
			   # 在这里要告诉php,负载均衡那里已经使用80跳转443了,使用https访问
			   fastcgi_param HTTPS on;
                include fastcgi_params;
        }
}

[root@web01 /etc/nginx/conf.d]# vi zh.conf 
server {
        listen 80;
        server_name zh.gong.com;
        root /website/zh;
        index index.php;

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
			   
			   fastcgi_param HTTPS on;
                include fastcgi_params;
        }
}

ssl优化参数

https使用在负载均衡中。

server {
    listen 443 ssl;
    server_name blog.driverzeng.com;
    root /var/www/wordpress;
    index index.php;
    # 指定证书的路径
    ssl_certificate   ssl/215089466160853.pem;
    ssl_certificate_key  ssl/215089466160853.key;
    
    include ssl_params;
    }
  #在建立完ssl握手后如果断开连接,在session_timeout时间内再次连接,是不需要再次获取公钥建立握手的,可以使用之前的连接
 ssl_session_cache shared:SSL:10m;
 #ssl连接断开后的超时时间
 ssl_session_timeout 1440m;  
  #配置加密套接协议
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; 
  #使用TLS版本协议
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
  #nginx决定使用哪些协议与浏览器通信
 ssl_prefer_server_ciphers on; 
    
    
[root@lb01 /etc/nginx]# vi ssl_params
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1440m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

推荐阅读