說明
此文章是 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)
操作步驟
一個大概流程如下:
- 購買前準備 - 服務器生成 csr 和 key 文件;
- 購買證書 - 利用上面生成的 csr 文件去購買證書;
- 購買成功後的證書有兩個, 一個是域名證書, 一個是鏈證書, 把他們倆按照順序合併爲 crt 文件;
- 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 例如:
# 自己簽發證書 $ openssl x509 -req -days 365 -in 33iq.csr -signkey 33iq.key -out 33iq.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 是否夠安全.
原文地址地址 : https://phphub.org/topics/206