본문 바로가기
Biz/Analysis

RStudio Server with a Proxy

by 조병희 2016. 7. 17.

Proxy는 자신을 통해서 다른 네트워크에 간접적으로 접속할 수 있도록 해주는 컴퓨터 혹은 프로그램을 가리킨다. 요청을 중계하는 일을 하는 컴퓨터 혹은 프로그램으로 이해하면 된다. 이때 중계하는 프로그램을 proxy 프로그램이라고 한다. proxy 프로그램은 서버로서 작동하기 때문에 일반적으로 proxy 서버라고 한다. 여러 가지 방법이 있지만 여기서는 간단히 Nginx 와 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 RStudio 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 RStudio Server.

http {

map $http_upgrade $connection_upgrade {

default upgrade;

'' close;

}

server {

listen 80;

 

 

 

location / {

proxy_pass http://localhost:8787;

proxy_redirect http://localhost:8787/ $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 RStudio Server from a custom path (e.g. /rstudio) you would edit your nginx.conf file as shown below:

http {

map $http_upgrade $connection_upgrade {

default upgrade;

'' close;

}

server {

listen 80;

 

 

location /rstudio/ {

rewrite ^/rstudio/(.*)$ /$1 break;

proxy_pass http://localhost:8787;

proxy_redirect http://localhost:8787/ $scheme://$host/rstudio/;

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

Apache Configuration

To enable an instance of Apache running on the same server to act as a front-end proxy to RStudio 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 RStudio Server. For example:

<VirtualHost *:80>

 

<Proxy *>

Allow from localhost

</Proxy>

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

</VirtualHost>

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

  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

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

RStudio Configuration

If your RStudio Server and proxy server are running on the same machine you can also change the port RStudio Server listens on from 0.0.0.0 (all remote clients) to 127.0.0.1 (only the localhost). This ensures that the only way to connect to RStudio Server is through the proxy server. You can do this by adding the www-address entry to the /etc/rstudio/rserver.conf file as follows:

www-address=127.0.0.1

Note that you may need to create this config file if it doesn't already exist.

 

출처: https://support.rstudio.com/hc/en-us/articles/200552326-Running-with-a-Proxy

 

댓글