中秋假期,闲来无事。花了一下午折腾了下https,说实话这年头还有网站不上https显然是折腾精神不够啊~
1、SSL证书评估
看了市面上各种类型的证书,有收费的也有免费的,但是最终还是选择了腾讯云提供的TrustAsia一年免费期的证书,没有次数限制,可以过期后再次申请。最主要的原因还是我懒,哈哈~~
2、SSL证书安装
从腾讯云将申请的证书下载到本地,解压获得3个文件夹,分别是Apache、IIS、Nginx 服务器的证书文件,可以根据自己的实际情况,安装在三种服务器上。
我这里使用Nginx做前端转发。更多的介绍可以查看文档:https://cloud.tencent.com/document/product/400/4143
2.1 获取证书
Nginx文件夹内获得SSL证书文件 1_www.domain.com_bundle.crt 和私钥文件 2_www.domain.com.key,
1_www.domain.com_bundle.crt 文件包括两段证书代码 “-----BEGIN CERTIFICATE-----”和“-----END CERTIFICATE-----”,
2_www.domain.com.key 文件包括一段私钥代码“-----BEGIN RSA PRIVATE KEY-----”和“-----END RSA PRIVATE KEY-----”。
2.2 证书安装
将域名 www.domain.com 的证书文件1_www.domain.com_bundle.crt 、私钥文件2_www.domain.com.key保存到同一个目录,例如/usr/local/nginx/conf目录下。
更新Nginx根目录下 conf/nginx.conf 文件如下:
server { listen 443; server_name www.domain.com; #填写绑定证书的域名 ssl on; ssl_certificate 1_www.domain.com_bundle.crt; ssl_certificate_key 2_www.domain.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置 ssl_prefer_server_ciphers on; location / { root html; #站点目录 index index.html index.htm; } }
配置完成后,先用bin/nginx –t
来测试下配置是否有误,正确无误的话,重启nginx。就可以使用 https://www.domain.com
来访问了。
注:
配置文件参数 | 说明 |
---|---|
listen 443 | SSL访问端口号为443 |
ssl on | 启用SSL功能 |
ssl_certificate | 证书文件 |
ssl_certificate_key | 私钥文件 |
ssl_protocols | 使用的协议 |
ssl_ciphers | 配置加密套件,写法遵循openssl标准 |
2.3 使用全站加密,http自动跳转https(可选)
对于用户不知道网站可以进行https访问的情况下,让服务器自动把http的请求重定向到https。
在服务器这边的话配置的话,可以在页面里加js脚本,也可以在后端程序里写重定向,当然也可以在web服务器来实现跳转。Nginx是支持rewrite的(只要在编译的时候没有去掉pcre)
在http的server里增加rewrite ^(.*) https://$host$1 permanent;
这样就可以实现80进来的请求,重定向为https了。
3、证书安装的一系列问题。
3.1 端口号问题
需要注意的是,我们通常使用的80端口作为http默认访问的端口,而在https中使用的443端口,这个是需要特别注意的,我在安装过程中一开始就没有注意端口号的问题,导致https一直没有生效,后来问了公司的运维同学才知道,耽误了很长的时间。
3.2 http跳转https
一般我们的网站支持https之后,就不在支持http的访问了。这时候就需要把http的访问请求,重定向到https。实现的过程也很简单,新增一个server,然后加入以下配置。
server { listen 80; location / { rewrite (.*) https://www.laoyeye.net; } }
直接将80端口的访问,全部转发到网站的https域名,其实我这个写的还不是很规范,一般不会将域名写死,而是根据请求的域名做重定向的。
3.3 一级域名跳转二级域名的问题
一般我们的网站很多都想让www作为网站的首页,这是一个二级域名,而我们默认的一级域名,如laoyeye.net是不提供服务的,这个时候就需要将https访问这个域名的请求重定向到www域名,实现的过程也很简单。
server { listen 443; server_name laoyeye.net; #填写绑定证书的域名 ssl on; ssl_certificate 1_www.laoyeye.net_bundle.crt; ssl_certificate_key 2_www.laoyeye.net.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置 ssl_prefer_server_ciphers on; location / { rewrite (.*) http://www.laoyeye.net; } }
需要注意的是这里处理的是https://laoyeye.net链接,而http://laoyeye.net在上面端口80的监听中是已经做了处理的。
3.4 400 Bad Request | The plain HTTP request was sent to HTTPS port
完成上述的配置,基本上访问http://www.laoyeye.net/,还是http://laoyeye.net/,亦或是https://laoyeye.net/,均会跳转到https://www.laoyeye.net/。但是我发现个别的链接访问会出现400错误,搞了一下午才弄清问题所在。
原因主要是我在后端逻辑处理的时候使用了重定向,后端重定向后返回的是http链接,就会出现这个问题。
解决办法也很多,很多人会改tomcat的配置,让重定向后的链接直接是https形式的。其实我不怎么喜欢这种方式,因为这样做的侵入性太大。当换一个tomcat服务器后,可能你并不会想起这个配置。
既然我们是在nginx上配置的ssl,这里我们还是从nginx上想办法。
在location种加入如下配置
proxy_redirect http:// https://;
这句话的作用是对发送给客户端的URL进行修改, 将http协议强制转为https,完成这个配置后就不会出现400的问题了。
当然我还有一些个人的配置问题,比如QQ互联的回调地址啦,也是需要修改http为https才能正常使用。
附Nginx入门以及我的配置参考:
我的nginx配置供参考:
user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; upstream tomcat_server { server xxxx:xxxx; } #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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; location / { rewrite (.*) https://www.laoyeye.net; } } server { listen 443; server_name laoyeye.net; ssl on; ssl_certificate 1_www.laoyeye.net_bundle.crt; ssl_certificate_key 2_www.laoyeye.net.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; #charset koi8-r; #access_log logs/host.access.log main; location / { rewrite (.*) https://www.laoyeye.net; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } server { listen 443; server_name www.laoyeye.net; ssl on; ssl_certificate 1_www.laoyeye.net_bundle.crt; ssl_certificate_key 2_www.laoyeye.net.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / { #域名www.laoyeye.net的请求全部转发到tomcat_server即tomcat服务上 proxy_pass http://tomcat_server; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-PORT $remote_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 必须配置: proxy_set_header X-Forwarded-Proto $scheme; # 作用是对发送给客户端的URL进行修改, 将http协议强制转为https proxy_redirect http:// https://; index index.jsp index.html index.htm; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
原文出处:https://www.cnblogs.com/laoyeye/p/9694833.html
共同学习,写下你的评论
评论加载中...
作者其他优质文章