五、Nginx|Tomcat 配置 https


一、http & https

  • http:
  1. 是互联网上应用最为广泛的一种 网络协议。
  2. 是一个 客户端 和 服务端 请求和应答的标准(TCP)。
  3. 用于从 WWW服务器 传输超文本到本地浏览器的传输协议。
  4. 它可以使浏览器更加高效,使网络传输减少。

  • https:
  1. 是以安全为目标的 http通道,简单讲是 http的安全版,即 http下加入 SSL层。
  2. https 的安全基础是 SSL,因此加密的详细内容就需要 SSL。
  • https协议 的主要作用可以分为两种:
  1. 一种是建立一个信息安全通道,来保证数据传输的安全。
  2. 另一种就是确认网站的真实性。

1. http & https 区别

  1. https协议 需要到 申请ca证书,一般免费证书较少,因而需要一定费用。
  2. http 是超文本传输协议,信息是明文传输,https 则是具有安全性的 ssl加密 传输协议。
  3. http 和 https 使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。
  4. http 的连接很简单,是无状态的。
  5. https协议 是由 http协议+ssl 构建的可进行加密传输、身份认证的网络协议,比 http协议安全。

2. Nginx 配置 https 前置条件

  1. 服务器上已经安装 nginx服务,已经具备 http访问。
  2. nginx服务器 是否已经安装 ssl模块的支持。
  3. 是否已经拥有 ssl证书,如果没有可以去阿里云或者腾讯云免费申请。
# 检查`--with-http_ssl_module`模块
nginx -V

在这里插入图片描述


3. 安装 http_ssl_module模块

# 1、安装`http_ssl_module`模块
cd /usr/local/nginx-1.9.9

# 2、configure
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# 可能需要的依赖包
#yum -y install pcre-devel openssl openssl-devel

# 3、make
make

# 4、备份原有nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

# 5、将刚刚编译好的nginx,覆盖掉原有的nginx(nginx需要停止)
cp ./objs/nginx /usr/local/nginx/sbin/

# 7、查看安装情况
/usr/local/nginx/sbin/nginx -V

在这里插入图片描述


二、免费 SSL证书


1. 生成 CSR文件


2. 生成 SSL证书

  • SSL证书生成
    把 www.qs.com_csr.txt 转为 www.qs.com_ssl.crt直接修改后缀无效)。
    在这里插入图片描述
    在这里插入图片描述

# Windows:`www.qs.com_ssl.crt`
# Linux:`www.qs.com_ssl.pem`
sudo openssl x509 -in 'www.qs.com_ssl.crt' -out 'www.qs.com_ssl.pem' -outform PEM

3. Nginx 配置

  • 把 www.qs.com_ssl.crt 改为 qs.crt
  • 把 www.qs.com_key.txt 改为 qs.key
    在这里插入图片描述
server {
	listen       80;
	server_name  qs.com;

	# 将请求转成https
	# rewrite ^(.*) https://$host$1 permanent;
	rewrite ^(.*)$ https://${server_name}$1 permanent;
}

server {
	listen       443 ssl;
	server_name  qs.com;
	# charset utf-8;
	
	# 启用ssl功能(最新版本不需要配置,默认开启)
	ssl on;
	# 为虚拟主机指定pem格式的证书文件(Windows:qs.crt、Linux:qs.pem)
	ssl_certificate      E:/java/nginx/nginx-1.22.0/conf/ssl/qs.crt;
   	# ssl_certificate      /usr/local/nginx/conf/ssl/qs.pem;
	# 为虚拟主机指定私钥文件
	ssl_certificate_key  E:/java/nginx/nginx-1.22.0/conf/ssl/qs.key;
	# 客户端能够重复使用存储在缓存中的会话参数时间  
	ssl_session_timeout  5m;  
	# 指定使用的ssl协议   
	ssl_protocols  SSLv3 TLSv1 TLSv1.1 TLSv1.2;   
	# 指定许可的密码描述  
	ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;   
	# ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
	# SSLv3和TLSv1协议的服务器密码需求优先级高于客户端密码  
	ssl_prefer_server_ciphers   on;
	
	ssl_session_cache    shared:SSL:1m;
	# 如果是全站 HTTPS 并且不考虑 HTTP 的话,可以加入 HSTS 告诉你的浏览器本网站全站加密,并且强制用 HTTPS 访问
	fastcgi_param  HTTPS        on;
	fastcgi_param  HTTP_SCHEME  https;

	location / {
		root   html;
		index  index.html index.htm;
	}

	# error_page   500 502 503 504  /50x.html;
	# location = /50x.html {
	#     root   html;
	# }
	# error_page 497 404 https://$host$uri?$args;
}

