HTTPS证书

HTTPS证书基本介绍

为什么需要使用HTTPS,因为HTTP不安全。当我们使用http网站时,经常会遇到包遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报告信息。https主要解决了什么问题,避免网站传输时信息泄露,避免网站传输时内容不被劫持和篡改。

HTTPS证书购买选择

保护1 个域名www
保护5 个域名www images cdn test m
通配符域名 *.test.com

HTTPS注意事项

Https 不支持续费,证书到期需重新申请新并进行替换.
Https 如果是通配符域名,二级域名和三级域名需要分别购买,如test.m.oldboy.com
Https 显示绿色, 说明整个网站的URL 都是https 的。
Https 显示黄色, 因为网站代码中包含http的不安全连接。
Https 显示红色, 要么证书是假的,要么证书过期

Nginx单台实现HTTPS

1.准备环境

#nginx 必须有ssl 模块
[root@web01 ~]# nginx -V
--with-http_ssl_module
#创建存放ssl 证书的路径
[root@web01 ~]# cd /etc/nginx/ssl_key
[root@web01 /etc/nginx/ssl_key]#
[root@web01 ~]# yum -y install openssl

2.使用openssl命令充当CA权威机构创建证书,生产不使用此方式生成证书,因为不会被互联网认可

[root@web01 ~]# 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:1234546
Verifying - Enter pass phrase for server.key:123456
[root@web01 ~]# 

3.生成自签证书,同时去掉私钥的密码

[root@web01 ~]# 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) []:SZ
Locality Name (eg, city) [Default City]:SZ
Organization Name (eg, company) [Default Company Ltd]:eg
Organizational Unit Name (eg, section) []:SA
Common Name (eg, your name or your server's hostname) []:test
Email Address []:
[root@web01 ~]# 

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

4.证书申请完成后需要了解Nginx如何配置Https

#是否开始ssl 支持
Syntax: ssl on | off;
Default: ssl off;
Context: http, server

#ssl crt 文件存放位置
Syntax: ssl_certificate file;
Default: —
Context: http, server

#ssl key 文件存放位置
Syntax: ssl_certificate_key file;
Default: —
Context: http, server

5.配置Nginx配置Https实例

[root@web01 ~]# mkdir /etc/nginx/ssl_key
[root@web01 ~]# mv server.* /etc/nginx/ssl_key/
[root@web01 ~]# vim /etc/nginx/conf.d/www.conf 
server {
    listen  443 ssl;
    server_name  www.test.com;
    ssl_certificate ssl_key/server.crt;
    ssl_certificate_key ssl_key/server.key;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
[root@web01 ~]# systemctl restart nginx
[root@web01 ~]# echo "https_test" >/usr/share/nginx/html/index.html

6.浏览器输入https://www.test.com访问,由于该证书非第三方权威机构颁发,而是我们自己签发的,所以浏览器会警告

7.以上配置如果用户忘记在浏览器地址栏输入https://那么将不会跳转至https,建议配置将用户访问http请求强制跳转https

[root@web01 ~]# vim /etc/nginx/conf.d/www.conf 
server {
   listen 80;
   server_name www.test.com;
   rewrite ^(.*) https://www.test.com/$1 permanent;
}
server {
    listen  443 ssl;
    server_name  www.test.com;
    ssl_certificate ssl_key/server.crt;
    ssl_certificate_key ssl_key/server.key;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

Nginx集群实现HTTPS实践

实战Nginx负载均衡+ Nginx WEB配置HTTPS安全

1.环境准备

主机名 外网IP(NAT) 内网IP(LAN) 角色
LB01 为eth0:10.0.0.5 eth1的:172.16.1.5 nginx的代理
web01 为eth0:10.0.0.7 eth1的:172.16.1.7 nginx的-web01
web02 为eth0:10.0.0.8 eth1的:172.16.1.8 nginx的-web02
web03 为eth0:10.0.0.9 eth1的:172.16.1.9 nginx的-web03

2.配置后端三台web配置相同

[root@web01 ~]# vim /etc/nginx/conf.d/www.conf 
server {
    listen  80;
    server_name  www.test.com;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
#web01
[root@web01 ~]# echo "172.16.1.7 https_test" >/usr/share/nginx/html/index.html
#web02
[root@web02 ~]# echo "172.16.1.8 https_test" >/usr/share/nginx/html/index.html
#web03
[root@web03 ~]# echo "172.16.1.9 https_test" >/usr/share/nginx/html/index.html


3.在代理服务器生成证书(我这里就使用上面web01生成的证书)

[root@web01 ~]# rsync -azvP /etc/nginx/ssl_key 172.16.1.5:/etc/nginx/


4.Nginx负载均衡配置文件如下

[root@lb01 nginx]# vim fastcgi_proxy
proxy_set_header Host $http_host;
proxy_set_header Host $http_host;  
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;

[root@lb01 nginx]# vim /etc/nginx/conf.d/lb01.conf 
upstream web {
    server 10.0.1.7:80;
    server 10.0.1.8:80;
    server 10.0.1.9:80;
}
include fastcgi_proxy;
server {
   listen 80;
   server_name www.test.com;
   rewrite ^(.*) https://www.test.com/$1 permanent;
}
server {
  listen 443 ssl;
  server_name www.test.com;
  ssl_certificate ssl_key/server.crt;
  ssl_certificate_key ssl_key/server.key;
  location / {
  proxy_pass http://web;
  }
}


5.进行访问测试

有些动态的网站要支持HTTPS还需要在nginx的配置文件里添加PHP-FastCGI的解析的参数

location ~ \.php$ {
    ...
    fastcgi_param  HTTPS on;
    ...
}

posted @ 2019-10-09 22:02  helloord  阅读(723)  评论(0编辑  收藏  举报