I am trying to deploy an angular application to AWS ECS by using the following image based on nginx:
FROM nginx:1.21.0-alpine
COPY nginx.conf /etc/nginx/nginx.conf
COPY ./dist/TreVR-Admin /usr/share/nginx/html
The config file for nginx is the following:
events{}
http {
include /etc/nginx/mime.types;
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
}
The problem is the following:
My app is deployed to AWS and is part of a target group of a load balancer with the rule /admin*. However, the app in this way won't be reachable because it doesn't have any path starting with /admin. In order to make it work so, I added the BASE_HREF in my module as follows:
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptorService, multi: true },
{ provide: APP_BASE_HREF, useValue: '/admin/app'}
]
This way the app is now reachable from outside the network at path http://[aws-lb-url]/admin/, however when I enter this address the app appears to be blank, and that is because it's trying to find some static files without adding the /admin prefix, in fact from the console I can see it receives a 503 when trying to reach [aws-lb-url]/main.affd1277a903a3be184d.js.
In order to solve this, I've read that one can use deployUrl, inside angular.json, to tell angular where static files will be deployed, adding:"deployUrl": "/admin/" under build>options.
This way the 503 error goes away, and now comes my current situation... The app is correctly accessible and static files are all prefixed with /admin, however, the page remains blank and console says:
Did not parse stylesheet at 'http://[aws-lb-url]/admin/styles.e2c1386ae8c4ef235c20.css' because non CSS MIME types are not allowed in strict mode.
[Error] SyntaxError: Unexpected token '<'
(anonymous function) (runtime.4fdc2b3a7bc1a0053176.js:1)
[Error] SyntaxError: Unexpected token '<'
(anonymous function) (polyfills.e569bb8dde4f7004e177.js:1)
[Error] SyntaxError: Unexpected token '<'
(anonymous function) (scripts.20a5fef0ca071d6f938f.js:1)
[Error] SyntaxError: Unexpected token '<'
(anonymous function) (main.63d270b9b9b4e693ab14.js:1)
It seems those files are not css or js, but instead the main page, in fact if I try to curl the url at http://[aws-lb-url].elb.amazonaws.com/admin/scripts.20a5fef0ca071d6f938f.jsI get this:
<!DOCTYPE html><html lang="en"><head>
<meta charset="utf-8">
<title>TreVRAdmin</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<style>@charset "UTF-8";:root{--bs-blue:#0d6efd;--bs-indigo:#6610f2;--bs-purple:#6f42c1;--bs-pink:#d63384;--bs-red:#dc3545;--bs-orange:#fd7e14;--bs-yellow:#ffc107;--bs-green:#198754;--bs-teal:#20c997;--bs-cyan:#0dcaf0;--bs-white:#fff;--bs-gray:#6c757d;--bs-gray-dark:#343a40;--bs-gray-100:#f8f9fa;--bs-gray-200:#e9ecef;--bs-gray-300:#dee2e6;--bs-gray-400:#ced4da;--bs-gray-500:#adb5bd;--bs-gray-600:#6c757d;--bs-gray-700:#495057;--bs-gray-800:#343a40;--bs-gray-900:#212529;--bs-primary:#0d6efd;--bs-secondary:#6c757d;--bs-success:#198754;--bs-info:#0dcaf0;--bs-warning:#ffc107;--bs-danger:#dc3545;--bs-light:#f8f9fa;--bs-dark:#212529;--bs-primary-rgb:13,110,253;--bs-secondary-rgb:108,117,125;--bs-success-rgb:25,135,84;--bs-info-rgb:13,202,240;--bs-warning-rgb:255,193,7;--bs-danger-rgb:220,53,69;--bs-light-rgb:248,249,250;--bs-dark-rgb:33,37,41;--bs-white-rgb:255,255,255;--bs-black-rgb:0,0,0;--bs-body-color-rgb:33,37,41;--bs-body-bg-rgb:255,255,255;--bs-font-sans-serif:system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans","Liberation Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--bs-font-monospace:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;--bs-gradient:linear-gradient(180deg,#ffffff26,#fff0);--bs-body-font-family:var(--bs-font-sans-serif);--bs-body-font-size:1rem;--bs-body-font-weight:400;--bs-body-line-height:1.5;--bs-body-color:#212529;--bs-body-bg:#fff;}*,:after,:before{box-sizing:border-box;}@media (prefers-reduced-motion: no-preference){:root{scroll-behavior:smooth;}}body{margin:0;font-family:var(--bs-body-font-family);font-size:var(--bs-body-font-size);font-weight:var(--bs-body-font-weight);line-height:var(--bs-body-line-height);color:var(--bs-body-color);text-align:var(--bs-body-text-align);background-color:var(--bs-body-bg);-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:rgba(0,0,0,0);}</style><link rel="stylesheet" href="/admin/styles.e2c1386ae8c4ef235c20.css" media="print" onload="this.media='all'"><noscript><link rel="stylesheet" href="/admin/styles.e2c1386ae8c4ef235c20.css"></noscript></head>
<body>
<app-root></app-root>
<script src="/admin/runtime.4fdc2b3a7bc1a0053176.js" defer></script><script src="/admin/polyfills.e569bb8dde4f7004e177.js" defer></script><script src="/admin/scripts.20a5fef0ca071d6f938f.js" defer></script><script src="/admin/main.63d270b9b9b4e693ab14.js" defer></script>
</body></html>