HTTPS +Nginx免费部署指南

       

前言

随着国内各大网站纷纷开启全站 HTTPS 时代,HTTPS 已不再是支付等敏感操作过程的专属,开启 HTTPS 对于个人网站或者小型网站也不再遥不可及。 今天博主就以自己的网站 www.rapospectre.com 为例叙述一下为自己网站点亮 HTTPS 小绿锁的过程。

HTTP 和 HTTPS

HTTPS( Hypertext Transfer Protocol over Secure Socket Layer ),是以安全为目标的 HTTP 通道,简单讲是 HTTP 的安全版。即 HTTP 下加入 SSL 层,HTTPS 的安全基础是 SSL ,因此加密的详细内容就需要 SSL 。 它是一个 URI scheme( 抽象标识符体系 ),句法类同 http :体系。用于安全的 HTTP 数据传输。 https:URL 表明它使用了 HTTP,但 HTTPS 存在不同于 HTTP 的默认端口及一个加密/身份验证层(在 HTTP 与 TCP 之间)。这个系统的最初研发由网景公司进行,提供了身份验证与加密通讯方法,现在它被广泛用于万维网上安全敏感的通讯,例如交易支付方面。

HTTP 超文本传输协议 ( HTTP-Hypertext transfer protocol ) 是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文档的数据传送协议。

从概念里可以看到,要开启 HTTPS 至关重要的一点就是 ssl 层的身份验证,而身份验证需要用到 ssl 证书,以前少有免费 ssl 证书,所以小站基本不会选择 https ,而现在网上提供个人免费 ssl 证书的机构越来越多,这使得免费升级站点为 https 成为可能。

1. 申请 SSL 证书

网上已经有不少机构提供个人免费 ssl 证书,有效期几个月到几年不等,博主使用的是 StartSSL, 申请成功后有效期 3 年,到期后可免费续租。 具体申请过程不复杂,注册后根据提示验证网站 + 生成证书即可,如果不清楚可以 Google 一下

要注意 StartSSL 验证网站拥有者时是给域名所有者的邮箱发验证邮件,如果域名开启了隐私保护请暂时关闭。

然后在自己服务器中生成 SSL 证书的 csr ,记住生成输入的秘密,之后要用到:

  1. openssl req -new-sha256 -key rapospectre.com_secure.key -out rapospectre.com.csr

假设以上文件生成在 /var/tmp 文件夹下

在 StartSSL 填写 csr 文件内容,生成 SSL 证书并下载, 生成成果后如图:

点击 Retrieve 下载证书,解压缩后包含各种服务器的 crt ,博主使用 nginx 做反代,所以选择 nginxserver 解压缩后得到 www.rapospectre.com_bundle.crt 将此文件上传到服务器,假设传到 /var/tmp/ 文件夹

2. 配置服务器

