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:


My application is using the IDP addon setup this way:

cuba.web.idp.baseUrl =
cuba.idp.serviceProviderUrls =,
cuba.idp.serviceProviderLogoutUrls =,

In Nginx conf, it is setup as below:

upstream main_server{
   server  <ip of the main page>;
upstream sub_server{
   server  <ip of the sub page>;
server {
   listen 80;
   listen [::]:80;
   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!