1. 程式人生 > >Nginx 下部署 HTTPS 與安全調優

Nginx 下部署 HTTPS 與安全調優

quest tro efault attribute you attr inf [] validate

什麽是 HTTPS?#

HTTPS(全稱:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全為目標的HTTP通道,簡單講是HTTP的安全版。

更多基本介紹請查閱:

  • 數字簽名是什麽?(圖文並茂, 清晰易懂, 重點推薦)
  • HTTPS on WIKI
  • 密碼學筆記
  • SSL 與數字證書
  • 另一個圖文並茂的筆記, 供參考 --> 泛域名ssl證書搭建全攻略

需要弄清楚的幾個問題:

  • 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

至此文件夾裏面有 三個文件:

[email protected]:/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#

鏈接:

  • Best nginx configuration for security
  • Nginx config on Gits
  • Top 20 Nginx WebServer Best Security Practices
  • SSL Server Test -- 安全測試工具

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

技術分享

Nginx 下部署 HTTPS 與安全調優