# 检查配置文件正确性
nginx -t

# Nginx 重新加载
nginx -s reload  

在这里插入图片描述


5. 访问测试


6. ssl_client_certificate

  • ssl_client_certificate证书 不需要和 server证书 属于同一个CA根证书:
  1. server证书 用于 SSL服务器端认证,在生产环境一定要使用 CA机构颁发的证书。
  2. ssl_client_certificate配置 的是客户端认证。
ssl_client_certificate ca.pem;
ssl_verify_client on

  • 使用 curl测试部署:
  1. 如果客户端证书需要密码,则需要使用冒号在 crt文件 后添加密码。
curl -v -s -k --key /etc/nginx/certs/client.key --cert /etc/nginx/certs/client.crt:12345 https://example.com

三、openssl 生成证书

  • openssl 是目前最流行的 SSL密码库工具:
  1. 其提供了一个通用、健壮、功能完备的工具套件,用以支持 SSL/TLS协议 的实现。
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/ssl/qs.key -out /usr/local/ssl/qs.crt

------------------------------
#openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/ssl/qs.key -out /usr/local/ssl/qs.crt
Generating a 2048 bit RSA private key
...............................................................................+    ++
...............+++
writing new private key to '/usr/local/ssl/qs.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shanghai
Locality Name (eg, city) [Default City]:shanghai
Organization Name (eg, company) [Default Company Ltd]:qs
Organizational Unit Name (eg, section) []:xxxx
Common Name (eg, your name or your server's hostname) []:qs.com
Email Address []:443749091@qq.com
------------------------------

#ll
total 8
-rw-r--r--. 1 root root 1391 Apr 21 13:29 qs.crt
-rw-r--r--. 1 root root 1704 Apr 21 13:29 qs.key

四、阿里云 SSL证书申请

在这里插入图片描述


五、Tomcat 配置 https


1. cer|crt证书 + key文件——生成 keystore文件

  • 因为 Tomcat 配置 https,需要将证书转成 keystore文件。
  1. 先通过 openssl 将 cer证书 和 私钥文件 生成 p12证书。
  2. 再通过 Java的keytool 生成 keystore文件。
# 1、先通过 openssl 将 cer证书 和 私钥文件 生成 p12证书。
openssl pkcs12 -export -in './qs.cer' -inkey './qs.key' -out './qs.p12'

# 2、再通过 Java的keytool 生成 keystore文件。
# keytool -importkeystore -v -srckeystore '*.p12' -srcstoretype pkcs12 -srcstorepass '*.p12生产的密码' -destkeystore '*.keystore' -deststoretype jks -deststorepass '*.keystore密钥库的密码'
keytool -importkeystore -v -srckeystore './qs.p12' -srcstoretype pkcs12 -srcstorepass '123456' -destkeystore './qs.keystore' -deststoretype jks -deststorepass '123456'
  • 注意:*.p12文件 的密码 需要跟 *.keystore文件 的密码一致,可能会报错。

# crt证书。
openssl pkcs12 -export -in './qs.crt' -inkey './qs.key' -out './qs.p12' -name 'tomcat' -CAfile './qs.crt' -caname root -chain

keytool -importkeystore -v -srckeystore './qs.p12' -srcstoretype pkcs12 -srcstorepass '*.p12生产的密码' -destkeystore '*.keystore' -deststoretype jks -deststorepass '*.keystore密钥库的密码' 

2. 配置 Tomcat

  • $TOMCAT_HOME/conf/server.xml
<Executor name="tomcatThreadPoolHttps" namePrefix="catalina-exec-https-" 
	maxThreads="6000" minSpareThreads="100" maxIdleTime="120000" />

<Connector executor="tomcatThreadPoolHttps" port="8443" redirctPort="6064" 
	protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true" 
	maxHttpHeaderSize="8192" acceptCount="1000" enableLookups="false" 
	scheme="https" secure="true" keystoreFile="/root/qs.keystore" keystorePass="123456" 
	clientAuth="false" sslProtocol="TLSv1.2"/>

六、东方通 配置 https

  • $TongWeb/conf/tongweb.xml
<http-listener name="system-http-listener" port="9060" status="started" address="0.0.0.0" 
	io-mode="nio2" http2-enabled="false" ssl-enabled="false" redirect-port="443" 
	uri-encoding="GBK" use-body-encoding-for-uri="false" 
	max-parameter-count="10000" max-post-size="2097152" max-swallow-size="2097152" 
	parse-body-methods="POST,PUT,DELETE" default-virtual-host="admin" 
	create-time="2019-10-29 10:54:11">
	
	<ssl openssl-enabled="false" gm-enabled="false" 
		keystore-file="conf/server.keystore" keystore-pass="vVm6OyRRxhs3QGlTnzcKGA==" 
		keystore-type="JKS" gm-enc-file="conf/EncryptionKey.p12" client-auth="false" 
		ssl-protocol="TLS" truststore-file="conf/server.keystore" truststore-type="JKS"/>
	
	<protocol not-allow-HTTP-methods="TRACE,OPTIONS,HEAD,CONNECT,DELETE" 
		async-timeout="10000" enable-lookups="false" max-header-count="100" 
		use-ipv-hosts="false" xpowered-by="false" backlog="100" accept-thread-count="1"
		connection-timeout="60000" keep-alive-timeout="60000" max-threads="200" min-spare-threads="10" 
		processor-cache="200" tcp-no-delay="true" max-connections="10000" self-tuned="false">
		<property name="threadPriority" value="5"/>
	</protocol>
	
	<http-options compression="on" compressable-mime-type="text/html,text/plain,text/xml" 
		compression-min-size="2048" no-compression-user-agents="" disable-upload-timeout="true" 
		max-http-header-size="8192" max-keep-alive-requests="100"/>
	
	<advance disable-keep-alive-percentage="75" selector-timeout="1000" usecomet="true" 
		use-sendfile="true" oom-parachute="1048576"/>
	<property name="server" value="webserver"/>
</http-listener>

<!-- http配置。-->
<http-listener name="tong-http-listener" port="8080" io-mode="nio2" redirect-port="8443" 
	uri-encoding="GBK" parse-body-methods="POST" default-virtual-host="server" 
	create-time="2019-10-29 10:54:11">
	<ssl/>
	<protocol not-allow-HTTP-methods="TRACE,OPTIONS,HEAD,CONNECT,DELETE,PUT" 
		max-threads="200" min-spare-threads="10"/>
	<http-options compression="off"/>
	<advance/>
	<property name="server" value="webserver"/>
</http-listener>

------------------------------------------------------------------------------------------
<!-- https配置。-->
<http-listener name="tong-http-listener" port="8080" status="started" address="0.0.0.0" 
	io-mode="nio2" http2-enabled="false" ssl-enabled="true" redirect-port="443" 
	uri-encoding="GBK" use-body-encoding-for-uri="false" 
	max-parameter-count="10000" max-post-size="2097152" max-swallow-size="2097152" 
	parse-body-methods="POST" default-virtual-host="server" 
	create-time="2019-10-29 10:54:11">
	
	<ssl openssl-enabled="false" gm-enabled="false" 
		keystore-file="conf/qs.keystore" keystore-pass="75YIpThjTTFSD/xkeTd4+Q==" 
		keystore-type="JKS" gm-enc-file="conf/EncryptionKey.p12" client-auth="false" 
		ssl-protocol="TLS" truststore-file="conf/server.keystore" truststore-type="JKS"/>
		
	<protocol not-allow-HTTP-methods="TRACE,OPTIONS,HEAD,CONNECT,DELETE,PUT" 
		async-timeout="10000" enable-lookups="false" max-header-count="100" 
		use-ipv-hosts="false" xpowered-by="false" backlog="100" accept-thread-count="1" 
		connection-timeout="60000" keep-alive-timeout="60000" max-threads="200" min-spare-threads="10" 
		processor-cache="200" tcp-no-delay="true" max-connections="10000" self-tuned="false">
		<property name="threadPriority" value="5"/>
	</protocol>
	
	<http-options compression="off" compressable-mime-type="text/html,text/xml,text/plain" 
		compression-min-size="2048" disable-upload-timeout="true" 
		max-http-header-size="8192" max-keep-alive-requests="100"/>
		
	<advance disable-keep-alive-percentage="75" selector-timeout="1000" usecomet="true" 
		use-sendfile="true" oom-parachute="1048576"/>
	<property name="enableRC4" value="false"/>
	<property name="server" value="webserver"/>
</http-listener>

<http-listener name="ejb-server-listener" port="5100" uri-encoding="GBK" parse-body-methods="POST" 
	default-virtual-host="server" create-time="2019-10-29 10:54:12">
	<ssl/>
	<protocol not-allow-HTTP-methods="TRACE,OPTIONS,HEAD,CONNECT,DELETE,PUT" 
		max-threads="200" min-spare-threads="10"/>
	<http-options/>
	<advance/>
</http-listener>

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骑士梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值