最近关于 苹果要求所有应用到 2016 年底必须使用 HTTPS 出来后,好多服务端需要改版支持,不过这并不是什么难题,但是对于 好多人认为 必须在 Nginx 和 Tomcat 两边同时配置 SSL 支持 的看法我十分不赞同,我认为只需要在 nginx端配置证书即可。下面看下demo。
看一下图解
还有一个误解就是必须有域名才可以,其实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域名测试地址