vue+SpringBoot部署在https
时间: 2025-05-03 21:46:59 浏览: 30
### Vue与SpringBoot项目部署到HTTPS环境的配置教程
为了将Vue与SpringBoot项目部署至HTTPS环境中,需要完成以下几个方面的配置:
#### 1. 获取SSL证书并安装
在生产环境下启用HTTPS协议前,需获取合法的SSL/TLS证书。可以通过Let's Encrypt免费申请SSL证书[^2]。
- 使用Certbot工具自动化生成和管理SSL证书:
```bash
sudo apt update && sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
```
上述命令会自动为指定域名生成SSL证书,并将其绑定到Nginx配置中[^4]。
#### 2. Nginx反向代理配置支持HTTPS
编辑Nginx配置文件 `/etc/nginx/sites-available/yourdomain` 或者 `/usr/local/nginx/conf/nginx.conf` ,添加以下内容以实现对SpringBoot后端API接口的支持以及强制跳转HTTPS的功能[^5]:
```nginx
server {
listen 80;
server_name yourdomain.com;
# 强制重定向HTTP请求到HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name yourdomain.com;
# SSL 配置
ssl_certificate /path/to/fullchain.pem;
ssl_certificate_key /path/to/privkey.pem;
location /api/ {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
root /var/www/html/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
```
此配置实现了两个功能:一是通过 `location /api/` 将所有匹配路径转发给本地运行的Spring Boot应用;二是通过 `try_files` 实现单页应用(SPA)路由回退机制^。
#### 3. 启动Spring Boot应用程序作为后台服务
确保Spring Boot程序已编译打包成可执行Jar包形式,在服务器上使用nohup指令启动它以便于长期稳定工作而不受SSH连接断开影响[^3]:
```bash
nohup java -jar springboot-app.jar &
```
#### 4. 测试访问站点
最后一步就是测试整个系统的连通性和安全性了。打开浏览器输入https://yourdomain.com验证页面能否正常加载并且地址栏显示锁形图标表示当前链接受到加密保护.
---
### 注意事项
- 如果遇到任何错误,请先查看日志文件定位问题所在位置。
- 定期更新或续订您的SSL证书以免过期造成网站不可用情况发生。
阅读全文
相关推荐



















