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不是這種形式,就出錯了。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM