最近關(guān)于 蘋果要求所有應(yīng)用到 2016 年底必須使用 HTTPS 出來后,好多服務(wù)端需要改版支持伯襟,不過這并不是什么難題握童,但是對于 好多人認(rèn)為 必須在 Nginx 和 Tomcat 兩邊同時配置 SSL 支持 的看法我十分不贊同姆怪,我認(rèn)為只需要在 nginx端配置證書即可。下面看下demo澡绩。
看一下圖解
還有一個誤解就是必須有域名才可以稽揭,其實(shí)ip也可以 并不需要域名。下面先給出ip的配置方式 再給出域名的配置方式肥卡。
ip方式:
利用openssl生成私鑰和公鑰
- yum install openssl(安裝openssl)
- yum install nginx (安裝nginx)
- nginx -V (查看nginx是否支持了OpenSSL 我的顯示已帶且支持)
- openssl genrsa -des3 -out ca.key 2048 (制作ca證書)
- openssl req -new -x509 -days 3650 -key ca.key -out ca.crt (制作ca證書)
- nginx 配置
upstream tomcat {
server localhost:8080 fail_timeout=0;
}
server {
listen 443 ssl;
server_name 112.126.90.18; # 客戶端直接用IP來訪問
#server_name jianzhike.haozhenjia.com; #客戶端通過域名來訪問
#root /usr/share/nginx/html;
#
ssl_certificate /home/soft/ssl/domain/ca.crt;
ssl_certificate_key /home/soft/ssl/domain/ca.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
proxy_connect_timeout 240;
proxy_send_timeout 240;
proxy_read_timeout 240;
# note, there is not SSL here! plain HTTP is used
proxy_pass http://tomcat;
}
#
error_page 404 /404.html;
location = /40x.html {
}
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
- Tomcat 配合的配置
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443"
proxyPort="443"/>
這樣啟動tomcat和nginx即可溪掀。
- 域名的配置
域名的配置是一樣的 首先生成證書的時候 serverdomain輸入域名
1 . openssl genrsa -des3 -out ca.key 2048
2 . openssl req -new -x509 -days 3650 -key ca.key -out ca.crt
- nginx 配置
upstream tomcat {
server localhost:8080 fail_timeout=0;
}
server {
listen 443 ssl;
#server_name 112.126.90.18; # 客戶端直接用IP來訪問
server_name jianzhike.haozhenjia.com; #客戶端通過域名來訪問
#root /usr/share/nginx/html;
#
ssl_certificate /home/soft/ssl/domain/ca.crt;
ssl_certificate_key /home/soft/ssl/domain/ca.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
proxy_connect_timeout 240;
proxy_send_timeout 240;
proxy_read_timeout 240;
# note, there is not SSL here! plain HTTP is used
proxy_pass http://tomcat;
}
#
error_page 404 /404.html;
location = /40x.html {
}
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
4 . Tomcat配置不變
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443"
proxyPort="443"/>
重啟tomcat和nginx即可 瀏覽器訪問的時候 由于是自簽名證書所以需要信任次證書
https域名測試地址