letsencrypt+nginx+tomcat 免费https搭建
Posted 喔易
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了letsencrypt+nginx+tomcat 免费https搭建相关的知识,希望对你有一定的参考价值。
一、nginx安装
参考:https://blog.csdn.net/luckyzsion/article/details/76473039
二、安装certbot工具
yum install -y epel-release
yum install -y certbot
三、初次申请证书
1、配置nginx,主要新增location ~ /.well-known/acme-challenge,获取证书时,需要验证域名有效性。修改完nginx.conf后,执行命令nginx -s reload
server {
listen 80;
server_name 119.23.24.173;
location / {
proxy_pass http://servers2.mydomain.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /.well-known/acme-challenge {
root /usr/local/nginx/html;
allow all;
}
}
2、获取证书
certbot certonly --webroot -w /usr/local/nginx/html -d www.xxx.com -m xx@qq.com --agree-tos
获取成功后,证书保存位置:
/etc/letsencrypt/live/www.xxx.com/
查看证书有效期
openssl x509 -noout -dates -in /etc/letsencrypt/live/www.xxx.com/fullchain.pem
四、配置nginx、tomcat
服务器部署采用nginx作为反向代理、或负载均衡,外面访问采用https,nginx访问tomcat采用http,这样好处是tomcat不需要配置证书,只需要nginx配置即可。
1、nginx证书配置,新增以下内容。
upstream tomcat {
server 127.0.0.1:8080;
}
server {
listen 443 ssl;
server_name www.xxx.com;
ssl_certificate /etc/letsencrypt/live/www.xxx.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.xxx.com/privkey.pem;
ssl on;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
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;
proxy_pass http://tomcat;
}
}
重启时可能报错,提示https错误,请查看附录1
2、tomcat主要配置如下,proxyPort配置为nginx https所监听的端口号
<Connector connectionTimeout="20000" port="8080" executor="tomcatThreadPool" acceptCount="600" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443" scheme="https" proxyPort="443"/>
五、定时更新证书
执行命令 crontab -e
0 00 01 * * certbot renew --quiet --pre-hook "/usr/local/nginx/sbin/nginx -s stop" --post-hook "/usr/local/nginx/sbin/nginx"
每月1号0点更新 --quiet不返回信息
附录1 Nginx如果未开启SSL模块,配置Https时提示错误
原文:https://www.cnblogs.com/ghjbk/p/6744131.html
原因也很简单,nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了,但是现在的情况是我的nginx已经安装过了,怎么添加模块,其实也很简单,往下看:做个说明:我的nginx的安装目录是/usr/local/nginx这个目录,我的源码包在/usr/local/src/nginx-1.6.2目录
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37
1.2 Nginx开启SSL模块
切换到源码包:
cd /usr/local/src/nginx-1.11.3
查看nginx原有的模块
/usr/local/nginx/sbin/nginx -V
在configure arguments:后面显示的原有的configure参数如下:
--prefix=/usr/local/nginx --with-http_stub_status_module
那么我们的新配置信息就应该这样写:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
运行上面的命令即可,等配置完
配置完成后,运行命令
make
这里不要进行make install,否则就是覆盖安装
然后备份原有已安装好的nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)
cp ./objs/nginx /usr/local/nginx/sbin/
然后启动nginx,仍可以通过命令查看是否已经加入成功
/usr/local/nginx/sbin/nginx -V
附录2 cer或者pem转换为jks证书,tomcat配置证书
原文:https://www.iyunw.cn/archives/cer-huo-zhe-pem-zhuan-huan-wei-jks-zheng-shu-tomcat-pei-zhi-zheng-shu/
1. 默认pem证书转换为Tomcat需要的jks
①pem或者cer文件转换为pfx文件,会让设置密码我这里设置的全部为password
openssl pkcs12 -export -out server.pfx -inkey server.key -in server.pem
openssl pkcs12 -export -out server.pfx -inkey server.key -in server.pem
②pfx文件转换为jks文件
keytool -importkeystore -srckeystore server.pfx -destkeystore server.jks -srcstoretype PKCS12 -deststoretype JKS
keytool -importkeystore -srckeystore server.pfx -destkeystore server.jks -srcstoretype PKCS12 -deststoretype JKS
2. 配置Tomcat配置文件server.xml文件,会让设置密码我这里设置的全部为password
Shell
<Connector protocol="org.apache.coyote.http11.Http11NioProtocol"
port="8443" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="/root/key/server.jks" keystorePass="password"
clientAuth="false" sslProtocol="TLS"
ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256" />
<Connector protocol="org.apache.coyote.http11.Http11NioProtocol"
port="8443" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="/root/key/server.jks" keystorePass="password"
clientAuth="false" sslProtocol="TLS"
ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256" />
3.重启Tomcat
附录3:springboot 2.0配置内置tomcat
原文:https://blog.csdn.net/wd2014610/article/details/79587161
1、之前老的版本TomcatEmbeddedServletContainerFactory取的是这个类
2、在SpringBoot 2.0.0框架中,已经没有类TomcatEmbeddedServletContainerFactory了
3、在老版本的Tomcat配置中,构造tomcatFactory的bean
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory();
tomcatFactory.addConnectorCustomizers(new GwsTomcatConnectionCustomizer());
return tomcatFactory;
}
4、那么早SpringBoot 2.0.0中该怎么构建呢?
去到SpringBoot官方文档这里写链接内容、找到Tomcat配置
5、最新的已经有了全新的类了
6、事例
7、那么就可以用全新的ServletWebServerFactory类来构造Tomcat的配置了
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addConnectorCustomizers(new GwsTomcatConnectionCustomizer());
return tomcat;
}
8、最后附上全新的Tomcat配置
package com.gws.configuration;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
/**
* 使用tomcat配置
*
* @version
* @author
*
*/
@Configuration
public class TomcatConfig {
@Value("${spring.server.port}")
private String port;
@Value("${spring.server.acceptorThreadCount}")
private String acceptorThreadCount;
@Value("${spring.server.minSpareThreads}")
private String minSpareThreads;
@Value("${spring.server.maxSpareThreads}")
private String maxSpareThreads;
@Value("${spring.server.maxThreads}")
private String maxThreads;
@Value("${spring.server.maxConnections}")
private String maxConnections;
@Value("${spring.server.protocol}")
private String protocol;
@Value("${spring.server.redirectPort}")
private String redirectPort;
@Value("${spring.server.compression}")
private String compression;
@Value("${spring.server.connectionTimeout}")
private String connectionTimeout;
@Value("${spring.server.MaxFileSize}")
private String MaxFileSize;
@Value("${spring.server.MaxRequestSize}")
private String MaxRequestSize;
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addConnectorCustomizers(new GwsTomcatConnectionCustomizer());
return tomcat;
}
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 单个数据大小
factory.setMaxFileSize(MaxFileSize); // KB,MB
/// 总上传数据大小
factory.setMaxRequestSize(MaxRequestSize);
return factory.createMultipartConfig();
}
/**
*
* 默认http连接
*
* @version
* @author liuyi 2016年7月20日 下午7:59:41
*
*/
public class GwsTomcatConnectionCustomizer implements TomcatConnectorCustomizer {
public GwsTomcatConnectionCustomizer() {
}
@Override
public void customize(Connector connector) {
connector.setPort(Integer.valueOf(port));
connector.setAttribute("connectionTimeout", connectionTimeout);
connector.setAttribute("acceptorThreadCount", acceptorThreadCount);
connector.setAttribute("minSpareThreads", minSpareThreads);
connector.setAttribute("maxSpareThreads", maxSpareThreads);
connector.setAttribute("maxThreads", maxThreads);
connector.setAttribute("maxConnections", maxConnections);
connector.setAttribute("protocol", protocol);
connector.setAttribute("redirectPort", "redirectPort");
connector.setAttribute("compression", "compression");
connector.setAttribute("scheme", "https");
connector.setAttribute("proxyPort", 9092);
}
}
}
9、最后在application.properties,进行配置
#嵌入tomcat配置
spring.server.port=8095
#和CPU数
spring.server.acceptorThreadCount=4
spring.server.minSpareThreads=50
spring.server.maxSpareThreads=50
spring.server.maxThreads=1000
spring.server.maxConnections=10000
#10秒超时
spring.server.connectionTimeout=10000
spring.server.protocol=org.apache.coyote.http11.Http11Nio2Protocol
spring.server.redirectPort=443
spring.server.compression=on
#文件请求大小
spring.server.MaxFileSize=300MB
spring.server.MaxRequestSize=500MB
以上是关于letsencrypt+nginx+tomcat 免费https搭建的主要内容,如果未能解决你的问题,请参考以下文章
[web建站]-简单通过letsencrypt配置HTTPS证书
letsencrypt,nginx,aws和nodejs无法设置
sh 用于通过letsencrypt的docker镜像更新docker nginx代理中的letsencrypt证书的模板