前后端代码分离的时候刷新页面404的问题

1.问题:

vue项目部署完成之后,突然发现除了首页,访问其它页面的时候刷新会出现404的问题,如果nginx这块没有配置好的话,便会出现这个问题。好了,现在说下解决方案,顺便也是给自己做个小的笔记。

2.解决:

在nginx配置里添加添加对应的跳转设置。

找到/www/server/panel/vhost/nginx/*.conf;

添加

  location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html =404;
    }

加#注释掉
index index.php index.html index.htm default.php default.htm default.html;

配置如下

server
{
    listen 80 default_server;
    server_name zhai.hui.com;
    # index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/zhai.hui.com;
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-74.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/zhai.hui.com.conf;
    #REWRITE-END
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log /dev/null;
        access_log /dev/null;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log /dev/null;
        access_log /dev/null; 
    }
    
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.html =404;
    }
    access_log  /www/wwwlogs/zhai.hui.com.log;
    error_log  /www/wwwlogs/zhai.hui.com.error.log;
}

 

posted @ 2021-09-29 17:54  代码沉思者  阅读(622)  评论(0编辑  收藏  举报