Nginx 下部署 HTTPS 與安全調優

來源:互聯網
上載者:User

標籤:其他   ati   dig   ica   secure   cgi   unit   wiki   []   

什麼是 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/phphubcd /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 incorporatedinto 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 blankFor some fields there will be a default value,If you enter ‘.‘, the field will be left blank.-----Country Name (2 letter code) [AU]:CNState or Province Name (full name) [Some-State]:BeiJingLocality Name (eg, city) []:BeiJingOrganization Name (eg, company) [Internet Widgits Pty Ltd]:The EST GroupOrganizational Unit Name (eg, section) []:DevCommon Name (e.g. server FQDN or YOUR name) []:*.phphub.org // ----------注意這個地方要認真填寫Email Address []: emailaddress @ gmail.comPlease enter the following ‘extra‘ attributesto be sent with your certificate requestA 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 與安全調優

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.