可以看看简书上的这篇文章写得比较详细《Let's Encrypt SSL证书配置》
文章中详细介绍了使用如何手动生成SSL证书。但是我个人觉得还是比较麻烦,下面我来介绍一个简单的生成SSL证书方案。
填写域名创建免费的SSL证书
下载文件上传到服务器验证并下载证书
文件上传目录
验证文件
配置好Nginx,点击第5步中的链接看看能不能访问到,如果访问正常就可以点击Download SSL Certificate
按钮下载证书了。
Nginx 主配置nginx.conf
如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
server_tokens off;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include conf.d/*.conf;
}
Nginx web配置web.conf
如下:
upstream tomcat {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /home/dev/logs/nginx/web.access.log main;
# error_page 500 502 503 504 /service/tomcat/nginx/504/504.html;
location /static/{
alias /home/dev/www/;
}
location ~/.well-known/{
add_header Content-Type text/plain;
allow all;
root /home/dev/www/;
}
location / {
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-For $remote_addr;
client_max_body_size 20m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 900;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_pass http://tomcat;
}
}
总结 1、相比较从服务器中命令行获取脚本部署方法简单很多,至少这边可以图形化操作,这样对于一般的用户不用担心是否影响服务器的安全和稳定性。 2、通过获取到的Let's Encrypt SSL证书,我们可以部署到虚拟主机、VPS、服务器中,根据各种需要的WEB环境自行部署。 3、因为Let's Encrypt证书90天有效期,我们在SSL FOR FREE注册过账户,所以到期前会提醒我们,我们需要根据提示重新续约时间和重新替换证书部署。
要让nginx支持多证书,nginx必须支持TLS SNI。可以使用如下命令查看
./sbin/nginx -V 或者 /usr/local/nginx/sbin/nginx -V
查看Nginx是否支持TLS SNI
如果显示TLS SNI support disabled
可以参考这篇文章进行配置
SSL FOR FREE
上下载的证书的zip
包含如下内容:
下载的证书
这里我们要将ca_bundle.crt
以及certificate.crt
整合到一个文件中cert_chain.crt
。
cat certificate.crt ca_bundle.crt >> cert_chain.crt
整合到一个文件需要手动处理一下换行,不然启动Nginx会出现"PEM_read_bio:bad end line" 的问题
合并文件需要换行
Nginx 配置SSLhttps.conf
如下:
1、指定域名80端口强制使用https 2、配置https监听
[root@localhost conf]# cat conf.d/https.conf
upstream tomcats {
server 127.0.0.1:8088;
}
server
{
listen 80;
#listen [::]:80;
server_name ngrok.javen205.1mfy.cn static.javen205.1mfy.cn frp.javen205.1mfy.cn ijpay.javen205.1mfy.cn;
return 301 https://$host$request_uri;
#rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443;
server_name ngrok.javen205.1mfy.cn static.javen205.1mfy.cn frp.javen205.1mfy.cn ijpay.javen205.1mfy.cn;
ssl on;
ssl_certificate /usr/local/nginx/conf/ssl/cert_chain.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/private.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
access_log /home/dev/logs/nginx/https.access.log main;
# error_page 500 502 503 504 /service/tomcat/nginx/504/504.html;
location /static/{
alias /home/dev/www/;
}
location ~/.well-known/{
add_header Content-Type text/plain;
allow all;
root /home/dev/www/;
}
location / {
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-For $remote_addr;
client_max_body_size 20m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 900;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_pass http://tomcats;
}
}
[root@localhost conf]#
配置Tomcat server.xml 的 Engine 模块下配置一个 Valve:
<Engine name="Catalina" defaultHost="localhost">
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="X-Forwarded-For"
protocolHeader="X-Forwarded-Proto"
protocolHeaderHttpsValue="https" httpsServerPort="8088"/> #非80端口时,必须增加httpsServerPort配置,不然request.getServerPort()方法返回 443.
</Engine>
在Tomcat的webapps/ROOT
目录下添加test.html
测试如下图:
image.png