zoukankan      html  css  js  c++  java
  • Nginx 下部署 HTTPS 与安全调优

    什么是 HTTPS?#

    HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。

    更多基本介绍请查阅:

    需要弄清楚的几个问题:

    • HTTPS 和 SSL 的关系与基本技术实现;
    • SSL 证书的类型;
    • 什么是证书颁发机构, 为什么会存在;
    • 证书认证等级, DV, OV 和 EV 各自的意思;
    • 什么是 泛域名 SSL 证书 (Wildcard Domain SSL Certificates)

    操作步骤#

    一个大概流程如下:

    1. 购买前准备 - 服务器生成 csr 和 key 文件;
    2. 购买证书 - 利用上面生成的 csr 文件去购买证书;
    3. 购买成功后的证书有两个, 一个是域名证书, 一个是链证书, 把他们俩按照顺序合并为 crt 文件;
    4. Nginx 下配置 key 和 crt 文件, 并做安全调优.

    购买证书前的准备#

    1. 生成证书 CSR 和 KEY#

    mkdir -p /etc/nginx/ssl/phphub
    cd /etc/nginx/ssl/phphub

    2. 生成 orig 文件#

    openssl genrsa -out phphub.orig 2048

    3. 生成 csr 文件#

    运行

    openssl req -new -key phphub.orig -out phphub.csr

    输出, 需要填写内容:

    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) [AU]:CN
    State or Province Name (full name) [Some-State]:BeiJing
    Locality Name (eg, city) []:BeiJing
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:The EST Group
    Organizational Unit Name (eg, section) []:Dev
    Common Name (e.g. server FQDN or YOUR name) []:*.phphub.org // ----------注意这个地方要认真填写
    Email Address []: emailaddress @ gmail.com
    
    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:  ----------注意不填写----------
    An optional company name []:  ----------注意不填写----------

    4. 生成 private key 文件#

    openssl rsa -in phphub.orig -out phphub.key

    至此文件夹里面有 三个文件:

    root@localhost:/etc/nginx/ssl/phphub# tree
    .
    ├── ikbcity.csr
    ├── phphub.key
    └── phphub.orig

    购买证书#

    购买细节这里省去, 需要注意的是要认准比较权威的认证机构购买...

    购买成功后会给你发两个证书 server.crt 和 server.intermediate.crt, 生成最终的 server.chained.crt

    cat server.crt server.intermediate.crt > phphub.crt

    此文件就可以和上面生成的 key 文件一起用来配置 nginx 了:

    ssl_certificate     /etc/nginx/ssl/phphub/phphub.crt;
    ssl_certificate_key /etc/nginx/ssl/phphub/phphub.key;

    配置安全的 Ngxin#

    链接:

    强制使用 HTTPS#

    server {
        listen 80;
        listen 443 ssl;
        server_name example.com;
    
        if ($scheme = http) {
            return 301 https://$server_name$request_uri;
        }
    
        ....
    }    

    去除 Nginx 的 X-Powered-By header#

    fastcgi_hide_header X-Powered-By;

    去除 nginx 版本#

    server_tokens off;

    不允许被 iframe 加载#

    add_header X-Frame-Options SAMEORIGIN;

    其他参照此 Gits: Nginx config on Gits

    静态内容#

    一般都会出现 cdn 服务器无法访问 https 源服务器的问题, 可以使用专门的域名 static.phphub.org 来解决, 此域名专门用来输送静态内容:

    server {
        listen 80;
        server_name static.phphub.org;
        root /var/www/phphub/public;
        location ~* .(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
                add_header        Cache-Control public;
                add_header        Cache-Control must-revalidate;
                expires           7d;
        }
        location  / {
                deny all;
        }
    }

    结语#

    可以利用 SSL Server Test -- 安全测试工具 去测试下你的 HTTPS 是否够安全.

    附上 phphub 的 test

  • 相关阅读:
    DLL编写中extern “C”和__stdcall的作用
    spring mvc controller中的异常封装
    springMVC 【@response 返回对象自动变成json并且防止乱码】 & 【配置支持实体类中的@DateTimeFormat注解】
    Eclipse中修改SVN用户名和密码方法
    Maven
    j2ee、mvn、eclipse、Tomcat等中文乱码问题解决方法
    maven生成jar包
    windows超过最大连接数解决命令
    spring 国际化-i18n
    springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用
  • 原文地址:https://www.cnblogs.com/linkenpark/p/7283145.html
Copyright © 2011-2022 走看看