vue vue-router(history模式) node(express)项目部署到云主机上的nginx配置思考

我我的想了2种部署方案html

一、将vue项目打包后放入node服务端的静态资源中访问

总体结构基本是这样的vue

clipboard.png

这样在云主机上的nginx配置就很简单node

server {
    listen 80;
    # 你的域名
    server_name xxxxx.com;
    
    location / {
        # 你的node服务进程
        proxy_pass http://localhost:8088;
    }
}
但这样有一个问题,若是你的vue-router是history模式的话,你刷新或者手动输入url访问将会是404,你也很难经过修改nginx配置来规避这个错误(由于并不须要在nginx配置里面指定根目录)

解决办法:
vue官方
基于 Node.js 的 Expressnginx

对于 Node.js/Express,请考虑使用 connect-history-api-fallback 中间件。vue-router

这个方法我没有尝试过,不过应该是可行的!api

这样的优势是很简便,适合小型的网站项目跨域

2 将打包的vue项目和node服务端分别部署

server {
    listen 80;
    server_name xxx.com;
    # 客户端
    location / {
            # 根目录
            root html/client;
            # 主页
            index index/html index/htm;
            # 避免history模式刷新404
            try_files  $uri $uri/ /index.html;
    }
    # 管理控制后台
    location /admin {
            root html/admin;
            index index/html index/htm;
            try_files  $uri $uri/ /index.html;
    }
    # 服务端
    # api 
    location /api {
            proxy_pass   http://localhost:8088;
            # 跨域
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    }

}

这样部署虽然稍微麻烦一点,但能够适应不少场景,并且开发分工更方便,结构也一目了然ide

相关文章
相关标签/搜索