Shiny App 如何将http地址替换为https

Posted

技术标签:

【中文标题】Shiny App 如何将http地址替换为https【英文标题】:How to replace http address to https for Shiny App 【发布时间】:2019-05-20 05:33:21 【问题描述】:

我有一个在 Digitalocean 中托管的 Shiny 应用,其 Web 服务器为 nginx。网址看起来像

http://www.exacmple.com/ShinyApp

但是我希望我可以将 http 更改为 https。即对这个应用程序的所有请求都将被路由到 https:///www.exacmple.com/ShinyApp

我已经从letsencrypt 安装了 SSL 证书,证书文件位于以下地址:

/etc/letsencrypt/live/example.com/fullchain.pem;
/etc/letsencrypt/live/example.com/privkey.pem;

目前,我的 Nginx 代理文件设置如下:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
  map $http_upgrade $connection_upgrade 
      default upgrade;
      ''      close;
    



server 
    listen 80 default_server;
    listen [::]:80 default_server;
        server_name example.com www.example.com;
    if ($http_host = example.com) 
            rewrite  (.*)  https://www.example.com$1;
          



    root /var/www/html;

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

    location / 
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    

location /ShinyApp/ 
      rewrite ^/ShinyApp/(.*)$ /$1 break;
      proxy_pass http://localhost:4242;    
      proxy_redirect http://localhost:4242/ $scheme://$host/ShinyApp/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_read_timeout 20d;
      proxy_buffering off;
    

为了实现 https,我已将这个文件附加如下(位置部分

location /ShinyApp/ 
      rewrite ^/ShinyApp/(.*)$ /$1 break;
      SSLEngine on
      ssl_certificate           /etc/letsencrypt/live/example.com/fullchain.pem;
       ssl_certificate_key       /etc/letsencrypt/live/example.com/privkey.pem;

      ProxyPreserveHost On
      proxy_pass http://localhost:4242;
      roxyPassReverse http://localhost:4242;
      proxy_redirect http://localhost:4242/ $scheme://$host/ShinyApp/;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_read_timeout 20d;
      proxy_buffering off;
    

但上述更改未能实现 https 请求。

我已经浏览了 Web 上提供的各种建议(例如 HTTPS for Shiny apps?),但未能找到任何可行的解决方案。

任何指向正确方向的指针都会非常有帮助。

谢谢,

【问题讨论】:

【参考方案1】:

通常的做法是使用两个服务器块:

server 
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;


server 
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name example.com www.example.com;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ... # root, index and other top-level directives here
    location /ShinyApp/ 
        # your backend configuration here
    

不要在 location 块内使用 ssl_certificatessl_certificate_key 指令,注意可能使用或不使用 nginx 指令的上下文。 SSLEngineProxyPreserveHostProxyPassReverse 是 apache 指令,删除它们!在使用新配置重新加载 nginx 之前,使用 nginx -t 测试您的配置。

【讨论】:

以上是关于Shiny App 如何将http地址替换为https的主要内容,如果未能解决你的问题,请参考以下文章

如何在 R Shiny App 中保留复选框中的值?

如何在Shiny app中的仪表板主体中输出数据库表?

Shiny App - 从上传的 CSV 生成和下载 PPTX 幻灯片

R shiny教程-3:添加小部件到Shiny App

在 Shiny 中替换列中的重复行

R shiny 中的级别替换创建两个级别列表,一个为 NULL