Cuba IDP addon redirection issue when using Nginx

I’m facing an issue where after logging in to main page (baseUrl), I open an SP page (serviceProviderUrls) expecting it to show the SP page already logged in but instead, I got redirected again to the main page. This happens due to using Nginx redirection (this issue does not happen in localhost). The network inspector below shows the URL of the sp that seem to redirect to itself:

image

My application is using the IDP addon setup this way:

cuba.web.idp.baseUrl = https://abc.xyz.com/app/idp/
cuba.idp.serviceProviderUrls = https://abc.xyz.com/main/, https://abc.xyz.com/sub/
cuba.idp.serviceProviderLogoutUrls = https://abc.xyz.com/app/dispatch/idpc/logout, https://abc.xyz.com/sub/dispatch/idpc/logout

In Nginx conf, it is setup as below:

upstream main_server{
   ip_hash;
   server  <ip of the main page>;
}
upstream sub_server{
   ip_hash;
   server  <ip of the sub page>;
}
server {
   listen 80;
   listen [::]:80;
   server_name abc.xyz.com;
   ....
   location = / {
      return 301 http://$host/app/login.html;
   }
   location /app/ {
      proxy_pass http://main_server;
   }
   location /sub/ {
      proxy_pass http://sub_server/app/;
   }
}

Hope I can get some advice on what I might need to add to the Nginx conf to get the redirection correctly. Thanks!