不积跬步,无以至千里;不积小流,无以成江海。

使用SSL证书,摆脱运营商流量劫持!

网站建设 康康 1979℃ 1评论

2015 年底,多家公司就联合声明:呼吁运营商严格打击流量劫持。但是,能够实施这一攻击行为的目前也就是各地的宽带运营商了,让贼捉贼怎么可能?!况且, 在某些地区,这种劫持文件并插入广告的方式已经成为他们轻松来钱的“优质”渠道了,怎能轻易放手!所以,从 15 年起,很多大型网站开启了 HTTPS ,包括 淘宝、百度等。

鉴于国内网络环境复杂,建议大家也尽快支持 HTTPS。尤其是国内云厂商基本都支持 Let’s encrypt 免费证书了,无论申请还是开启 Let’s encrypt 证书都很方便了。

关于流量劫持,常见的就是网页内容被插入各种广告!而这些网站本身源代码并没有植入广告,这种方式的流量劫持属于中间人攻击(Man-in-the-Middle Attack,MITM)的一种,其实质就是在数据通路上劫持文件并篡改(一般是加入广告代码),并将篡改后的文件 发送给客户端。在这种攻击下,源服务器上的文件是不受影响的,文件被篡改是发生在传输过程中,由于 HTTP 协议完全是明文传输,很容易被劫持、篡改,因此,只要文件在 加密通道中传输就能够避免被劫持、篡改。

一、获取 Let’s encrypt 免费证书

建议使用git工具,安装部署时需要Python2.7的支持,linux默认都装了python ,本案例在centos7下进行

如果没有以上依赖,请先安装:

  1. shell# yum install git #安装git
  2. shell# git clone https://github.com/letsencrypt/letsencrypt #获取源码

二、生成证书密钥

  1. shell# cd letsencrypt
  2. shell# ./letsencrypt-auto certonly --standalone --email 354867750@qq.com -d chenweikang.top -d www.chenweikang.top
  3.  
  4. #以下为交互日志
  5. Saving debug log to /var/log/letsencrypt/letsencrypt.log
  6. Plugins selected: Authenticator standalone, Installer None
  7.  
  8. -------------------------------------------------------------------------------
  9. Please read the Terms of Service at
  10. https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
  11. agree in order to register with the ACME server at
  12. https://acme-v01.api.letsencrypt.org/directory
  13. -------------------------------------------------------------------------------
  14. (A)gree/(C)ancel: A
  15.  
  16. -------------------------------------------------------------------------------
  17. Would you be willing to share your email address with the Electronic Frontier
  18. Foundation, a founding partner of the Let's Encrypt project and the non-profit
  19. organization that develops Certbot? We'd like to send you email about EFF and
  20. our work to encrypt the web, protect its users and defend digital rights.
  21. -------------------------------------------------------------------------------
  22. (Y)es/(N)o: Y
  23. Obtaining a new certificate
  24. Performing the following challenges:
  25. tls-sni-01 challenge for chenweikang.top
  26. tls-sni-01 challenge for www.chenweikang.top
  27. Waiting for verification...
  28. Cleaning up challenges
  29.  
  30. IMPORTANT NOTES:
  31. - Congratulations! Your certificate and chain have been saved at:
  32. /etc/letsencrypt/live/chenweikang.top/fullchain.pem
  33. Your key file has been saved at:
  34. /etc/letsencrypt/live/chenweikang.top/privkey.pem
  35. Your cert will expire on 2018-03-17. To obtain a new or tweaked
  36. version of this certificate in the future, simply run
  37. letsencrypt-auto again. To non-interactively renew *all* of your
  38. certificates, run "letsencrypt-auto renew"
  39. - Your account credentials have been saved in your Certbot
  40. configuration directory at /etc/letsencrypt. You should make a
  41. secure backup of this folder now. This configuration directory will
  42. also contain certificates and private keys obtained by Certbot so
  43. making regular backups of this folder is ideal.
  44. - If you like Certbot, please consider supporting our work by:
  45.  
  46. Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
  47. Donating to EFF: https://eff.org/donate-le

在 /etc/letsencrypt/live/chenweikang.top 目录下会生成4个文件,分别为Apache和Nginx用到的:

