https 协议
是一种通过计算机网络进行安全通信的传输协议. HTTPS 经由 HTTP 进行通信, 但利用 SSL/TLS 来加密数据包. HTTPS 开发的主要目的, 是提供对网站服务器的身份认证, 保护交换数据的隐私与完整性. 这个协议由网景公司 (Netscape) 在 1994 年首次提出, 随后扩展到互联网上.
简单来说, HTTPS 是 HTTP 的安全版, 是使用 SSL/TLS 加密的 HTTP 协议. 通过 TLS/SSL 协议的的身份验证, 信息加密和完整性校验的功能, 从而避免信息窃听, 信息篡改和信息劫持的风险.
HTTPS 提供了加密 (Encryption), 认证 (Verification), 鉴定 (Identification) 三种功能
私密性(Confidentiality/Privacy):
也就是提供信息加密, 保证数据传输的安全;
可信性(Authentication):
身份验证, 主要是服务器端的, 确认网站的真实性, 有些银行也会对客户端进行认证;
完整性(Message Integrity):
保证信息传输过程中的完整性, 防止被修改;
HTTPS 就是在应用层和传输层中间加了一道验证的门槛以保证数据安全
部署服务
准备环境
主机名 | IP 地址 | 作用 |
web03 | 10.0.0.9 | 网站服务 |
lb01 | 10.0.0.6 | 负载均衡 |
配置说明: http://nginx.org/en/docs/http/ngx_http_ssl_module.html
- server {
- listen 443 ssl;
ssl 算法协议
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl 算法方式 证书机构
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
* 指定公钥文件(证书)
ssl_certificate /etc/nginx/conf.d/cert.pem;
* 指定私钥文件
- ssl_certificate_key /etc/nginx/conf.d/cert.key;
- }
创建私钥文件
- [[email protected] ~]# openssl genrsa -idea -out /etc/nginx/conf.d/server.key 2048 #位数决定私钥的长度, 加 - idea 需要输入密码
- # 说明: 密钥文件也可以进行加密的, 并且支持后期手工加密, 但不建议加密, 每次使用密钥都需要解密, 比较麻烦
- [[email protected] ~]# chmod 600 /etc/nginx/conf.d/server.key
创建公钥文件(假证)
- [[email protected] /etc/nginx/conf.d]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
- # -out 生成证书
- 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) []:BJ
- Locality Name (eg, city) [Default City]:BJ
- Organization Name (eg, company) [Default Company Ltd]:edu
- Organizational Unit Name (eg, section) []:IT
- Common Name (eg, your name or your server's hostname) []:Web
- Email Address []:1354586***@qq.com
目前标准的证书存储格式是 x509 , 还有其他的证书格式, 需要包含的内容为:
公钥信息, 以及证书过期时间
证书的合法拥有信息
证书该如何被使用
CA 颁发机构信息
CA 签名的效验码
互联网上使用的 SSL 和 TLS 证书管理机构均使用 x509 的格式
阿里云 SSL 证书地址: https://common-buy.aliyun.com/?spm=5176.7968328.1266638..298b1232b1th4h&commodityCode=cas&aly_as=CG02jNzM#/buy
nginx 配置文件
1. nginx 源:
- VIM /etc/yum.repos.d/nginx.repo
- [nginx-stable]
- name=nginx stable repo
- baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
- gpgcheck=1
- enabled=1
- gpgkey=https://nginx.org/keys/nginx_signing.key
2. 重新配置完 yum 源信息, 建议清除一些 yum 缓存信息
- yum clean all
- yum makecache
3. 安装软件程序
yum install nginx -y
4. 编写程序配置文件
- cd /etc/nginx/conf.d
- mv default.conf default.conf.bak
- VIM xxx.conf
5. 编写扩展配置文件
- server {
- listen 443 ssl; #开启 ssl 功能
- server_name www.oldboy.com;
- ssl_certificate /etc/nginx/conf.d/server.crt; #公钥文件地址
- ssl_certificate_key /etc/nginx/conf.d/server.key; #私钥文件地址
- location / {
- root /HTML/www;
- index index.HTML index.htm;
- }
- }
nginx 主配置文件(默认)
- cat /etc/nginx/nginx.conf
- user nginx;
- worker_processes 1;
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- log_format main '$remote_addr - $remote_user [$time_local]"$request" '
- '$status $body_bytes_sent"$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/access.log main;
- sendfile on;
- #tcp_nopush on;
- keepalive_timeout 65;
- #gzip on;
- include /etc/nginx/conf.d/www.conf;
- }
确认 nginx 是否支持 hppts yum 安装默认支持
模块信息: --with-http_ssl_module
网站登录 #直接使用 ip 地址登录, 或者在 Windows 的 hosts 文件内进行解析
网站实现跳转功能
直接实现 HTTPs 跳转访问
情况一:
添加一下代码在扩展文件中
- server {
- listen 80;
- server_name www.oldboy.com;
- rewrite ^(.*)$ https://$host$1 permanent; #直接跳转到 https 上
- }
- cat /etc/nginx/confi.d/www.conf
- server {
- listen 80;
- server_name www.oldboy.com;
- rewrite ^(.*)$ https://$host$1 permanent;
- }
- server {
- listen 443 ssl;
- server_name www.oldboy.com;
- ssl_certificate /etc/nginx/conf.d/server.crt;
- ssl_certificate_key /etc/nginx/conf.d/server.key;
- location / {
- root /HTML/www;
- index index.HTML index.htm;
- }
- }
说明: 在 https 配置 server 基础上再添加 http 跳转 server
情况二:
- server {
- listen 443;
- listen 80;
- ssl on;
- server_name www.etiantian.org;
- ssl_certificate /application/nginx/conf/key/server.crt;
- ssl_certificate_key /application/nginx/conf/key/server.key;
- location / {
- root HTML/www;
- index index.HTML index.htm;
- }
- error_page 497 https://$host$uri;
- }
说明: 497 为内置错误码, 当访问 http 无法处理, 需要利用 https 处理时
负载均衡上实现 HTTPs 跳转访问
用户访问网站先访问负载均衡, 负载均衡跳转到 https 访问网站 443 端口, 在 Web 网站上面匹配证书
- # 将公钥和密钥传输到负载均衡上
- [[email protected] /HTML]# scp -rp /etc/nginx/conf.d/server.* 172.16.1.5:/etc/nginx/conf.d/
- [[email protected] ~]# VIM /etc/nginx/nginx.conf
- worker_processes 1;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- server {
- listen 80;
- server_name www.oldboy.com;
- rewrite ^(.*)$ https://$host$1 permanent;
- }
- upstream oldboy {
- server 10.0.0.7:443;
- }
- server {
- listen 443 ssl;
- server_name localhost;
- ssl_certificate /etc/nginx/conf.d/server.crt;
- ssl_certificate_key /etc/nginx/conf.d/server.key;
- location / {
- root HTML;
- index index.HTML index.htm;
- proxy_pass https://oldboy;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- proxy_next_upstream error timeout invalid_header http_403 http_502;
- }
- }
- }
后端 Web 网站配置, 跳转已经让负载均衡完成
- [[email protected] /]# VIM /etc/nginx/conf.d/www.conf
- server {
- listen 80;
- server_name www.oldboy.com;
- location / {
- root /HTML/www;
- index index.HTML index.htm;
- }
- }
用户访问网站先访问负载均衡, 负载均衡跳转到 https 访问网站 80 端口, 在 Web 网站上面不用匹配证书
来源: http://www.bubuko.com/infodetail-3330356.html