在CentOS环境中使用Nginx和Spring Boot,同时配置Free SSL免费HTTPS证书并实现3个月自动续期,可以按照以下步骤进行:
申请 FreeSSL.cn 证书步骤如下 :
输入要申请的域名为范域名

配置验证方式为CNAME方式

DCV配置及验证

验证通过,选择安装脚本

在服务器执行脚本,生成证书文件

以下为服务器的部署安装说明
1. 安装Nginx:
sudo yum install nginx
2. 安装Java(如果未安装):
sudo yum install java
3. 部署Spring Boot应用:
确保你的Spring Boot应用已经打包成JAR文件,然后通过以下命令运行:
java -jar your-spring-boot-app.jar
4. 安装acme.sh:
curl https://get.acme.sh | sh -s email=my@example.com
5. 配置Nginx 80端口转向:
编辑Nginx配置文件,一般位于 /etc/nginx/nginx.conf 添加类似以下的配置:
server {
listen 80;
server_name ai-ziyuan.techwisdom.cn;
location / {
return 301 https://$host$request_uri;
}
}
域名配置了端口 80自动转向功能,用户输入
http://ai-ziyuan.techwisdom.cn 会自动转向
https://ai-ziyuan.techwisdom.cn
6. 获取SSL证书:
运行Certbot命令获取SSL证书:
acme.sh --issue -d *.techwisdom.cn --dns dns_dp --server https://acme.freessl.cn/v2/DV90/directory/xxxxxx
按照提示完成证书申请过程。
7. 配置SSL证书到Nginx:
继续编辑Nginx配置文件,添加SSL配置:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
#include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 100M;
server {
listen 443 ssl;
server_name ai-ziyuan.techwisdom.cn;
access_log /var/log/nginx/ai-ziyuan.techwisdom.cn.access.log;
error_log /var/log/nginx/ai-ziyuan.techwisdom.cn.error.log;
ssl_certificate /home/server/ssl/*.techwisdom.cn_ecc/*.techwisdom.cn.cer;
ssl_certificate_key /home/server/ssl/*.techwisdom.cn_ecc/*.techwisdom.cn.key;
ssl_session_cache shared:SSL:30m;
ssl_session_timeout 60m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8000;
}
}
}
8. 设置证书自动续期:
acme.sh已经默认会创建一个 cron job 来自动 renew 证书。你无需手动操作。
9. 重启Nginx:
sudo systemctl restart nginx
目前,你的Nginx将通过SSL连接到Spring Boot应用,并使用 FreeSSL.cn 免费SSL证书,而且证书将会在3个月后自动续期。确保定期检查Nginx和Certbot的日志以确保一切正常。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...
