如何使用闪亮服务器修复反向代理Nginx

时间:2019-09-04 18:27:39

标签: django nginx shiny shiny-server nginx-reverse-proxy

我无法通过nginx反向代理使用闪亮的应用程序使用django渲染javascript。例如,将不显示动态图,但将显示HTML和CSS滑块。我的Nginx配置是否正确设置?

我正在尝试在srv / shiny-server目录中显示通过闪亮服务器提供的闪亮应用程序。我一直在跟随本教程http://pawamoy.github.io/2018/03/15/django-auth-server-for-shiny/#try-it-with-a-dockerized-project

我将shny-server.conf更改为侦听端口8100。

我想知道是否有人对如何正确显示闪亮的应用程序有任何想法?

谢谢!

Nginx设置:


    # declare your Django app
    upstream djangoapp_server {
        server localhost:8000;
    }

    # declare your Shiny app
    upstream shinyapp_server {
        server localhost:8100;
    }

    # required for WebSockets
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    server {

        listen 80;
        server_name localhost;

        client_max_body_size 100M;

        # normal requests go to Django
        location / {
            proxy_pass http://djangoapp_server;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
            if (!-f $request_filename) {
                proxy_pass http://djangoapp_server;
                break;
            }
        }

        # "shinyTest" requests go to Shiny
        location ~ /shinyTest/.+ {

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

            proxy_pass http://shinyapp_server;
            proxy_redirect http://shinyapp_server/ $scheme://$host/shinyTest/;

            # required for WebSockets
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;

            proxy_read_timeout 20d;
            proxy_buffering off;
        }

    }

Django模板 template / shinytest.html


    {% load i18n static %}
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

        <script src="{% static "node_modules/bootstrap/dist/js/bootstrap.min.js" %}"></script>
        <link href="{% static "node_modules/bootstrap/dist/css/bootstrap.min.css" %}" rel="stylesheet">
        <link href="{% static "css/base.css" %}" rel="stylesheet">
      </head>
        <body>

        <div class="container-fluid">

          <section id="content">
            <script>
              $(document).ready(function () {
                $.getJSON('{% url "shiny_contents" %}', function (data) {
                  var iframe = document.createElement("iframe");
                  $('#content').append(iframe);
                  iframe.contentWindow.document.open();
                  iframe.contentWindow.document.write(data.html_contents);
                  iframe.contentWindow.document.close();




                  // Attempt circumvention
                  if (iframe.contentWindow.WebSocket)
                      WebSocket = iframe.contentWindow.WebSocket;
                });
              });

            </script>
          </section
        </div>
      </body>
    </html>

Django模板


    def shinyTest(request):
        return render(request, 'djangoShinyBeta/shinyTest.html')

    def shiny_contents(request):
        response = requests.get('http://localhost:8100/shinyapp/')
        soup = BeautifulSoup(response.content, 'html.parser')

        return JsonResponse({'html_contents': str(soup)})

我认为请求已正确代理,因为已加载静态资产。没有渲染的一件事就是图形。我的闪亮应用是通过srv / shiny-server / shinyapp中的闪亮服务器提供的。我可以通过http://localhost:8100/shinyapp/完全访问它。

还有一点要注意的是,如果我使用

为应用程序提供服务
sudo R -e "shiny::runApp(appDir='shinyapp', port=8100)

,然后将Shiny_contents视图更改为

def shiny_contents(request):
    response = requests.get('http://localhost:8100')
    soup = BeautifulSoup(response.content, 'html.parser')
    return JsonResponse({'html_contents': str(soup)})

闪亮的应用程序可以正确呈现。

Screenshot of reverse proxy not working

Screenshot of Console

0 个答案:

没有答案
相关问题