vue3项目怎么部署到服务器并将前缀改为域名
时间: 2024-02-01 14:04:00 浏览: 70
部署 Vue3 项目到服务器可以使用以下步骤:
1. 在本地使用 `npm run build` 命令构建生产环境的代码。
2. 将生成的 `dist` 目录上传到服务器。
3. 安装 Node.js 和 Nginx。
4. 在服务器上创建一个 Nginx 配置文件,将域名指向 `dist` 目录。
5. 启动 Nginx 服务。
以下是一个简单的 Nginx 配置文件示例:
```
server {
listen 80;
server_name yourdomain.com;
location / {
root /path/to/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
}
```
其中,`yourdomain.com` 替换为你的域名,`/path/to/dist` 替换为 `dist` 目录的路径。
在配置文件中,`try_files` 选项用于在找不到文件时,重定向到 `index.html`,保证路由正常。如果你的项目使用了前缀,可以在 `router.js` 文件中设置 `base` 选项:
```
const router = createRouter({
history: createWebHistory(),
base: '/your-prefix/',
routes: [...]
})
```
然后在 Nginx 配置文件中,将 `location` 配置为:
```
location /your-prefix/ {
root /path/to/dist;
index index.html;
try_files $uri $uri/ /your-prefix/index.html;
}
```
重启 Nginx 服务后,你的 Vue3 项目就可以通过域名访问了。