Nginx 配置vue项目

    worker_processes  1;
    events {
        worker_connections  1024;
    }

    http {
        # 必须要导入不然样式nginx加载不出来样式
        include   mime.types;
        # 必须要导入不然样式nginx加载不出来样式
        default_type  application/octet-stream;
     
       # sendfile        on;
       # keepalive_timeout  65;
     
      server {
          listen       1100;
          server_name  localhost;
          # vue 存放位置
          root /usr/vuejs/nginx;
          # 指定首页
          index index.html;
          # 以下配置是防止刷新页面404。例如进入 http://localhost/test/index 页面。刷新页面出现404.加上下面到代码即可解决
          location / {
               try_files $uri $uri/ @router;
               index index.html;
           }

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

nginx 前缀转发

  • 报错 vue Uncaught SyntaxError: Unexpected token '<' 或css 出现 404

  • 配置以下内容即可

    nginx 配置
    server {
    listen 80;
    server_name your_domain.com;

    location /backend-management/ {
        alias /path/to/your/vue/app/backend-management/; # 必须知道项目路径
        try_files $uri $uri/ /backend-management/index.html; # 或者 try_files $uri $uri/ /index.html;
    }
    

    }

    vue项目重vue.config.js 配置
    module.exports = {
    publicPath: process.env.NODE_ENV === 'production'? '/backend-management/' : '/'
    }

posted @ 2025-04-09 11:48  程序员の奇妙冒险  阅读(19)  评论(0)    收藏  举报