[TOC]
前言
上一篇,我們介紹了nginx啟用https,但是使用的證書(shū)是私有CA頒發(fā)的。
這種私有證書(shū)出來(lái)個(gè)人練習(xí)或者在內(nèi)部使用外,還真不知道有什么其他用途。
現(xiàn)在,我們來(lái)體驗(yàn)體驗(yàn)真正的商用https證書(shū)。
1 環(huán)境準(zhǔn)備
- 域名
- 本人這里是在阿里云買的域名
- 只要你想買的域名不是 google.com或在baidu.com這種白金次的話,一般都不貴吧……
- 域名備不備案在這里無(wú)所謂了
- 將域名解析到你自己的服務(wù)器
- 公網(wǎng)服務(wù)器
- 本人這里使用的是阿里云主機(jī)
- 得搞個(gè)公網(wǎng)主機(jī),不然域名解析到哪里去呢?
- https證書(shū)
- 本人這里使用的是https://startssl.com/頒發(fā)的免費(fèi)證書(shū)
- 確實(shí)不錯(cuò),免費(fèi)的
- 證書(shū)申請(qǐng)有疑問(wèn)?請(qǐng)移駕:https://www.oschina.net/translate/switch-to-https-now-for-free?cmp
2 申請(qǐng)https證書(shū)
此處使用的是https://startssl.com/提供的免費(fèi)https證書(shū)。
這部分有疑問(wèn)的話,可以參考本人另一篇文章:http://blog.csdn.net/hylexus/article/details/53150333
# 本人申請(qǐng)證書(shū)后下載得到了一個(gè)hyl.xxx.tech.zip的壓縮包
# 其中hyl.xxx.tech應(yīng)該會(huì)用你自己的域名代替
# 該文件內(nèi)容如下:
[root@hylexus https]# tree
.
├── ApacheServer.zip # apache/httpd
├── IISServer.zip # MS-IIS
├── NginxServer.zip # Nginx
└── OtherServer.zip # 其他服務(wù)器
這里我們使用NginxServer.zip中的文件進(jìn)行后續(xù)操作.
# 解壓后得到文件:1_hyl.xxx.tech_bundle.crt
# 為方便后續(xù)操作,將該文件重命名為nginx.crt
# 并將其移動(dòng)至nginx配置文件目錄下新建的ssl目錄下
mkdir /etc/nginx/ssl
# 證書(shū)文件nginx.crt
cp 1_hyl.xxx.tech_bundle.crt /etc/nginx/ssl/nginx.crt
# 應(yīng)用程序秘鑰nginx.key
# 名字隨意,這個(gè)文件是你自己生成CSR的時(shí)候用的秘鑰文件
cp nginx.key /etc/nginx/ssl/nginx.key
3 nginx啟用https
此時(shí)的ssl目錄:
[root@hylexus ssl]# pwd
/etc/nginx/ssl
[root@hylexus ssl]# tree
.
├── nginx.crt # 申請(qǐng)的https證書(shū)
└── nginx.key # 應(yīng)用程序私鑰
基于域名的虛擬主機(jī)配置
server{
# 同時(shí)支持http和https
listen 80;
listen 443 ssl;
server_name hyl.xxx.tech;
access_log /var/log/nginx/hyl.xxx.tech.access.log;
keepalive_timeout 70;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
location / {
#root /usr/share/nginx/html;
index dashboard index;
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
4 tomcat-server.xml配置
Connector等的配置按自己的喜好來(lái)
此處應(yīng)該注意的地方是在你的虛擬主機(jī)下加一個(gè) Valve
# 注意幾個(gè)請(qǐng)求頭和nginx虛擬主機(jī)的配置中應(yīng)該是對(duì)應(yīng)的
# X-Forwarded-For、X-Forwarded-Proto等
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="X-Forwarded-For"
protocolHeader="X-Forwarded-Proto"
protocolHeaderHttpsValue="https"/>
5 效果預(yù)覽
沒(méi)有警告信息的https