RStudio Connect Reverse Proxy to avoid SAML redirect errors

I have an professional version of RStudio Connect. For some reason Connect does not work with basic SAML integration, if your install is behind a LB or Proxy. I am getting an error which many others have gotten:

{"code":97,"error":"Changing host or scheme in redirect is forbidden.","payload":null}

Based on information given by RStudio Admins, this error is due to Connect not being satisfied with the HTTP headers that its getting in requests. It seems Ngnix reverse proxy is the solution for this, and while I have followed guidance in setting this up. I am still getting the above error.

Below is my Ngnix conf file

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
   # default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    server {
        listen 80;
        server_name fdsahl96ceb1b2;

        location / {
           #rewrite ^/connect/(.*)$ /$1 break;
            proxy_set_header    X-RSC-Request scheme://$host:$server_port$request_uri;
            proxy_pass http://localhost:3939/;
            proxy_redirect http://localhost:3939/ $scheme://$host/connect/;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
                proxy_http_version 1.1;
               #try_files $uri $uri/ =404;
    }
}

Thanks for making this post @kp117! I have it on my list to dig into further next week and see if there is a simple fix. This is the type of thing that you are certainly welcome to share with our professional support team by emailing support@rstudio.com.

It is also a great resource to have publicly for other users to reference, so we appreciate you posting here!

One note off the top of my head. I think this line:

            proxy_set_header    X-RSC-Request scheme://$host:$server_port$request_uri;

Is missing a $ on scheme. Switching scheme to $scheme might solve your issue!

However, it looks like you may have another proxy in front of nginx terminating TLS. As a result, $scheme will be http and could still cause issues. As a result, you may want to replace $scheme with https (just hard-code it). So the line would look like this:

            proxy_set_header    X-RSC-Request https://$host:$server_port$request_uri;

In any case, can you say a bit more about your architecture? Is there another proxy in front of nginx, like a load balancer, etc.? If so, can you describe it / how it is configured?