- 修改src/environments/environment.prod.ts
文件中有參數serverUrl將url改成生產環境的ip:port。
- 使用命令ng build --prod來打包生產環境鏡像。打包結果在項目目錄dist文件夾下
- 將文件拷貝到Nginx的html目錄下,替換掉Nginx的index.html文件
- 修改nginx.conf
不加會出現刷新404的情況。
如果要配置帶有特定前綴的時候。比如http://172.16.18.199/ad-web/home中的ad-web
- 修改nginx.conf
項目放在/home/ad-web文件夾下,因爲我們使用的是root來指定根目錄。
root---實際訪問文件路徑會拼接URL中的路徑。
alias---實際訪問文件路徑不會拼接URL中的路徑。
- 然後,修改index.html文件。
將base.href設置爲匹配路徑,且需要前後加上斜槓。否則無法訪問js與css導致如下報錯。
runtime-es2015.ca84bb782683800d794b.js:1 Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
報錯的原因是我們在nginx中配置了try_files導致找不到js文件就會去找index.html。