Search code examples
rshinyapache2shiny-server

Shiny server running on localhost can't be accessed from web when redirected by apache


Hello dear stackoverflow!

I'm having trouble setting up shiny server and apache2 routing. Roughly following this guide https://support.rstudio.com/hc/en-us/articles/213733868-Running-Shiny-Server-with-a-Proxy (but with apache2)

I set up shiny to run on my 127.0.0.1:2224 port. Everything works just fine when I open a tunnel and port forward, I see my app on localhost.

I set up pwd and redirection in apache as:

<VirtualHost *:80>
    ServerAdmin karin@localhost
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    ProxyPass "/myapp" "http://localhost:2224"
    ProxyPassReverse "/myapp" "http://localhost:2224"

   <Location /myapp>
    AuthType Basic
    AuthName "Enter your login name and password"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
   </Location>

And when I open public my.ip/myapp I am prompted to log in, and after doing so I am redirected to local port 2224, yay!

~ however, the app seems to be broken. I can't see any of the resources, images, css - just the plain interface as coded in the app ui.R

I see a plenty of 404 in logs, the shiny server is looking for all these resources on the web, with address my.ip/shared/whatever - but the resources are accessible only through localhost:2224/shared/whatever

...

any idea where I messed up the routing? What privileges are missing, or what to try to be able to access my app without setting up ssh tunnel and port forward?

Thanks a lot for any ideas!


Solution

  • Roughly following this guide https://support.rstudio.com/hc/en-us/articles/213733868-Running-Shiny-Server-with-a-Proxy (but with apache2)

    I have had success in using apache2 as a reverse proxy for shiny server when following that guide in detail, in particular the last examples. In your case, you should replace

    ProxyPass "/myapp" "http://localhost:2224"
    ProxyPassReverse "/myapp" "http://localhost:2224"
    

    with

     RedirectMatch permanent ^/myapp$ /myapp/
    
     RewriteEngine on
     RewriteCond %{HTTP:Upgrade} =websocket
     RewriteRule /myapp/(.*) ws://localhost:2224/$1 [P,L]
     RewriteCond %{HTTP:Upgrade} !=websocket
     RewriteRule /myapp/(.*) http://localhost:2224/$1 [P,L]
     ProxyPass /myapp/ http://localhost:2224/
     ProxyPassReverse /myapp/ http://localhost:2224/
    
     Header edit Location ^/ /myapp/
     ProxyRequests Off