Serve RStudio from example.com/rstudio with an Apache proxy

I tried to follow the instructions for serving RStudio from a custom path (e.g. example.com/rstudio), but they didn't work for me.

The instructions: https://support.rstudio.com/hc/en-us/articles/200552326-Configuring-the-Server/

Apache configuration:

  RewriteEngine on
  RewriteCond %{HTTP:Upgrade} =websocket
  RewriteRule /rstudio/(.*)     ws://localhost:8787/$1  [P,L]
  RewriteCond %{HTTP:Upgrade} !=websocket
  RewriteRule /rstudio/(.*)     http://localhost:8787/$1 [P,L]
  ProxyPass /rstudio/ http://localhost:8787/
  ProxyPassReverse /rstudio/ http://localhost:8787/
  ProxyRequests Off

When I visit example.com/rstudio I get redirected to example.com/auth-sign-in and see:

Not Found
The requested URL /auth-sign-in was not found on this server.

If I add two more lines for the auth-sign-in URL like this:

ProxyPass        /auth-sign-in   http://localhost:8787/auth-sign-in
ProxyPassReverse /auth-sign-in   http://localhost:8787/auth-sign-in

Then I see a login screen like this (notice the image does not load):

RStudio_Sign_In

The image does not load because it's being requested from example.com/images/rstudio.png

Finally, when I try to login with an existing user/pass on my system, I get Error: Not Found.

I guess the instructions should be updated. My guess is that serving from a custom path like example.com/rstudio is more difficult to set up than serving from a naked domain like example.com.

I'd appreciate any tips!

I'm hoping to have a chance to give this a shot later and see if I can reproduce. In the meantime, it can be helpful to enable the Chrome / Firefox developer tools and look at the "network" tab to see where you are being routed:

You can also do some cool stuff with curl - i.e. curl -i http://my.url will show the response header, which can be useful for determining what is going on with a proxy.

I can reproduce on Ubuntu 16.04 and apache2. Setting up Shiny Server and RStudio Servers on subpaths is notoriously difficult. My config so far:

# /etc/apache2/sites-available/shiny.conf                                                                                                                                                                                                                                                                                                                                                    
   RewriteEngine on                                                                                                                                                                                                 
   RewriteCond %{HTTP:Upgrade} =websocket                                                                                                                                                                           
   RewriteRule /shiny/(.*) ws://127.0.0.1:3838/public/$1 [P,L]                                                                                                                                                      
   RewriteCond %{HTTP:Upgrade} !=websocket                                                                                                                                                                          
   RewriteRule /shiny/(.*) http://127.0.0.1:3838/public/$1 [P,L]                                                                                                                                                    
                                                                                                                                                                                                                    
   #ProxyPreserveHost On                                                                                                                                                                                            
   ProxyPass /shiny/ http://localhost:3838/public/                                                                                                                                                                  
   ProxyPassReverse /shiny/ http://localhost:3838/public/                                                                                                                                                           
   ProxyRequests Off                                                                                                                                                                                                
                                                                                                                                                                                                                    
   # redirect to add trailling slash                                                                                                                                                                                
   RedirectMatch permanent ^/shiny$ /shiny/    

# /etc/apache2/sites-available/rstudio.conf 
RewriteEngine on                                                                                                                                                                                                  
  RewriteCond %{HTTP:Upgrade} =websocket                                                                                                                                                                            
  RewriteRule /rstudio/(.*) ws://localhost:8787/$1 [P,L]                                                                                                                                                            
  RewriteCond %{HTTP:Upgrade} !=websocket                                                                                                                                                                           
  RewriteRule /rstudio/(.*) http://localhost:8787/$1 [P,L]                                                                                                                                                          
                                                                                                                                                                                                                    
  ProxyPreserveHost On                                                                                                                                                                                              
  ProxyPass /rstudio/ http://localhost:8787/                                                                                                                                                                        
  ProxyPassReverse /rstudio/ http://localhost:8787/                                                                                                                                                                 
  ProxyRequests Off                                                                                                                                                                                                 
                                                                                                                                                                                                                    
  # redirect to add trailling slash                                                                                                                                                                                 
  RedirectMatch permanent ^/rstudio$ /rstudio/    

Sorry for the (extreme) delay here. I have been doing some digging on this today.

In your configuration for RStudio, it looks like ProxyPreserveHost On is causing problems. After commenting out that line, the configuration works as expected for me.

I am planning some updates to the support article for this type of behavior to be sure that this experience is improved. Apologies it took so long for me to get to this!

This definitely looks like textbook proxy-mis-configuration. However, this set-up works fine for me, and everything looks correct! Would you mind sharing your rserver.conf and the full apache VirtualHost configuration? It is possible that there may be another directive that is causing problems somewhere. It also may be necessary to restart the apache server (something like service apache2 restart) to pick up any changes to your configuration.

1 Like

Cole,
thank you, removing that line did the trick for me as well!

1 Like

Cole,

I am not exactly sure whether this is also related to a bad Apache proxy config, but I run into "Trying to reconnect" errors with all my Shiny apps (behind Apache2 with mod_auth_openid enabled and Google OAuth credentials), as shown below. The apps load no problem, but after session idle time, users are not able to reconnect.

Tried adding session$allowReconnect(TRUE) to server.R, but no difference.

Curious if others have a similar setup. Thx.

Here is the Apache openid config bit:

# Google OAuth data-worldcovr-com
OIDCProviderMetadataURL https://accounts.google.com/.well-known/openid-configuration 
OIDCClientID xxx.apps.googleusercontent.com 
OIDCClientSecret xxx 
OIDCRedirectURI https://data.worldcovr.com/oauth/auth 
OIDCCryptoPassphrase xxx
OIDCScope "openid email" 
OIDCCookiePath / 
OIDCAuthNHeader X-Forwarded-User 
OIDCAuthRequestParams hd=worldcovr.com