部署补充

本文对上一篇文章进行补充,记录部署完成之后发现的几个点。

nginx 的反向代理

  根据官方文档走是没有问题的但是请注意在写代理地址的时候如果你的站点不在 80 端口上请加上端口号因为 nginx 默认代理到 80 端口。

server {
    listen        80;
    server_name   example.com *.example.com;
    location / {
        proxy_pass         http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host:5000;//加上端口号
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

关于 angular 跨域代理的问题。

  在 angular 中处理跨域问题时,一般使用 proxyconifg.json 文件处理跨域。当 angular 发布之后这个文件不会在起作用,在请求的时候需要补全请求地址并且由接口处理跨域,如果没有不全则默认请求 angular 地址的/xxx/xxxx 会返回 json 格式化失败,因为返回的是发布后的 index.html 文件。好像 nginx 也能处理跨域不太清楚,哈哈哈。

nginx 部署 angular 项目

  因为 angular 发布之后是静态文件,所以不需要进程守护,那么只需要把 ngxin 的配置文件写对就可以了。

server {
        listen 4200;//监听端口
        root //这里写文件地址;
        index index.html index.htm index.html;//第三个为发后之后的首页
        server_name _;
        location / {
                try_files $uri $uri/ /index.html;//防止刷新404还是子路由无效?
        }

}

原文地址:https://www.cnblogs.com/zyz-Notes/p/12631156.html