Nginx 下部署 HTTPS 与安全调优

说明

此文章是 phphub 部署 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;

配置安全的 Nginx

链接:

强制使用 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

sun_with_face:

摈弃世俗浮躁,追求技术精湛
本帖已被设为精华帖!
Summer
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 19
Summer

@lijinma Static 域名就是作为 CDN 的源服务器, 网站直接读 CDN 内容, 不直接读源服务器 (CND 不就是来干这使得吗 :smile_cat: ) .

那个链接是 七牛的 HTTPS CDN 域名.

9年前 评论
Summer

@lijinma 之所以要单独给七牛开个 Static 域名是因为七牛不支持 HTTPS 源数据读取 ( 貌似很多 CDN 都不支持 ).

9年前 评论
Summer

@baocaixiong 没看明白你在问什么, 你是想这么做吗?

# Force non www
if ($host ~* ^www\.(.*))
{
    set $host_without_www $1;
    rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# Force HTTPS
if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}
9年前 评论
Summer

@baocaixiong 邮件的事情不客气.

9年前 评论
Summer

@baocaixiong

强制 HTTPS

# Force HTTPS
if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}
9年前 评论
Summer

@nihaoit 建议在 如果是商业项目的话, 建议在 https://www.godaddy.com/ssl/ssl-certificat... 这里购买.

8年前 评论

为什么我默认安装证书后,默认输入网址没有跳转到https中

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!