Adding a redirect to RStudio Connect Config

I originally had my Shiny Application set up on the open source shiny-server but ran into the load balancing issue. We upgraded to Connect to take advantage of load balancing, but now we can't figure out how to include a redirect in the configuration file.

When someone visits the website (www.example.com) it redirects them to www.example.com/content and asks them to login. I need it to redirect directly to the app, instead: www.example.com/myapp. How can I add this to my configuration file? It was simple in shiny-server open source, but I can't find documentation for Connect.


; RStudio Connect configuration file

[Server]

Address = https://dynamicfit.app
;LandingDir = /opt/rstudio-connect/myapp
;Server.LandingDir = /opt/rstudio-connect/myapp
;X-RSC-Request = https://dynamicfit.app/myapp
;None of these worked

;[HTTP]
;Listen = :3939
;Turned off

[HTTPS]
Listen = :443
Permanent = true
Certificate = /etc/letsencrypt/live/www.dynamicfit.app/fullchain.pem
Key = /etc/letsencrypt/live/www.dynamicfit.app/privkey.pem

[HTTPRedirect]
Listen = :80

[Authentication]
; Do I need this?  There is no password necessary to access the website.
Provider = password

Hello! Thanks so much for reaching out here and sharing your use case!! Unfortunately there is not an "out of the box" way to do this in the product today, but I will definitely pass along the feedback to our team so we can consider this case as we improve the product!

A couple of items and possible workarounds here:

  • On the Provider = password piece. Connect must run with some authentication mechanism. This is imperative because publishing to the server does not require SSH access (as was the case with Shiny Server) - you can publish directly through the browser. As a result, authentication restricts who can publish to the server. However, you can make the app itself unauthenticated by setting the "Access Control" to "Anyone - No Login Required"

LandingDir = /etc/landing-dir/
  • You can then create a file named index.html at /etc/landing-dir/index.html (there is a crude example at /opt/rstudio-connect/examples/landing-dir to get you started)
  • In that file, if you add this JavaScript to the <head> section, it will redirect the /connect/ page to /someapp
<script>
  window.top.location.href = "/someapp"
</script>
  • The other option here is to put a proxy in front of RStudio Connect to do this redirect. Most proxies (nginx, apache/httpd, etc.) have some simple patterns for issuing rewrites / redirects. However, this is a whole different toolset and can be very tricky to debug, so we would not recommend going this direction unless you or someone at your organization is comfortable with this type of tooling.

In the future, I am hopeful we will have some type of configuration setting that would make the "root" redirect URL more customizable! Please let us know how you decide to proceed here and how it goes or if you run into any trouble! :smile:

Thank you so much! This is so helpful. It's nice to know that we aren't going crazy!

For option 1, this is what I came up with (as a reference for myself and others):

/opt/rstudio-connect/myapp/index.html

<!DOCTYPE html>
<html>
  <head>

<script>
  window.top.location.href = "/myapp/"
</script>

  </head>
</html>

.gcfg


; RStudio Connect configuration file

[Server]
Address = https://dynamicfit.app
LandingDir = /opt/rstudio-connect/myapp

[HTTPS]
Listen = :443
Permanent = true
Certificate = /etc/letsencrypt/live/www.dynamicfit.app/fullchain.pem
Key = /etc/letsencrypt/live/www.dynamicfit.app/privkey.pem

[HTTPRedirect]
Listen = :80

[Authentication]
Provider = password

I restarted rstudio-connect and cleared my cookies/history. It is now redirecting to /myapp instead of /connect!

Edit: It looks like I can still access the dashboard by going to https://dynamicfit.app/connect/#/login as this does not redirect, so this should hold us over for the time being until a more permanent solution comes about. It isn't ideal, because I have to clear my cookies for the website to start redirecting back to /myapp. However, it is a fine solution compared to no solution at all! Or, is there another way to access the dashboard that avoids this?

Regarding option 2:

I am working with someone in IT and if it's just standard Nginx, we can probably figure out the redirects. Do we have to follow these instructions? https://docs.rstudio.com/connect/1.8.0/admin/running-a-proxy.html#nginx-as-reverse-proxy, or is this for a different purpose? Under Path rewriting configuration, there are instructions to add a X-RSC-Request command which, to my knowledge, is not standard Nginx. I'm just trying to figure out if we're comfortable taking this on!

Thanks again.

Great questions, and so glad to hear you got that working!

Regarding Option 1 - I believe you are missing the redirect at present because you are logged in. If you log out, the redirect will start happening again. Connect is trying to balance two things for you: As a logged in user, you have the ability to make changes to applications / publish new ones / etc. That is done in the dashboard.

As a not-logged-in user, the landing page (served at /connect/) will redirect where you want it to - in this case an App. If you have thoughts on how you envision this dichotomy and what would work better for you, we are definitely all ears!

On option 2:

That is right - if you are using version 1.8.0, then that is the right version of the documentation :smile: For the latest version of the docs, you probably want this one: https://docs.rstudio.com/connect/admin/proxy/#simple-configuration

To be clear, though, you probably don't need the full "path-rewriting configuration." That is only needed if you are hosting Connect itself at a path other than root. I.e. at /rsc/, so your app URL would be /rsc/my-app/. The X-RSC-Request is not "standard" per se, but it is just a special header being added to tell Connect about what the proxy is doing :smile: For non-path-rewriting configuration, it is superfluous and can be substituted for more standard X-Forwarded-* headers if that is preferable.

In any case, I suspect you would only need a line like this to take our simple configuration into what you're looking for. It's worth keeping in mind that nginx is another service and another potential point of failure being introduced into your infrastructure. For instance, we have seen cases where a proxy in front of the product blocks upload bundles of a certain size (mitigated by client_max_body_size 1024m; or some such), or times out websocket connections after a certain time (mitigated by things like proxy_read_timeout 20d;), etc. It's also worth documenting why nginx is a part of your infrastructure in case you are ever inclined to remove it! (It can be hard to remove things when you don't remember why they are there... spoken from experience :see_no_evil: )

In any case, this is a simple (untested) redirect that might provide a starting point:

rewrite ^/$ https://$http_host/myapp/ permanent;
1 Like

Oh one last thing! I would recommend moving your LandingDir outside of /opt/rstudio-connect! It is best to keep it outside of the directories that the product itself manages (/etc/rstudio-connect and /opt/rstudio-connect and the DataDir - /var/lib/rstudio-connect by default).

That way if Connect makes changes / does anything on upgrade in the future, it will not conflict with your files.

1 Like

Opt 1: Perfect! Thank you. This works well.
Opt 2: Thank you so much for explaining this. I'll chat with our IT guy and see if we want to explore this. I imagine that we might end up placated by the first solution for quite a while :wink: ... but this absolutely seems worth exploring (if not just for a learning experience).

Lastly - good to know! I am very new to this, and didn't realize that Connect modifies these folders. I will move it!

I really can't thank you enough - I've been struggling with this entire process for 2-3 months, so it's amazing to get this puzzle piece in place!

1 Like

Very glad to hear this is working for you! :smile: Although, our apologies for the challenge in getting it setup! Please do let us know if you have any other feedback, ideas, or pain points!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.