Nginx部署vue项目

byourb 2020-07-18

原文地址:https://www.cnblogs.com/mao2080/p/9340026.html

1、问题描述

给前端同事部署了web项目之后,访问发现除了index.html可以访问,其他的路径使用了“伪静态”。比如访问:http://localhost:8081/user/login,访问即报404错误,这个实际上是请求HTML相关资源而非后台接口,后面网上查了相关资料找到了解决办法。

2、配置文件

upstream portalServer {
        server 192.168.1.1:8080;
    }
    
    server {
        listen       8081;
        server_name  localhost;

        root   /usr/local/application/nginx/web/build;
        
        location / {
            try_files $uri $uri/ @router;
            index  index.html;
        }
        
        location @router{
            rewrite ^.*$ /index.html last;
        }
        #代理后台接口
        location /api/ {
            proxy_pass http://portalServer/;
            proxy_set_header Host $host:$server_port;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

3、参考网站

https://my.oschina.net/u/1760791/blog/1575808

http://blog.sina.com.cn/s/blog_bd418dfa0102wser.html

相关推荐

lwplvx / 0评论 2020-11-22
岁月如歌 / 0评论 2020-07-21