2016-06-13 117 views
1

有3种成分,这个问题:闪亮服务器反向nginx的代理不会跟随301重定向

  1. 泊坞容器:我有一个部署在EC2实例泊坞窗容器。更具体地讲,我有rocker/shiny形象,我一直在使用运行:

    sudo docker run -d -v /home/ubuntu/projects/shiny_example:/srv/shiny-server -p 3838:3838 rocker/shiny 
    
  2. 闪亮服务器:标准闪亮的服务器配置文件是不变,并成立了以服务端口的/srv/shiny-server夹中的一切3838,我的本地~/projects/shiny_example的内容映射到容器的/srv/shiny-server/

    在我的本地~/projects/shiny_example,我已经克隆随机闪亮的应用程序:

    git clone https://github.com/rstudio/shiny_example 
    
  3. nginx的:我已经建立了nginx作为反向代理和here在整体上/etc/nginx/nginx.conf的内容。


的问题是,这种设置,当我试图找回http://<ip-address>/shiny/shiny_example,我得到一个404的主线索,我有,什么可能是错误的是,当我做:

wget http://localhost:3838/shiny_example 

从我的EC2实例在命令行中,我得到:

--2016-06-13 11:05:08-- http://localhost:3838/shiny_example 解决localhost(localhost)... 127.0.0.1

连接到本地主机(本地主机)| 127.0.0.1 |:3838 ...已连接。

HTTP请求发送的,在等待响应... 301永久移动

位置:/ shiny_example/[以下]

--2016-06-13 11:05:08-- http://localhost:3838/shiny_example/

将现有连接重用到localhost:3838。

HTTP请求发送的,在等待响应... 200 OK

长度:3136(3.1K)的text/html]

保存到: 'shiny_example.3'

100%[ ================================================== ================================================== ============================] 3,136 - .- K/s在0.04s

2016-06-13 11: 05:09(79.6 KB/s) - 'shiny_example。3'保存[3136/3136]

其中重点是我的

我认为我的nginx配置没有考虑到请求Docker映射端口时出现301重定向的情况。我认为该解决方案涉及proxy_next_upstream,但我希望在尝试在我的上下文中进行设置时提供一些帮助。

我也认为这个问题可以忽略Docker上下文,但是理解如何在从Docker容器中的Shiny服务器请求资源时阻止301重定向,以及此行为是否为预期。

+0

您是否正在与'http:// /shiny/shiny_example'或'http:// :3838/shiny/shiny_example /'进行外部连接?这将有助于您展示失败的wget命令。请注意,重定向只是为您的url添加了一个尾部斜线,这不是Docker在做301. – BMitch

+0

@BMitch nginx正在侦听80并反向代理'localhost:3838'。另外请注意,'wget'不是失败,而是正在处理301。 – tchakravarty

+0

@BMitch所以,为了明确起见,我正在通过外部连接'http:// /shiny/shiny_example /'。问题中包含'wget'的输出。 – tchakravarty

回答

0

没有什么问题。基本上,在/etc/nginx/nginx.conf线路之一是include /etc/nginx/sites-enabled/*,这是拉动启用了网站的默认文件,里面有下面几行:

server { 
     listen 80 default_server; 
     listen [::]:80 default_server ipv6only=on; 

     root /usr/share/nginx/html; 
     index index.html index.htm; 

     # Make site accessible from http://localhost/ 
     server_name localhost; 

     location/{ 
       # First attempt to serve request as file, then 
       # as directory, then fall back to displaying a 404. 
       try_files $uri $uri/ =404; 
       # Uncomment to enable naxsi on this location 
       # include /etc/nginx/naxsi.rules 
     } 

这是覆盖我听指令的端口80和用于位置/。注释掉/etc/nginx/nginx.conf文件中已启用站点的默认conf文件的include指令解决了所有问题。

1

没有更多的产出,我不能肯定,但怀疑你的错误是在你的proxy_redirect行:

location /shiny/ { 
     rewrite ^/shiny/(.*)$ /$1 break; 
     proxy_pass http://localhost:3838; 
     proxy_redirect http://localhost:3838/ $scheme://$host/shiny_example; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection $connection_upgrade; 
     proxy_read_timeout 20d; 
    } 

尝试改变,为:

location /shiny/ { 
     rewrite ^/shiny/(.*)$ /$1 break; 
     proxy_pass http://localhost:3838; 
     proxy_redirect http://localhost:3838/ $scheme://$host/shiny/; 
     proxy_http_version 1.1; 
     proxy_set_header Upgrade $http_upgrade; 
     proxy_set_header Connection $connection_upgrade; 
     proxy_read_timeout 20d; 
    } 

其原因是当301头从“http://localhost:3838”回来添加尾部斜杠,它会被重写为“http://localhost/shiny_example”,它不存在于您的nginx配置中,并且还可能会从路径中删除斜杠。这意味着从“http://localhost:3838/shiny_example”到“http://localhost:3838/shiny_example/”的301将被重写为“http://localhost/shiny_exampleshiny_example/”,此时您将得到404。

+0

感谢您的回答BMitch。我明白你的理由。然而,我认为'http_redirect'这一行的写法是这样的,因为服务器实现了websocket,'scheme'(即'ws:')对于处理很重要。 [这里](https://support.rstudio.com/hc/en-us/articles/213733868-Running-Shiny-Server-with-a-Proxy)是一个参考。 – tchakravarty

+0

另外,你可以列出你想要我尝试的东西,包括输出?从你的评论中我不清楚。 – tchakravarty

+0

我正在查找的输出是来自http:// localhost/shiny/shiny_example的失败的wget行。你说过你看到一个你认为与301相关的404,但是没有提供任何输出,日志或其他细节。我已经更新了包含$ scheme重定向的答案,从提供的URL中我没有看到$ host/shiny_example,只有$ host/shiny / – BMitch