Encountered the same issue while trying to deploy an Angular UI in a subfolder of nginx.
After some troubleshooting, I managed to resolve it. Sharing my solution here.
For example, if you wish to host your website at
Step 1: Use --base-href when building
ng build --base-href=/sub1/
Step 2: Nginx configuration
There are two approaches to hosting your subfolder.
Assuming your dist folder is located at html/sub1dist
1) Hosting local dist as a subfolder
server {
listen 80;
server_name localhost;
location /sub1 {
alias html/sub1dist;
try_files $uri $uri/ /index.html =404;
}
}
2) Proxy passing another endpoint as a subfolder
server {
listen 80;
server_name localhost;
location /sub1/ {
# rewrite to remove the subfolder
rewrite ^/sub1/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:8071;
}
}
server {
listen 8071;
server_name localhost;
location / {
root html/sub1dist;
try_files $uri $uri/ /index.html =404;
}
}
Both of the above solutions have worked successfully for me.