Support

Running Shiny Server with a Proxy

Follow

Shiny Server: Running with a Proxy

Overview

If you are running Shiny Server behind a proxy server you need be sure to configure the proxy server so that it correctly handles all traffic to and from Shiny Server. Beyond the normal reverse proxy configuration you'd apply for any HTTP server application, you also need to ensure that websockets are forwarded correctly between the proxy server and Shiny Server so that Shiny applications will run properly. This article describes how to correctly configure a reverse proxy with Nginx and Apache.

Nginx Configuration

On Debian or Ubuntu a version of Nginx that supports reverse-proxying can be installed using the following command:

sudo apt-get install nginx

On CentOS or Red Hat you can install Nginx using the following command:

sudo yum install nginx

To enable an instance of Nginx running on the same server to act as a front-end proxy to Shiny Server you would add commands like the following to your nginx.conf file. Note that you must add code to proxy websockets in order to correctly display Shiny apps and R Markdown Shiny documents in Shiny Server.

http {
map $http_upgrade $connection_upgrade { default upgrade; '' close; }
server { listen 80; location / { proxy_pass http://localhost:3838; proxy_redirect http://localhost:3838/ $scheme://$host/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_read_timeout 20d; } } }

If you want to serve Shiny Server from a custom path (e.g. /shiny) you would edit your nginx.conf file as shown below:

http {
map $http_upgrade $connection_upgrade { default upgrade; '' close; }
server { listen 80; location /shiny/ { rewrite ^/shiny/(.*)$ /$1 break; proxy_pass http://localhost:3838; proxy_redirect http://localhost:3838/ $scheme://$host/shiny/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_read_timeout 20d; }

In addition, if you also want to proxy the admin dashboard, you'll need to add a separate directive for that port as well - for instance:

location /shiny-admin/ {
      rewrite ^/shiny-admin/(.*)$ /$1 break;
      proxy_pass http://localhost:4151;
      proxy_redirect http://localhost:4151/ $scheme://$host/shiny-admin/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_read_timeout 20d;
    }

After adding these entries you'll then need to restart Nginx so that the proxy settings take effect:

sudo /etc/init.d/nginx restart

Nginx SSL Configuration

First, follow instructions in the previous section to install Nginx. 

Next, you will need to create or purchase a SSL certificate. This will work with a self-signed certificate, but you should use an officially signed certificate if you want to avoid browser error codes.

Next, you will need to create a new configuration file for your Shiny Server in the /etc/nginx/sites-available directory. Here is an example of what that content might look like:


server {
    listen 443;
    server_name <your_server_name>;

    ssl    on;
    ssl_session_timeout  5m;
    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers   on;

    ssl_certificate        <path_ssl_cert_file>;
    ssl_certificate_key    <path_to_ssl_key_file>;

    access_log /var/log/nginx/<your-site>.log;
    error_log /var/log/nginx/<your-site>-error.log error;

    location / {

      proxy_set_header    Host $host;
      proxy_set_header    X-Real-IP $remote_addr;
      proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header    X-Forwarded-Proto $scheme;

      proxy_pass          http://localhost:3838;
      proxy_read_timeout  90;

      proxy_redirect      http://localhost:3838/ $scheme://$host/;
    }
}

Note that your particular SSL requirements may be different and you may need to change this setup accordingly.

After creating this file, create a symlink in the /etc/nginx/sites-enabled directory pointing to the above file as in the following:

ln -s/etc/nginx/sites-available/shiny-server /etc/nginx/sites-enabled/shiny-server

and then restart Nginx.

Apache Configuration

To enable an instance of Apache running on the same server to act as a front-end proxy to Shiny Server you need to use the mod_proxy and mod_proxy_wstunnel modules. The steps for enabling this module vary across operating systems so you should consult your distribution's Apache documentation for details.

On Debian and Ubuntu systems Apache can be installed with mod_proxy using the following commands:

sudo apt-get install apache2
sudo apt-get install libapache2-mod-proxy-html
sudo apt-get install libxml2-dev

Then, to update the Apache configuration files to activate mod_proxy you execute the following commands:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_wstunnel

On CentOS and RedHat systems Apache can be installed with mod_proxy and mod_proxy_wstunnel by following the instructions here:

http://httpd.apache.org/docs/2.4/platform/rpm.html

By default with Apache 2.4, mod_proxy and mod_proxy_wstunnel should be enabled. You can check this by opening the file /etc/httpd/conf.modules.d/00-proxy.conf and making sure the following lines are included and not commented out:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

Once you have enabled mod_proxy and mod_proxy_wstunnel in your Apache installation you need to add the required proxy commands to your VirtualHost definition. Note that you will also need to include code to correctly proxy websockets in order to correctly proxy Shiny apps and R Markdown documents within Shiny Server. For example:

<VirtualHost *:80>

  <Proxy *>
    Allow from localhost
  </Proxy>

 RewriteEngine on
RewriteCond %{HTTP:Upgrade} =websocket
RewriteRule /(.*) ws://localhost:3838/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule /(.*) http://localhost:3838/$1 [P,L]
ProxyPass / http://localhost:3838/
ProxyPassReverse / http://localhost:3838/ </VirtualHost>

Note that if you want to serve Shiny from a custom path (e.g. /shiny) you would replace the directives described above to:

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

In addition, if you also want to proxy the admin dashboard, you'll need to add a separate directive for that port as well - for instance:

ProxyPass /shiny-admin/ http://localhost:4151/
ProxyPassReverse /shiny-admin/ http://localhost:4151/

Finally, after you've completed all of the above steps you'll then need to restart Apache so that the proxy settings take effect:

sudo /etc/init.d/apache2 restart

 

 
Have more questions? Submit a request

Comments

Powered by Zendesk