I have the build of an Angular app located at:
/Users/myName/Documents/myApp/app1/dist
I’d like to serve the app directly from this build. I’ve set up an Nginx server with the following additional configuration:
server {
listen 4200 ssl;
server_name mysubdomain.com;
index index.html;
location / {
root /Users/myName/Documents/myApp/app1/dist;
try_files $uri $uri/ /index.html;
}
access_log /opt/homebrew/var/log/nginx/access.log;
error_log /opt/homebrew/var/log/nginx/error.log;
}
When I open the following URL, everything works correctly and I can see the expected page:
https://mysubdomain.com:4200/login
Now, I’d like to change the configuration so that the app is served under a suffix (e.g. mysubdomain.com:4200/app1/login), while keeping the rest exactly the same; I think, I should change the location but just adding a suffix after “/” there doesn’t work. The idea is to prepare for the future, where I’ll need to differentiate between two sub-apps (app1 and app2).
For several reasons, I cannot change the structure of the current Angular application (e.g. the base href). I only want to differentiate the location in Nginx, without modifying anything else in the app or its configuration.
How can I achieve this?
Thank you very much!
Update
If I try to use the alias, in this way:
server {
listen 4200 ssl;
server_name mysubdomain.com;
index index.html;
location / {
alias /Users/myName/Documents/myApp/app1/dist/;
try_files $uri $uri/ /index.html;
}
access_log /opt/homebrew/var/log/nginx/access.log;
error_log /opt/homebrew/var/log/nginx/error.log;
}
and if I serve the app by using the same address as before, it still works. But, if I try to change the location and the try_files like suggested in the first comment, the application is not longer able to load anything (in the console, network, I see that main.js, styles.css, polyfills and any other resource are not found).
Just to be fully clear, this is the result in my console just after:
-
Changing the location from
/to/app1/ -
Changing try_files from
try_files $uri $uri/ /index.html;totry_files $uri $uri/ /app1/index.html; -
Changing the url from
.../loginto.../app1/login
Now, I believe that my description is really clear. Thanks again for your help and your time.