以 nginx 为例,打开 /etc/nginx/nginx.conf,加入配置:

  1. server {
  2. listen 443 ssl;
  3. ssl_certificate /var/tmp/www.rapospectre.com_bundle.crt;
  4. ssl_certificate_key /var/tmp/rapospectre.com_secure.key;
  5. ssl_prefer_server_ciphers on;
  6. ssl_protocols TLSv1TLSv1.1TLSv1.2;
  7. #选择特定的加密方式, 避免已知的漏洞
  8. ssl_ciphers 'kEECDH+ECDSA+AES128 kEECDH+ECDSA+AES256 kEECDH+AES128 kEECDH+AES256 kEDH+AES128 kEDH+AES256 DES-CBC3-SHA +SHA !aNULL !eNULL !LOW !MD5 !EXP !DSS !PSK !SRP !kECDH !CAMELLIA !RC4 !SEED';
  9. #让浏览器记住直接访问 https 的网址, 不再去 http 重定向。
  10. add_header Strict-Transport-Security'max-age=31536000; preload';
  11. add_header X-Frame-Options DENY;
  12. ssl_session_cache shared:SSL:10m;
  13. ssl_session_timeout 10m;
  14. keepalive_timeout 70;
  15. ssl_dhparam /var/tmp/dhparam2048.pem;
  16. #禁止服务器自动解析资源类型
  17. add_header X-Content-Type-Options nosniff;
  18. #防XSS攻擊
  19. add_header X-Xss-Protection1;
  20. server_name www.rapospectre.com rapospectre.com;

在之前的 80 端口进行重定向配置:

  1. server {
  2. listen 80;
  3. server_name rapospectre.com www.rapospectre.com;
  4. return301 https://www.rapospectre.com$request_uri;
  5. }

3. HTTP 替换

将网站所有以 http 方式获取的资源全部改为 https 方式或自动方式获取, eg:

  1. <scriptsrc="http://xx.cdn.com/jquery.js"></script>
  2. 改为
  3. <scriptsrc="https://xx.cdn.com/jquery.js"></script>
  4. <scriptsrc="//xx.cdn.com/jquery.js"></script>

重启服务器,提示输入之前生成 csr 的密码,输入密码,重启成功,访问 https://www.rapospectre.com 可以看到 HTTPS 已经正常工作!

顺手来一发 SSLLABS测试,wtf 只有 F?

看图发现因为

This server is vulnerable to the OpenSSL Padding Oracle vulunerability ( CVE-2016-2107 )

原来是 OpenSSL 漏洞的锅,升级 OpenSSL 到 1.0.2h 版 ( 后续版本应该也可以,博主一开始升级到了最新的 1.1.0a 结果服务器挂了 ) 即可修复漏洞:

Fix OpenSSL Padding Oracle vulnerability (CVE-2016-2107) - Ubuntu 14.04

  1. # Based on http://fearby.com/article/update-openssl-on-a-digital-ocean-vm/
  2. $ apt-get update
  3. $ apt-get dist-upgrade
  4. $ wget ftp://ftp.openssl.org/source/old/1.0.2/openssl-1.0.2h.tar.gz
  5. $ tar -xvzf openssl-1.0.2h.tar.gz
  6. $ cd openssl-1.0.2h
  7. $ ./config --prefix=/usr/
  8. $ make depend
  9. $ sudo make install
  10. $ openssl version
  11. # OpenSSL 1.0.2h 3 May 2016
  12. # now restart your nginx or other server
  13. $ nginx -s reload

3. HTTP2

开启 http2 ,nginx 在 1.9.5 以后的版本才开始支持 http2 ,之前一直使用的是 spdy 而 ubuntu 自带的 nginx 是 1.4.6 的古董, 所以需要重新编译安装新版的 nginx ,博主选择了安装最新的 nginx 1.11.4:

  1. 下载 nginx 到 /var/tmp/nginx:

    1. wget http://nginx.org/download/nginx-1.11.4.tar.gz
  2. 解压nginx-1.11.4.tar.gz文件

    1. tar zxvf nginx-1.11.4.tar.gz
  3. 进入ngixn-1.11.4文件夹

    1. cd nginx-1.2.5
  4. 查看nginx原来的配置

    1. nginx -V

    上面的命令将输出类似如下信息:

    1. --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2'--with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro'--prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock--pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module

    我们在后面加上 http2 模块与 上一步中 openssl 源码( 是源码路径不是安装 )路径:

    1. --with-http_v2_module --with-openssl=/var/tmp/ssl/openssl-1.0.2h

    注意,如果以上信息内包含 --with-spdy_module 请去除,nginx 1.9.5 之后已弃用 spdy

  5. 执行configure命令,后面跟上原来nginx的配置

    1. ./configure --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock--pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --with-http_v2_module --with-openssl=/var/tmp/ssl/openssl-1.0.2h

configure时可能遇到的几个错误:

  1. –with-http_xslt_module 时提示 the HTTP XSLT module requires the libxml2/libxslt libraries

    1. apt-get install libxml2 libxml2-dev libxslt-dev
  2. –with-http_image_filter_module 时提示 the HTTP image filter module requires the GD library.

    1. apt-get install libgd2-xpm-dev
  3. –with-http_geoip_module 时提示 the GeoIP module requires the GeoIP library.

    1. apt-get install geoip-database libgeoip-dev
  4. ./configure: error: the HTTP rewrite module requires the PCRE library.

    1. apt-get install libpcre3 libpcre3-dev

再次执行 configure 命令, 然后make && make install。 编译好以后objs目录下多出一个nginx文件,用它替换旧的 nginx 文件:

  1. mv /usr/sbin/nginx /usr/sbin/nginx-backup
  2. cp objs/nginx /usr/sbin/nginx

执行/usr/sbin/nginx -t 命令检查配置文件返回下面的信息:

  1. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  2. nginx: configuration file /etc/nginx/nginx.conf test is successful

表示 nginx 升级成功,修改 nginx 配置,加入 http2 支持:

  1. listen 443 ssl http2 fastopen=3 reuseport;

重启 nginx 访问正常后再测一发:

搞定,个人网站加入 HTTPS 并且 SSLABS 评分 A+ 。 快来试试吧~

( 博主网站图片上传到七牛,而七牛免费似乎账户不支持 https 链接,所以有些文章比如说这篇会提示网页内有不安全的内容 )

原文地址https://www.rapospectre.com/blog/https-deploy-guide

猜你喜欢

转载自hugoren.iteye.com/blog/2372390
今日推荐