黑帽联盟

 找回密码
 会员注册
查看: 1300|回复: 0
打印 上一主题 下一主题

[资源教程] Linux+Nginx/Apache/Tomcat新增SSL证书,开启https访问教程

[复制链接]

132

主题

11

听众

295

积分

版主

Rank: 7Rank: 7Rank: 7

  • TA的每日心情
    擦汗
    2018-6-12 09:41
  • 签到天数: 273 天

    [LV.8]以坛为家I

    本帖最后由 权利的游戏 于 2018-5-3 15:06 编辑

    一、下载证书
    成功申请 SSL 证书之后,就可以下载到配置 SSL 的证书了!一般情况下,都可以选择下载相应 WEB 服务器的不同证书,或者直接打包下载主流 WEB 服务器的证书,如图所示:
    61.png


    下载后,就可以根据不同的 WEB 服务器来选择相应的证书了。

    二、Nginx
    先确认 nginx 安装时已编译 http_ssl 模块,也就是执行如下命令查看是否存在--with-http_ssl_module 参数:
    1. linux-test:~ # /usr/local/nginx/sbin/nginx -V
    2. nginx version: nginx/1.6.0
    3. built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
    4. TLS SNI support enabled
    5. configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.0.1/
    复制代码
    如果没有这个参数,说明没有编译 SSL 模块,那么请参考上上篇文章自行解决,此处就不赘述了。
    ①、准备证书
    Nginx 需要用到 2 个证书文件:
    I.  证书公钥 (crt 格式)
    II. 证书私钥(key 格式)
    62.png


    拿到证书后,将其上传到 nginx 下的 ssl 目录(也可自定义位置)。

    ②、修改配置A. http 和 https 全局共存
    在原 server 模块新增监听 443 端口,然后新增如下代码(具体看注释)。
    1. server {
    2.        listen 80;
    3.        #新增监听443端口,并指定443为ssl:
    4.        listen 443 ssl;
    5.        server_name yourdomain.com;
    6.        #新增ssl配置---开始:
    7.        ssl_certificate /usr/local/nginx/ssl/yourdomain_bundle.crt; #证书公钥文件路径
    8.        ssl_certificate_key  /usr/local/nginx/ssl/yourdomain.key;   #证书私钥文件路径
    9.        ssl_session_timeout  5m;  #5分钟session会话保持
    10.        ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    11.        ssl_ciphers  HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
    12.        #新增ssl配置---结束:
    13.        location / {
    14.                  #其他规则保持不变
    15.         }
    16.    }
    复制代码
    保存配置之后,先执行如下命令测试配置是否正确:
    1. linux-test:~ # /usr/local/nginx/sbin/nginx -t
    2. #如下显示则为正确无误:
    3. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    4. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    复制代码
    确认无误之后,执行如下命令重载 nginx,让配置生效:
    1. linux-test:~ # /usr/local/nginx/sbin/nginx -s reload
    复制代码
    如无错误,现在应该可以顺利访问 https://yourdomain.com/了!值得说明的是,这样配置后,http 和 https 是全局共存的,你能 http 访问到的页面,https 也可以访问得到。

    B. 全局强制 https
    如果是全局 https 访问,那么额外写一个监听 80 的 server,让 http 访问跳转到 https 上即可,下面是参考模板:
    1. server{
    2.       listen 80;
    3.       server_name yourdomain.com;
    4.       root  /path/for/yourdomain.com;
    5.       location / {
    6.           rewrite (.*) https://yourdomain.com$1 permanent;
    7.       }
    8.    }

    9. server {
    10.        listen 443;
    11.        server_name yourdomain.com;
    12.        ssl on;
    13.        ssl_certificate /usr/local/nginx/ssl/yourdomain_bundle.crt; #证书公钥文件路径
    14.        ssl_certificate_key  /usr/local/nginx/ssl/yourdomain.key;   #证书私钥文件路径
    15.        ssl_session_timeout  5m;
    16.        ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    17.        ssl_ciphers  HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
    18.        location / {
    19.                  #其他规则维持不变
    20.         }
    21.    }
    复制代码
    C. 部分强制 https,部分强制 http
    可能有部分强迫症会有这样的需求:我只要部分页面强制 https 访问,比如后台及登陆页面,其他常规页面强制 http 访问,我该如何设置?
    思路:和 B 方案一样,分别 2 个 server 模块,并新增判断规则,指定部分页面 http 访问,部分页面 https 访问。
    具体可以参考一下张戈博客的配置(主要修改中文注释部分,其他配置保持不变):
    1. #监听httpserver
    2. server
    3.         {
    4.                 listen 80;
    5.                 server_name zhangge.net m.zhangge.net;
    6.                 index index.html index.htm index.php default.html default.htm default.php;
    7.                 root  /home/web/zhangge.net;
    8.                 include zhangge.conf;
    9.                 location ~ /uploads/.*.(php|php5)?$ {
    10.                                 deny all;
    11.                         }
    12.                 #若是匹配到wp-login.php登陆,则跳到https
    13.                 location ~ /(wp-login.php(.*)$) {
    14.                         rewrite ^(.*)$ https://zhangge.net$1 permanent;
    15.                         break;
    16.                         }
    17.                 #wordpress后台强制跳到https
    18.                 location /wp-admin {
    19.                         rewrite ^(.*)$ https://zhangge.net$1 permanent;
    20.                         }

    21.                 location ~ [^/].php(/|$)
    22.                         {
    23.                                 try_files $uri =404;
    24.                                 fastcgi_pass  unix:/tmp/php-cgi.sock;
    25.                                 fastcgi_index index.php;
    26.                                 include fastcgi.conf;
    27.                         }
    28.                 location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
    29.                         {
    30.                                 expires      30d;
    31.                         }

    32.                 location ~ .*.(js|css)?$
    33.                         {
    34.                                 expires      30d;
    35.                         }
    36.                 access_log  /home/logs/zhangge.net.log  access;

    37.         }
    38. #监听https
    39. server
    40.         {
    41.                 listen 443;
    42.                 server_name zhangge.net m.zhangge.net;
    43.                 ssl on;
    44.                 ssl_certificate /usr/local/nginx/ssl/zhangge.net.crt;
    45.                 ssl_certificate_key  /usr/local/nginx/ssl/zhangge.net.key;
    46.                 ssl_session_timeout  5m;
    47.                 ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    48.                 ssl_ciphers  HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
    49.                 index index.html index.htm index.php default.html default.htm default.php;
    50.                 root  /home/web/zhangge.net;

    51.                 #有偿服务付款页面使用https访问
    52.                 location /wp-content/plugins/alipay {
    53.                         try_files $uri $uri/ /index.php?$args;
    54.                 }

    55.                 #若没有匹配到wp-admin或wp-includes,则跳到http访问(反向逻辑:即只允许指定页面开启https)
    56.                 location / {
    57.                         if ($request_uri !~* "wp-admin|wp-includes") {
    58.                                 rewrite (.*) http://zhangge.net$1 permanent;
    59.                         }
    60.                 }

    61.                 location ~ /uploads/.*.(php|php5)?$ {
    62.                                 deny all;
    63.                         }
    64.                 location ~ [^/].php(/|$)
    65.                         {
    66.                                 try_files $uri =404;
    67.                                 fastcgi_pass  unix:/tmp/php-cgi.sock;
    68.                                 fastcgi_index index.php;
    69.                                 include fastcgi.conf;
    70.                         }

    71.                 location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
    72.                         {
    73.                                 expires      30d;
    74.                         }

    75.                 location ~ .*.(js|css)?$
    76.                         {
    77.                                 expires      30d;
    78.                         }
    79.                 access_log  /home/wwwlogs/zhangge.net.log  access;

    80.         }
    复制代码
    二、Apache
    同样,先确认 Apache 安装时已添加 SSL 支持模块。如果没有请自行搜索搞定,本文不再赘述。
    ①、准备证书
    Apache 需要用到三个证书文件:
    I. 根证书:root_bundle.crt
    II. 证书公钥:yourdomain.com.crt
    III. 证书私钥:yourdomain.com.key
    63.png


    将下载好的三个证书文件,上传到 apache 下的 ssl 目录中(可自定义位置)。

    ②、修改配置
    I. 编辑 httpd.conf 文件,取消以下内容的#注释符号:
    1. #LoadModule ssl_module modules/mod_ssl.so  
    2. #Include conf/extra/httpd-ssl.conf
    复制代码

    II. 编辑 http-ssl.conf 文件,如下修改:
    1. #找到如下行,并替换为证书公钥的实际路径:
    2. SSLCertificateFile /usr/local/apache/ssl/public.cer

    3. #找到如下行,并替换为证书私钥的实际路径:
    4. SSLCertificateKeyFile /usr/local/apache/ssl/private.key

    5. #找到如下行,取消行首注释符,并替换为根证书实际路径:
    6. #SSLCertificateChainFile /usr/local/apache/ssl/ca.cer
    复制代码
    III. 保存退出,并重启 Apache 即可。

    三、Tomcat
    ①、准备证书
    Tomcat 只需要用到一个 jks 格式的证书文件,比如 yourdomain.com.jks。
    64.png

    拿到文件后,将其上传到 Tomcat 下的 conf 目录中

    ②、修改配置
    打开 conf 目录下的 server.xml 文件,找到以下内容:

    去掉前后的注释,并如下修改(或者直接其后添加以下代码亦可):
    1. <connector
    2.      port="443"
    3.      protocol="org.apache.coyote.http11.Http11Protocol"
    4.      SSLEnabled="true"
    5.      maxThreads="150"
    6.      scheme="https"
    7.      secure="true"
    8.      keystoreFile="confyourdomain.jks"  
    9.      keystorePass="password"
    10.      clientAuth="false"
    11.      sslProtocol="TLS"
    12. />
    复制代码
    退出并保存,最后重启 Tomcat 即可。

    四、解决警告
    如果网页中存在不带 https 的资源,比如 http 协议的 js、css 或图片,那么访问这个 https 页面,某些浏览器(比如 IE)就会发出警告,提示页面中存在不安全的内容,并且不会加载这些 http 协议的资源,导致页面错乱等问题:
    65.png


    解决办法:
    方法①、使用相对地址
    只要将这些 http 的资源链接,改为相对地址。比如原链接是那么改成即可。
    方法②、修改网站代码
    如果是全局 https 访问,那么你将网站代码中的链接均改为 https 好了。如果是 http 和 https 混合的,那么准备 2 套网站文件也行。然后在 nginx 当中设置不同的 root 路径。
    为了省事,我推荐方法①。


    本文转载自张戈博客,感谢张戈提供的好文章
    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    相关帖子

    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部