Nginx and Shiny Server
Last updated
Last updated
NGINX is a open-source, high-performance HTTP server with simple configuration and low resource consumption. NGINX powers many top sites, such as Netflix, Airbab, and Github.
In this topic, you will learn how to configure a simple HTTP server and a reverse proxy server for R-shiny
with NGINX.
To download and install NGINX. Execute the following commands.
To verify your installation, access http://your-ip-address
. You should see the default nginx page like below (if port forwarding is configured correctly).
Simple commands for start/stop/restart NGINX
server.
To make sure that NGINX server will restart automatically after rebooted, execute this:
NGINX's configuration capabilities are extensive and flexible. Thus, we will focus on simple HTTP server configuration for static content in this section.
To configure your server, open /etc/nginx/nginx.conf
. This is the global config file of NGINX.
Next, find the server
block within http
block, there may be some example or default setting in the block.
Now, let our server listen on the default TCP port 80
. Do not forget the trailing semicolon.
The default root folder of the web server is /var/www/html
. To change files in this path, you should always be a sudoer. For convenience sake, we will change the root
path to the home directory of the web server administrator.
First, create a folder www-data
in your web admin's home directory and a subfolder named www-data
in it. After that, create a new folder in the www-data
that named as the domain name/hostname of you server.
Next, add a server_name
and a root
parameters into NGINX config file.
Finally, to handle request from clients, add location
block inside the server block.
Add the following location block to the server block:
This location
block specifies the /
prefix compared with the URI from the request. The URI will be added to the root
for fetching local data under the root
concatenate with /
path.
That is, a http://example.domain/image.png
request may try to get an image right under you /home/a-web-admin/www-data/example.domain/image.png
if exists.
If you add another location like below:
The requests such as http://example.domain/image/something.jpg
would try to get file with the path home/a-web-admin/www-data/example.domain/image/something.jpg
.
For further basic configuration, see NGINX Official Tutorial.
R Shiny is a web application framework that can turn analyses into web applications without any web knowledge.
In this section, we will learn how to configure a Shiny server with the power of NGINX.
provides a convenient way to install and configure your R-shiny server. However, we would do some trick to combine Shiny and NGINX.
First, install shiny package for R.
Because RStudio company only provides Shiny server of Ubuntu version, we will use this Ubuntu version temporarily.
The default port for R-Shiny server is 3838. That means any can connect to the server through the URL http://<server-ip/hostname>:3838.
Simple commands for start/stop/restart your shiny-server
.
The default configure file for shiny-server locates at /etc/shiny-server/shiny-server.conf
file. This config file use nearly same syntax and parameters as NGINX config file. For example, if you want to change the default port to 13838, modify it as below;
Shiny Server administration document explains lots of features clearly. We will only show some useful config in this tutorial.
First, create a new account to manage all Shiny apps.
Here is the example configuration file at /etc/shiny-server/shiny-server.conf
.
To test your configs, put your Shiny apps in ~/ShinyApps/
. Assumed the hierarchy of your shiny app looks like this,
You should move your app folder into ~/ShinyApps/
directory such as ~/ShinyApps/myapps/
.
Finally, use a web browser to open http://localhost:3838/myapps/
. If you can connect to your app, the shiny server is successfully installed and working.
If you want to combine Shiny Server with NGINX HTTP Server. Here is the example configuration of NGINX.
If your shiny server serves at port 3838, NGINX will pass the request to your shiny server. Then redirect the URL as something like http://example.domain/shiny/you-apps/
.
For more NGINX configuration, see this resource.
For more Shiny server configs, see this guide.