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


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

    server {
            listen       80;
            server_name  www.liangyp.xyz;//访问网址
    
            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.liangyp.xyz;
    
            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/;
           }
    }

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

  • 相关阅读:
    html 注释和特殊字符
    html 锚点链接
    html 链接标签
    spring 利用工厂模式解耦
    html 路径
    html 图像标签
    html div和span标签
    html 文本格式化标签
    P5358 [SDOI2019]快速查询
    luoguP2679 子串
  • 原文地址:https://www.cnblogs.com/lyps/p/10183949.html
Copyright © 2020-2023  润新知