当前位置: 代码网 > 服务器>服务器>Linux > Nginx配置出现访问白屏问题的原因与解决

Nginx配置出现访问白屏问题的原因与解决

2025年02月18日 Linux 我要评论
问题复显当跳转 http://xxxxx/your/path 时 显示白屏但正常访问http://xxxxx 路径显示正常由于vue开发的为spa应用。打包后一个dist文件distcss文件html

问题复显

当跳转 http://xxxxx/your/path 时 显示白屏

但正常访问http://xxxxx 路径显示正常

由于vue开发的为spa应用。打包后一个dist文件

  • dist
    • css文件
    • html文件
    • static文件夹

后在nginx会进行一次配置

server {
    	listen 80;

    	location /apiset/ { //接口请求
            proxy_pass http://x x x x:3000/apiset/;
            proxy_set_header host $host:$server_port;
            proxy_set_header x-real-ip $remote_addr;
            proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
        }
       
    	location / {
      	    root /your/html/path;
            try_files $uri $uri/ /index.html;
            add_header cache-control "no-cache, no-store, must-revalidate";
        	add_header pragma "no-cache";
        	add_header expires 0;
            ;
        }
        
    }

问题原因

当访问http://xxxxx/your/path 时返回一个html文件,在根据内部的link,script标签进行对css js的访问,问题出在这块

<!doctype html>
<html lang="en">
  <head>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="icon" href="./static/favicon.ico" rel="external nofollow"  />
    <link rel="stylesheet" href="./static/app-loading.css" rel="external nofollow"  />
    <title>xxxx</title>
 
    
  </head>
  <body>
    <div id="app">
      <div id="app-loading"></div>
    </div>
    
    
  </body>
</html>

此时html上面访问的css路径为相对路径,在vite配置内部是base属性,base属性为‘’

当访问的路径为 http://xxxxx/your/path

<link rel="icon" href="./static/favicon.ico" />

解读为

<link rel="icon" href="http://xxxxx/your/static/favicon.ico" /> 

因为打包结构不存在‘your’这个文件夹导致找不到文件触发 try_files uriuri uriuri/ /index.html 规则把应该获取css/js文件变为返回html文件导致白屏问题。

解决

将内部相对路径修改为绝对路径,或者nginx配置重写

vite
export default (_configenv: configenv): userconfigexport => {
 

  return {
    base: "/",
    。。。
 
  }
}

nginx

server {
    	listen 80;

    	location /apiset/ { //接口请求
            proxy_pass http://x x x x:3000/apiset/;
            proxy_set_header host $host:$server_port;
            proxy_set_header x-real-ip $remote_addr;
            proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
        }
       
    	location / {
      	    root /your/html/path;
            try_files $uri $uri/ /index.html;
            add_header cache-control "no-cache, no-store, must-revalidate";
            add_header pragma "no-cache";
            add_header expires 0;
            rewrite ^/your/(.*)$ /$1 break; //重写your路径
        }
        
    }

到此这篇关于nginx配置出现访问白屏问题的原因与解决的文章就介绍到这了,更多相关nginx配置访问白屏内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com