上上篇文章《
nginx平滑升級&新增模塊》提到了公司的https訪(fǎng)問(wèn)需求。當我新增了SSL模塊之后,卻發(fā)現以前還真沒(méi)部署過(guò)https訪(fǎng)問(wèn)。
下面整理我的部署過(guò)程,并收集了一下Apache和Tomcat這2種Linux下常用的WEB軟件配置SSL的簡(jiǎn)單簡(jiǎn)單步驟,以便回頭翻閱。
一、下載證書(shū)
成功申請
SSL證書(shū)之后,就可以下載到配置SSL的證書(shū)了!一般情況下,都可以選擇下載相應WEB服務(wù)器的不同證書(shū),或者直接打包下載主流WEB服務(wù)器的證書(shū),如圖所示:
下載后,就可以根據不同的WEB服務(wù)器來(lái)選擇相應的證書(shū)了。
二、Nginx
先確認
nginx安裝時(shí)已編譯http_ssl模塊,也就是執行如下命令查看是否存在--with-http_ssl_module參數:
Shell
1
2
3
4
5
linux-test:~ # /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.6.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=/usr/local/src/openssl-1.0.1/
如果沒(méi)有這個(gè)參數,說(shuō)明沒(méi)有編譯SSL模塊,那么請參考上上篇文章自行解決,此處就不贅述了。
①、準備證書(shū)
Nginx需要用到2個(gè)證書(shū)文件:
I. 證書(shū)公鑰 (crt格式)
II. 證書(shū)私鑰(key格式)
拿到證書(shū)后,將其上傳到
nginx下的ssl目錄(也可自定義位置)。
②、修改配置
A. http和https全局共存
在原server模塊新增監聽(tīng)443端口,然后新增如下代碼(具體看注釋?zhuān)?div style="height:15px;">
Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
#新增監聽(tīng)443端口,并指定443為ssl:
listen 443 ssl;
server_name yourdomain.com;
#新增ssl配置---開(kāi)始:
ssl_certificate /usr/local/nginx/ssl/yourdomain_bundle.crt; #證書(shū)公鑰文件路徑
ssl_certificate_key /usr/local/nginx/ssl/yourdomain.key; #證書(shū)私鑰文件路徑
ssl_session_timeout 5m; #5分鐘session會(huì )話(huà)保持
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
ssl_prefer_server_ciphers on;
#新增ssl配置---結束:
location / {
#其他規則保持不變
}
}
保存配置之后,先執行如下命令測試配置是否正確:
Shell
1
2
3
4
linux-test:~ # /usr/local/nginx/sbin/nginx -t
#如下顯示則為正確無(wú)誤:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
確認無(wú)誤之后,執行如下命令重載
nginx,讓配置生效:
Shell
1
linux-test:~ # /usr/local/nginx/sbin/nginx -s reload
如無(wú)錯誤,現在應該可以順利訪(fǎng)問(wèn)https://yourdomain.com/了!值得說(shuō)明的是,這樣配置后,http和https是全局共存的,你能http訪(fǎng)問(wèn)到的頁(yè)面,https也可以訪(fǎng)問(wèn)得到。
B. 全局強制https
如果是全局https訪(fǎng)問(wèn),那么額外寫(xiě)一個(gè)監聽(tīng)80的server,讓http訪(fǎng)問(wèn)跳轉到https上即可,下面是參考模板:
Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server{
listen 80;
server_name yourdomain.com;
root /path/for/yourdomain.com;
location / {
rewrite (.*) https://yourdomain.com$1 permanent;
}
}
server {
listen 443;
server_name yourdomain.com;
ssl on;
ssl_certificate /usr/local/nginx/ssl/yourdomain_bundle.crt; #證書(shū)公鑰文件路徑
ssl_certificate_key /usr/local/nginx/ssl/yourdomain.key; #證書(shū)私鑰文件路徑
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
ssl_prefer_server_ciphers on;
location / {
#其他規則維持不變
}
}
C. 部分強制https,部分強制http
可能有部分強迫癥會(huì )有這樣的需求:我只要部分頁(yè)面強制https訪(fǎng)問(wèn),比如后臺及登陸頁(yè)面,其他常規頁(yè)面強制http訪(fǎng)問(wèn),我該如何設置?
思路:和B方案一樣,分別2個(gè)server模塊,并新增判斷規則,指定部分頁(yè)面http訪(fǎng)問(wèn),部分頁(yè)面https訪(fǎng)問(wèn)。
具體可以參考一下張戈博客的配置(主要修改中文注釋部分,其他配置保持不變):
Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#監聽(tīng)httpserver
server
{
listen 80;
server_name zhangge.net m.zhangge.net;
index index.html index.htm index.php default.html default.htm default.php;
root /home/web/zhangge.net;
include zhangge.conf;
location ~ /uploads/.*\.(php|php5)?$ {
deny all;
}
#若是匹配到wp-login.php登陸,則跳到https
location ~ /(wp-login\.php(.*)$) {
rewrite ^(.*)$ https://zhangge.net$1 permanent;
break;
}
#wordpress后臺強制跳到https
location /wp-admin {
rewrite ^(.*)$ https://zhangge.net$1 permanent;
}
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 30d;
}
access_log /home/logs/zhangge.net.log access;
}
#監聽(tīng)https
server
{
listen 443;
server_name zhangge.net m.zhangge.net;
ssl on;
ssl_certificate /usr/local/nginx/ssl/zhangge.net.crt;
ssl_certificate_key /usr/local/nginx/ssl/zhangge.net.key;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!ADH:!EXPORT56:RC4+RSA:+MEDIUM;
ssl_prefer_server_ciphers on;
index index.html index.htm index.php default.html default.htm default.php;
root /home/web/zhangge.net;
#有償服務(wù)付款頁(yè)面使用https訪(fǎng)問(wèn)
location /wp-content/plugins/alipay {
try_files $uri $uri/ /index.php?$args;
}
#若沒(méi)有匹配到wp-admin或wp-includes,則跳到http訪(fǎng)問(wèn)(反向邏輯:即只允許指定頁(yè)面開(kāi)啟https)
location / {
if ($request_uri !~* "wp-admin|wp-includes") {
rewrite (.*) http://zhangge.net$1 permanent;
}
}
location ~ /uploads/.*\.(php|php5)?$ {
deny all;
}
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 30d;
}
access_log /home/wwwlogs/zhangge.net.log access;
}
二、Apache
同樣,先確認Apache安裝時(shí)已添加SSL支持模塊。如果沒(méi)有請自行搜索搞定,本文不再贅述。
①、準備證書(shū)
Apache需要用到三個(gè)證書(shū)文件:
I. 根證書(shū):root_bundle.crt
II. 證書(shū)公鑰:yourdomain.com.crt
III. 證書(shū)私鑰:yourdomain.com.key
將下載好的三個(gè)證書(shū)文件,上傳到apache下的ssl目錄中(可自定義位置)。
②、修改配置
I. 編輯httpd.conf文件,取消以下內容的#注釋符號:
Shell
1
2
#LoadModule ssl_module modules/mod_ssl.so
#Include conf/extra/httpd-ssl.conf
II. 編輯http-ssl.conf文件,如下修改:
Shell
1
2
3
4
5
6
7
8
#找到如下行,并替換為證書(shū)公鑰的實(shí)際路徑:
SSLCertificateFile /usr/local/apache/ssl/public.cer
#找到如下行,并替換為證書(shū)私鑰的實(shí)際路徑:
SSLCertificateKeyFile /usr/local/apache/ssl/private.key
#找到如下行,取消行首注釋符,并替換為根證書(shū)實(shí)際路徑:
#SSLCertificateChainFile /usr/local/apache/ssl/ca.cer
III. 保存退出,并重啟Apache即可。
三、Tomcat
①、準備證書(shū)
Tomcat只需要用到一個(gè)jks格式的證書(shū)文件,比如yourdomain.com.jks。
拿到文件后,將其上傳到Tomcat下的conf目錄中。
②、修改配置
打開(kāi)conf目錄下的server.xml文件,找到以下內容:
Shell
1
2
3
4
5
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
去掉前后的注釋?zhuān)⑷缦滦薷模ɑ蛘咧苯悠浜筇砑右韵麓a亦可):
Shell
1
2
3
4
5
6
7
8
9
10
11
12
<Connector
port="443"
protocol="org.apache.coyote.http11.Http11Protocol"
SSLEnabled="true"
maxThreads="150"
scheme="https"
secure="true"
keystoreFile="conf\yourdomain.jks" <!-- 此處填寫(xiě)你上傳的證書(shū)的實(shí)際路徑 -->
keystorePass="password"
clientAuth="false"
sslProtocol="TLS"
/>
退出并保存,最后重啟Tomcat即可。
四、解決警告
如果網(wǎng)頁(yè)中存在不帶https的資源,比如http協(xié)議的js、css或圖片,那么訪(fǎng)問(wèn)這個(gè)https頁(yè)面,某些瀏覽器(比如IE)就會(huì )發(fā)出警告,提示頁(yè)面中存在不安全的內容,并且不會(huì )加載這些http協(xié)議的資源,導致頁(yè)面錯亂等問(wèn)題:
解決辦法:
方法①、使用相對地址
只要將這些http的資源鏈接,改為相對地址。比如原鏈接是<img src="http://yourdomain.com/images/demo.png" alt="Linux+Nginx/Apache/Tomcat新增SSL證書(shū),開(kāi)啟https訪(fǎng)問(wèn)教程">那么改成<img src="/images/demo.png" alt="Linux+Nginx/Apache/Tomcat新增SSL證書(shū),開(kāi)啟https訪(fǎng)問(wèn)教程">即可。
方法②、修改網(wǎng)站代碼
如果是全局https訪(fǎng)問(wèn),那么你將網(wǎng)站代碼中的鏈接均改為https好了。如果是http和https混合的,那么準備2套網(wǎng)站文件也行。然后在
nginx當中設置不同的root路徑。
為了省事,我推薦方法①。
好了,本文就寫(xiě)到這,希望能解您的燃眉之急!