Java笔记··By/蜜汁炒酸奶

nginx+tomcat配置ssl实现https

前言

本文主体为单向认证的配置方式,生成证书的方式放于最后附录里面。 实例中tomcat版本为tomcat9。

纯tomcat篇

仅为tomcat时,进入tomcat目录/conf/server.xml中,添加如下代码(具体参数请根据实际情况修改),并重启tomcat即可。若使用自定义的证书查看时使用ie为佳,chrome会直接屏蔽域名访问的链接(提示"此网站无法提供安全连接",以致纠结好长时间以为自己配置失败了呢= =),ip访问一般都会提示非安全链接,点击忽略继续就好。 [toggle hide=“no” title=“关键代码” color=“red”]

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true"  
       maxThreads="150" scheme="https" secure="true"  
       clientAuth="false" sslProtocol="TLS"   
       keystoreFile="C:\00Work\01Programming\01Java\JavaEE\localhost.keystore" keystorePass="123456"/>
1
2
3
4

[/toggle] clientAuth     false(单向认证)/true(双向认证) **keystoreFile   ** 证书所在目录 **keystorePass   ** 创建证书“keystore”的密码 nginx+tomcat配置ssl实现https

nginx+tomcat篇

nginx中在conf目录下的 nginx.conf文件中添加如下代码,tomcat目录/conf/server.xml中设置好相应domain即可。 [toggle hide=“no” title=“关键代码” color=“red”]

server {
      listen 443; 
      server_name domain;#要访问https的域名
      ssl on;
      root html;
      index index.html index.htm;
      ssl_certificate   C:/zhonya/keytool/ssl/domain.crt;
      ssl_certificate_key  C:/zhonya/keytool/ssl/domain.key;
      ssl_session_timeout 5m;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
      ssl_prefer_server_ciphers on;
      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://domain

		proxy_set_header Upgrade $http_upgrade;  
		proxy_set_header Connection "upgrade";
      }
    }
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

[/toggle]

附录

Java的keytool生成证书

方法一:

1、CMD命令

keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore "D:\wind\tomcat\keytool\tomcat.keystore"   -validity 36500
1

2.输入密码等相应内容 此处暂时不再过多叙述,具体的可查看:

http://lixor.iteye.com/blog/1532655

方法二:

keytool -genkeypair -alias "server"  -keyalg "RSA" -keysize 1024 -keypass "windtomcat" -keystore D:\wind\tomcat\keytool\server.jks -storepass windtomcat -validity 36500 -dname "CN=域名1,CN=域名2,CN=域名3,OU=wind,O=SJ,L=test,ST=HB,C=CN"
1

据说这种方法可以生成多域名证书,由于当初在chrome中测试始终出现错误提示(本文开始时说的情况),最终没做过多验证。 参考地址:

http://www.xuebuyuan.com/371801.html

预览
Loading comments...
0 条评论

暂无数据

example
预览