前端部署nginx刷新后404,解决Nginx刷新页面后404的问题

问题描述:

使用Nginx部署后,登录页面刷新一下就出来404,如下图:

刷新以后 ,页面变成404 Not Found

解决方案:

查看了一下nginx配置,出现问题的配置是这样的:

   server {
        listen       8088;
        server_name  localhost;

        location / {
            root   html/dist;
            index  index.html index.htm;
        }
        location /gateway/ {
             rewrite ^/gateway/(.*) /$1 break;
             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;
             proxy_connect_timeout 5;
             proxy_pass http://192.168.0.11:9000/;
    }

修改后的配置是这样的

    server {
        listen       8088;
        server_name  localhost;

        location / {
            root   html/dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
        location /gateway/ {
             rewrite ^/gateway/(.*) /$1 break;
             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;
             proxy_connect_timeout 5;
             proxy_pass http://192.168.0.11:9000/;
    }

添加了try_files $uri $uri/ /index.html,

然后重启一下nginx问题就解决了。

解释:

  • try_files 表示检查文件是否存在,返回第一个找到的文件,这里设置是index.html内部重定向。

另外,还有一种404报错的问题,可能是nginx访问文件权限问题,

打开nginx.conf,第一行默认是这样的

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

以下省略

可能是你访问的路径,需要root权限,而你启动nginx使用的普通用户,权限不足导致访问不到文件,所以可以这么修改:

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

以下省略
在 Vue 路由的 history 模式下,刷新页面会导致 404 错误的出现,这是因为浏览器会向服务器发送请求,但服务器并没有对应的路由,因此就会返回 404 错误。解决这个问题的方法有两种: 1. 配置后端服务器 如果你的 Vue 项目部署在 Apache 或 Nginx 等 Web 服务器上,你可以配置服务器来处理这个问题。具体做法是,在服务器上配置一个重定向规则,将所有的 URL 请求都重定向到 index.html 文件上。以 Apache 服务器为例,可以在 Apache 的配置文件(如 httpd.conf)中添加以下规则: ``` <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] </IfModule> ``` 这个配置将所有的 URL 请求都重定向到 index.html 文件上,这样即使刷新页面服务器也会返回正确的内容。 2. 使用 hash 模式 如果你不想在服务器上配置重定向规则,你可以考虑使用 hash 模式。在 hash 模式下,Vue 会使用 URL 中的 # 符号来表示路由,这样即使刷新页面,浏览器也只会重新加载 index.html 文件,而不会向服务器发送请求。要启用 hash 模式,只需要在 Vue 路由的配置中添加以下代码: ``` const router = new VueRouter({ mode: 'hash', routes: [...] }) ``` 这样就可以启用 hash 模式了。需要注意的是,hash 模式下,URL 中的 # 符号后面的内容不会被发送到服务器,因此你需要在前端代码中使用 JavaScript 来解析 URL 中的 hash 值,并根据这个值来渲染不同的页面。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

安逸的程序猿

意思不意思那是你的意思我没意思

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

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

打赏作者

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

抵扣说明:

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

余额充值