关于nginx部署vue项目的两个问题

首先我使用的是后端接口+前端vue的形式,这样就涉及到跨域的问题。我是这样配置的:

server {
        listen       80;
        server_name  www.qdabc.cn;//访问网址

        location / {
                root  /var/www/VueApp;
                index  index.html index.htm;
        }
                //这里是配置的如果访问apis则是转到后端接口,这样就避免了跨域
                location /apis {
                        rewrite  ^/apis/(.*)$ /$1 break;
                        proxy_pass   http://127.0.0.1:3000/;
       }
}

然后还遇到一个问题:我在vue中使用的是vue-router跳转的,如果跳到二级菜单,刷新页面的话会出现404页面。这是因为在vue中使用的是js渲染的虚拟目录,而在nginx配置中并没有实际的资源,所有会出现404。直接上配置:

server {
        listen       80;
        server_name  www.qdabc.cn;

        location / {
                root  /var/www/VueApp;
                index  index.html index.htm;
                try_files $uri $uri/ @router;
        }
        location @router {
            rewrite ^.*$ /index.html last;
        }

                location /apis {
                        rewrite  ^/apis/(.*)$ /$1 break;
                        proxy_pass   http://127.0.0.1:3000/;
       }
}

加上这些后就可以正常访问啦。

 

欢迎分享本文,转载请保留出处:前端ABC » 关于nginx部署vue项目的两个问题

分享到:更多 ()

发表评论 0