分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

vue-route+webpack部署单页路由项目,访问刷新出现404问题

发布时间:2023-09-06 01:10责任编辑:赖小花关键词:webpack

问题描述:

前端使用vue-cli开发单页路由项目,放到nginx上访问的时候,刷新会出现404现象

服务端一开始的配置如下:

server { ???????listen ??????80; ???????server_name ?localhost; ???????#charset koi8-r; ???????#access_log ?logs/host.access.log ?main; ???????location / { ???????????root ??html; ???????????index ?index.html index.htm; ???????} ???????#error_page ?404 ?????????????/404.html; ???????# redirect server error pages to the static page /50x.html ???????# ???????error_page ??500 502 503 504 ?/50x.html; ???????location = /50x.html { ???????????root ??html; ???????} ???????# proxy the PHP scripts to Apache listening on 127.0.0.1:80 ???????# ???????#location ~ \.php$ { ???????# ???proxy_pass ??http://127.0.0.1; ???????#} ???????# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 ???????# ???????#location ~ \.php$ { ???????# ???root ??????????html; ???????# ???fastcgi_pass ??127.0.0.1:9000; ???????# ???fastcgi_index ?index.php; ???????# ???fastcgi_param ?SCRIPT_FILENAME ?/scripts$fastcgi_script_name; ???????# ???include ???????fastcgi_params; ???????#} ???????# deny access to .htaccess files, if Apache‘s document root ???????# concurs with nginx‘s one ???????# ???????#location ~ /\.ht { ???????# ???deny ?all; ???????#} ???}

解决方法:

在nginx配置里添加vue-route的跳转设置(这里首页是index.html,如果是index.php就在下面对应位置替换),正确配置如下:

server { ???????listen ??????80; ???????server_name ?localhost; ???????#charset koi8-r; ???????#access_log ?logs/host.access.log ?main; ???root C:\Users\vicky\Documents\primary\i-link-request\dist; ???index ?index.html index.htm; ???????????????location / { ????????????try_files $uri $uri/ @router; ????????????index index.html; ????????} ???????location @router { ???????????rewrite ^.*$ /index.html last; ???????} ???????#error_page ?404 ?????????????/404.html; ???????# redirect server error pages to the static page /50x.html ???????# ???????error_page ??500 502 503 504 ?/50x.html; ???????location = /50x.html { ???????????root ??html; ???????} ???????# proxy the PHP scripts to Apache listening on 127.0.0.1:80 ???????# ???????#location ~ \.php$ { ???????# ???proxy_pass ??http://127.0.0.1; ???????#} ???????# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 ???????# ???????#location ~ \.php$ { ???????# ???root ??????????html; ???????# ???fastcgi_pass ??127.0.0.1:9000; ???????# ???fastcgi_index ?index.php; ???????# ???fastcgi_param ?SCRIPT_FILENAME ?/scripts$fastcgi_script_name; ???????# ???include ???????fastcgi_params; ???????#} ???????# deny access to .htaccess files, if Apache‘s document root ???????# concurs with nginx‘s one ???????# ???????#location ~ /\.ht { ???????# ???deny ?all; ???????#} ???}

修改的主要配置为:

location / { ?????try_files $uri $uri/ @router; ?????index index.html;}location @router { ?????rewrite ^.*$ /index.html last;}

重启nginx 之后就可以正常访问了

vue-route+webpack部署单页路由项目,访问刷新出现404问题

原文地址:http://www.cnblogs.com/rongjuan/p/7513271.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved