Nginx部署Vue项目动态路由刷新404

3 篇文章 0 订阅

前言

  • 记录下Nginx部署Vue项目刷新404的解决方案,遇到了两次,route用的是history路由模式,动态路由:
{
  path: '/article/:blogId',
  name: 'blog',
  component: blog
}
  • 然后部署nginx
location / {
	root   /usr/local/nginx/html/blog/;
    index  index.html index.htm;
}
  • 然后刷新页面

在这里插入图片描述


  • what happend?

在这里插入图片描述

  • 后请教了度娘,度娘回答
 location / {
    root   /usr/local/blog/;
    index  index.html index.htm;
    try_files  $uri $uri/ /index.html;
}
  • 再然后

在这里插入图片描述


  • what happend?

在这里插入图片描述
在这里插入图片描述

好吧,记录下两次的解决方案,先行感谢两位大佬给的启发


第一次

网站没有申请二级域名,部署了多项目,所以想的是添加项目前缀'/blog'访问,比如这个:

https://www.coisini.club/blog
  • router.js
mode: 'history',
routes: [
  {
    path: '/blog', // 博客首页
    component: home,
    children: [
      {
        path: '/',
        component: index
      },
      {
        path: '/blog/article/:blogId',
        name: 'blog',
        component: blog
      },
     ....
  • build.js
出错打包配置
assetsPublicPath: './',

正确打包配置
assetsPublicPath: '/blog/',
  • 就是这个assetsPublicPath资源路径忘记配置惹了N多麻烦, 留下了不学无数的眼泪,如果有遇到同样场景的同学记得核实

在这里插入图片描述

  • nginx.conf
有两种配置方式,均验证有效
方式一:
location /blog {
    root   /usr/local/nginx/html/;
    index  index.html index.htm;
    try_files  $uri $uri/ /blog/index.html;
}

方式二:
location /blog {
    root   /usr/local/nginx/html/;
    index  index.html index.htm;
    try_files $uri $uri/ @rewrites;
}

location @rewrites {
    rewrite ^/(blog)/(.+)$ /$1/index.html last;
}
  • LATER

在这里插入图片描述


第二次

网站申请了二级域名,之前的配置都要修改了,比如这样:

https://blog.coisini.club/
  • router.js
mode: 'history',
routes: [
  {
    path: '/',
    component: index
  },
  {
    path: '/article/:blogId',
    name: 'blog',
    component: blog
  },
  ....
  • build.js
assetsPublicPath: './',
  • nginx.conf
server {
    listen       443 ssl;
    server_name  blog.coisini.club;
    
    location / {
        root   /usr/local/blog/;
        index  index.html index.htm;
    }
}
  • 然后部署

在这里插入图片描述
在这里插入图片描述

  • 然后照例请教度娘,度娘说try_files $uri $uri/ /index.html;
 location / {
    root   /usr/local/blog/;
    index  index.html index.htm;
    try_files  $uri $uri/ /index.html;
}
  • 然后

在这里插入图片描述

在这里插入图片描述

  • 然后,看了无数篇一毛一样的博客后,找到了这位空虚公子vue部署在nginx后刷新404,虽然你肾虚,但我还是
    在这里插入图片描述

  • 正确的写法:

  • build.js

assetsPublicPath: '/',
  • nginx.conf
location / {
    root   /usr/local/blog/;
    index  index.html index.htm;
    try_files  $uri $uri/ /index.html =404;
    autoindex  on;
}
  • LATER

在这里插入图片描述



- End -
- 个人笔记 -
- 仅供参考-

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
当使用nginx部署vue项目时,刷新页面或访问路由配置页面时可能会出现404错误。这是因为vue项目属于单页面开发,只有一个index.html文件。解决这个问题很简单,只需要将访问重定向到index.html页面,然后由index.html处理对应的路由跳转。可以通过修改nginx的配置文件来实现。首先,在nginx的配置文件中添加以下代码: ``` location / { try_files $uri $uri/ /index.html; } ``` 这样就可以解决刷新页面时出现404错误的问题。然后,重启nginx服务器使配置生效。这样就完成了nginx部署vue项目并解决刷新404的问题。\[1\]\[3\] #### 引用[.reference_title] - *1* [nginx部署vue项目刷新页面404](https://blog.csdn.net/qq_45029459/article/details/129014641)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Nginx部署Vue项目以及解决刷新页面404问题](https://blog.csdn.net/qq_27985607/article/details/86608392)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [五步用Nginx部署Vue项目动态路由刷新404问题解决](https://blog.csdn.net/weixin_54218079/article/details/125755795)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maggieq8324

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值