[TOC]
前言
上一篇钦铺,我們介紹了nginx啟用https订雾,但是使用的證書是私有CA頒發(fā)的。
這種私有證書出來個人練習(xí)或者在內(nèi)部使用外矛洞,還真不知道有什么其他用途洼哎。
現(xiàn)在,我們來體驗體驗真正的商用https證書沼本。
1 環(huán)境準備
- 域名
- 本人這里是在阿里云買的域名
- 只要你想買的域名不是 google.com或在baidu.com這種白金次的話谱净,一般都不貴吧……
- 域名備不備案在這里無所謂了
- 將域名解析到你自己的服務(wù)器
- 公網(wǎng)服務(wù)器
- 本人這里使用的是阿里云主機
- 得搞個公網(wǎng)主機,不然域名解析到哪里去呢擅威?
- https證書
- 本人這里使用的是https://startssl.com/頒發(fā)的免費證書
- 確實不錯壕探,免費的
- 證書申請有疑問?請移駕:https://www.oschina.net/translate/switch-to-https-now-for-free?cmp
2 申請https證書
此處使用的是https://startssl.com/提供的免費https證書郊丛。
這部分有疑問的話李请,可以參考本人另一篇文章:http://blog.csdn.net/hylexus/article/details/53150333
# 本人申請證書后下載得到了一個hyl.xxx.tech.zip的壓縮包
# 其中hyl.xxx.tech應(yīng)該會用你自己的域名代替
# 該文件內(nèi)容如下:
[root@hylexus https]# tree
.
├── ApacheServer.zip # apache/httpd
├── IISServer.zip # MS-IIS
├── NginxServer.zip # Nginx
└── OtherServer.zip # 其他服務(wù)器
這里我們使用NginxServer.zip中的文件進行后續(xù)操作.
# 解壓后得到文件:1_hyl.xxx.tech_bundle.crt
# 為方便后續(xù)操作,將該文件重命名為nginx.crt
# 并將其移動至nginx配置文件目錄下新建的ssl目錄下
mkdir /etc/nginx/ssl
# 證書文件nginx.crt
cp 1_hyl.xxx.tech_bundle.crt /etc/nginx/ssl/nginx.crt
# 應(yīng)用程序秘鑰nginx.key
# 名字隨意厉熟,這個文件是你自己生成CSR的時候用的秘鑰文件
cp nginx.key /etc/nginx/ssl/nginx.key
3 nginx啟用https
此時的ssl目錄:
[root@hylexus ssl]# pwd
/etc/nginx/ssl
[root@hylexus ssl]# tree
.
├── nginx.crt # 申請的https證書
└── nginx.key # 應(yīng)用程序私鑰
基于域名的虛擬主機配置
server{
# 同時支持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等的配置按自己的喜好來
此處應(yīng)該注意的地方是在你的虛擬主機下加一個 Valve
# 注意幾個請求頭和nginx虛擬主機的配置中應(yīng)該是對應(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"/>