docker nginx angular 刷新错误,404错误

主要是router问题,两个解决方案

一个是修改angular项目的router选项,一个是修改Nginx的route 选项

一般情况下项目部署了,不愿意修改angular项目的router选项,所以

修改nginx 的选项:

第一步

查看nginx的配置文件位置

docker exec -it yourContainerId  /bin/sh

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

第二步

文件cp

docker cp yourContainerId:/etc/nginx/nginx.conf nginx.conf

打开复制到当前路径下的文件:nginx.conf,其他建议就是添加:


location /myangular {
alias /html/myangular;#angular项目所在目录
index index.html index.htm;
try_files $uri $uri/ /index.html =404;#主要是这一行
}

但不一定起作用,主要是这句配置:

include /etc/nginx/conf.d/*.conf;

这就需要重复上述步骤进入exec cp /etc/nginx/conf.d/下的conf文件修改:

还是加上这一行:

try_files $uri $uri/ /index.html;

最后

执行cp命令,复制回去,重新启动container就ok了。

docker cp default.conf yourContainerId:/etc/nginx/conf.d/default.conf

angular项目中的解决办法

在root.module.ts加入:

import {HashLocationStrategy, LocationStrategy} from '@angular/common';
添加provide:
{ provide: LocationStrategy, useClass: HashLocationStrategy, }
这时候在刷新时候会加上#,就不会出现刷新出错的问题了。
建议使用nginx配置方法,有时候项目中可能其他地方配置的url不是这种形式,就出错了。
 
posted @ 2019-08-06 09:11  indexlang  阅读(1065)  评论(0编辑  收藏  举报