How to setup nginx and shiny-server to support separate webpage(s) and multiple shiny apps?

59 Views Asked by At

Goals:

My goals are to host a normal html/php website along side a shiny-server that hosts multiple apps. I also want automatic http to https redirect for all pages.

For example I want the pages accessed like the following:

www.example.com/ <- main html/php webpage, retrieving pages from /var/www/html

www.example.com/shiny <- shiny server landing page with test/default app at /srv/shiny-server/

www.example.com/shiny/app1 <- points to /srv/shiny-server/app1/

www.example.com/shiny/app2 <- points to /srv/shiny-server/app2/

I've tried to follow a bunch of guides and tutorials [1-6], but none really match my goals nor explain how the nginx directives work. Or how nginx redirects interact with the shiny-server config.

Because each tutorial is slightly different and I've tried mixing and matching but it's not working (unsurprisingly).

Problem:

With the setup I've managed so far (detailed below) I have the following problem.

www.example.com/ <- this works with the default nginx welcome page.

www.example.com/shiny <- This works with the default shiny app page.

www.example.com/shiny/app1 <- instead of app1 working, I get redirected back to

www.exmaple.com/app1 and nothing is found (404 not found).

Yet, if I manually re-type the URL www.example.com/shiny/app1 in the browser, shiny-server will load app1 so the shiny-server is "working".

Current setup:

I'm running Ubuntu 20.04.6 LTS with shiny-server 1.5.20 and ngnix 1.18.0. The server is only available from an intranet and is not Internet facing.

nginx configuration: /etc/nginx/sites-enabled/example.com

map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
}

# redirect all http request to https
server {
        listen 80;
        listen [::]:80;

        server_name example.com www.example.com;

        return 301 https://$server_name$request_uri;
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;
        include snippets/self-signed.conf;
        include snippets/ssl-params.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        # server_name _;
        server_name example.com www.example.com;

        # Normal web-pages.
        location / {
                try_files $uri $uri/ uri.html =404;
                #proxy_pass http://W.X.Y.Z:/;
                #proxy_redirect http://W.X.Y.Z:443/ https://$host/;
                #proxy_http_version 1.1;
                #proxy_set_header Upgrade $http_upgrade;
                #proxy_set_header Connection $connection_upgrade;
        }

        # Shiny apps server.
        location /apps/ {

                add_header X-Frame-Options "ALLOW-FROM example.com";
                rewrite /apps/(.*) /$1 break;
                rewrite ^/apps$ /app/ permanent;
                proxy_pass http://W.X.Y.Z:3838/;
                proxy_redirect http://W.X.Y.Z:3838/apps/ https://$host/apps/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
                proxy_read_timeout 20d;
                proxy_buffering off;
        }

shiny-server configuration: /etc/shiny-server/shiny-server.conf

# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;

# Define a server that listens on port 3838
server {
    listen 3838;

    # Define a location at the base URL
    location / {

    # Host the directory of Shiny Apps stored in this directory
    site_dir /srv/shiny-server;

    # Log all Shiny output to files in this directory
    log_dir /var/log/shiny-server;
    }

    location /app1 {
        app_dir /srv/shiny-server/app1;

        # Define where we should put the log files for this location
        log_dir /var/log/shiny-server;
    }

    location /app2 {
        app_dir /srv/shiny-server/app2;

        # Define where we should put the log files for this location
        log_dir /var/log/shiny-server;
    }

}

Thanks for reading.

References:

  1. D. Lorino, “Is it possible to deploy multiple shiny apps to one server using NGINX?,” Stack Overflow. [Online]. Available: Is it possible to deploy multiple shiny apps to one server using NGINX?

  2. R. with W. Dwarf, “Deploy your own Shiny app server with debian | R-bloggers.” [Online]. Available: https://www.r-bloggers.com/2023/01/deploy-your-own-shiny-app-server-with-debian/

  3. A. B. Collier, “RStudio & Shiny Servers with NGINX & SSL.” [Online]. Available: https://datawookie.dev/blog/2018/11/rstudio-shiny-servers-with-nginx-ssl/

  4. “Running Shiny Server with a Proxy,” Posit Support. [Online]. Available: https://support.posit.co/hc/en-us/articles/213733868-Running-Shiny-Server-with-a-Proxy

  5. R. on T. Blog, “Adding a website next to your Shiny server | R-bloggers.” Accessed: [Online]. Available: https://www.r-bloggers.com/2023/09/adding-a-website-next-to-your-shiny-server-2/

  6. fromtheloam, “Shiny Server - problem hosting an additional app,” Stack Overflow. [Online]. Available: Shiny Server - problem hosting an additional app

0

There are 0 best solutions below