cert.pem  - Apache服务器端证书
chain.pem  - Apache根证书和中继证书
fullchain.pem  - Nginx所需要ssl_certificate文件
privkey.pem - 安全证书KEY文件

  1. [root@iz2zeeuc8ed3wflgmb7rakz chenweikang.top]# ll
  2. total 4
  3. lrwxrwxrwx 1 root root 39 Dec 17 17:13 cert.pem -> ../../archive/chenweikang.top/cert1.pem
  4. lrwxrwxrwx 1 root root 40 Dec 17 17:13 chain.pem -> ../../archive/chenweikang.top/chain1.pem
  5. lrwxrwxrwx 1 root root 44 Dec 17 17:13 fullchain.pem -> ../../archive/chenweikang.top/fullchain1.pem
  6. lrwxrwxrwx 1 root root 42 Dec 17 17:13 privkey.pem -> ../../archive/chenweikang.top/privkey1.pem
  7. -rw-r--r-- 1 root root 543 Dec 17 17:13 README

三、Nginx配置Https

根据自己网站配置,对nginx或apache进行配置,橘红色为我新增的,
关于http重定向到https有多种方式,这里使用rewrite重定向

  1. server {
  2. listen 80;
  3. server_name www.chenweikang.top chenweikang.top;
  4. root /home/wordpress/server;
  5. index index.html index.htm index.php;
  6. error_page 404 /ERROR/404.html;
  7. #访问http时跳重定向到https
  8. if (-f $request_filename/index.html){
  9. rewrite (.*) https://$host$1/index.html break;
  10. }
  11. if (-f $request_filename/index.php){
  12. rewrite (.*) https://$host$1/index.php;
  13. }
  14. if (!-f $request_filename){
  15. rewrite (.*) https://$host$1/index.php;
  16. }
  17. location ~ \.php$ {
  18. fastcgi_pass 127.0.0.1:9100;
  19. fastcgi_index index.php;
  20. include fastcgi.conf;
  21. }
  22. }
  23.  
  24. server {
  25. listen 443 ssl;
  26. ssl on;
  27. #指定pem格式的证书
  28. ssl_certificate /etc/letsencrypt/live/chenweikang.top/fullchain.pem;
  29. #指定私钥
  30. ssl_certificate_key /etc/letsencrypt/live/chenweikang.top/privkey.pem;
  31. server_name www.chenweikang.top chenweikang.top;
  32. root /home/wordpress/server;
  33. index index.html index.htm index.php;
  34. error_page 404 /ERROR/404.html;
  35. if (-f $request_filename/index.html){
  36. rewrite (.*) $1/index.html break;
  37. }
  38. if (-f $request_filename/index.php){
  39. rewrite (.*) $1/index.php;
  40. }
  41. if (!-f $request_filename){
  42. rewrite (.*) $1/index.php;
  43. }
  44. location ~ \.php$ {
  45. fastcgi_pass 127.0.0.1:9100;
  46. fastcgi_index index.php;
  47. include fastcgi.conf;
  48. }
  49. }

重新加载nginx,尝试使用http访问 ,若配置正确,会自动重定向到https

最后

Let's Encrypt 有效期为90天,我们可以创建脚本加入定时任务,自动生成密钥

编写脚本 vim /root/createSSL.sh

  1. [root@iz2zeeuc8ed3wflgmb7rakz letsencrypt]# vim createSSL.sh
  2. #!/bin/bash
  3.  
  4. logDir="/root/createSSL.log"
  5.  
  6. echo "-------------重新生成证书["`date`"]--------" >> $logDir
  7. echo "stop nginx : ok " >> $logDir
  8. #停止nginx
  9. systemctl stop nginx >> $logDir
  10.  
  11. echo "生成证书 : " >> $logDir
  12.  
  13. #重新生成证书
  14. /software/temp/letsencrypt/letsencrypt-auto certonly --renew-by-default --standalone --email 354867750@qq.com -d chenweikang.top -d www.chenweikang.top >> $logDir
  15.  
  16.  
  17. echo "start nginx : ok " >> $logDir
  18. #启动nginx
  19. systemctl start nginx >> $logDir
  20.  
  21. echo "--------------------------结束---------------------------" >> $logDir

别忘了权限:

chmod +x /root/createSSL.sh

创建定时任务 crontab -e

  1. 30 23 */60 * * expect /root/createSSL.sh

 

转载请注明:左手代码右手诗 » 使用SSL证书,摆脱运营商流量劫持!

喜欢 (4)or分享 (0)
发表我的评论
取消评论

 

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. mark
    大猫2017-12-19 18:31 回复