使用302时Nginx负载均衡器错误 [英] Nginx load balancer error when 302 is used

查看:746
本文介绍了使用302时Nginx负载均衡器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个nginx容器用作负载平衡器,用于基于DASH的流式传输服务,还有3个VM用作上游服务器. 这是nginx的配置文件:

I've an nginx container used as load balancer, for a streaming DASH-based service.There are also 3 VM used as upstream servers. This is nginx config file:

upstream cdn-audio {
        server 192.168.99.103:9500;
        server 192.168.99.104:9500;
        server 192.168.99.105:9500;
   }

upstream cdn-video {

        server 192.168.99.103:9500;
        server 192.168.99.104:9500;
        server 192.168.99.105:9500;
    }


server {

listen 80;
server_name 172.17.0.1;
access_log /var/log/nginx/acces.log main;

location = /LynyrdSkynyrdFreebirdAudio.mp4 {

#           proxy_pass http://192.168.99.103:9500;

# proxy_pass http://cdn-audio/LynyrdSkynyrdFreebirdAudio.mp4;
add_header X-Upstream  $upstream_addr;
add_header Host $host;
if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin '*';
        add_header Access-Control-Allow-Headers "Authorization,Range";
        add_header Access-Control-Allow-Credentials "true";
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        add_header Host $host;
        return 200;
    }

       return 302 $scheme://cdn-audio/LynyrdSkynyrdFreebirdAudio.mp4;

        }

location = /LynyrdSkynyrdFreebirdVideo.mp4 {

add_header X-Upstream  $upstream_addr;
#  proxy_pass http://cdn-audio/LynyrdSkynyrdFreebirdVideo.mp4;
add_header Host $host;


if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin '*' ;
        add_header Access-Control-Allow-Headers "Authorization,Range";
        add_header Access-Control-Allow-Credentials "true";
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        add_header Host $host;
        return 200;
}


#       proxy_pass http://cdn-video$request_uri;

#       proxy_pass http://192.168.99.103:9500;

       return 302 $scheme://cdn-video/LynyrdSkynyrdFreebirdVideo.mp4;
#       add_header X-Upstream  $upstream_addr;

        }

   }

在另一个容器(前端容器)中托管的html页面中,由于跨站点起源,首先存在一个HTTP OPTIONS请求::/LynyrdSkynyrdFreebirdAudio.mp4和:/LynyrdSkynyrdFreebirdVideo.mp4. 然后响应头显示在配置代码中.

From an html page hosted in another container ( a frontend container), first there is a HTTP OPTIONS request to :/LynyrdSkynyrdFreebirdAudio.mp4 and :/LynyrdSkynyrdFreebirdVideo.mp4, because of cross site origin. Then the response header is shown in the config code.

然后,当我尝试将请求重定向到三个上游服务器之一时,出现错误:

Then, when i try to redirect request to one of my three upstream server, there is an error:

  • XMLHttpRequest无法加载 http://本地主机:9200/LynyrdSkynyrdFreebirdVideo.mp4.重定向自 'http://本地主机:9200/LynyrdSkynyrdFreebirdVideo.mp4'到 'http://cdn-video/LynyrdSkynyrdFreebirdVideo.mp4'已被屏蔽 CORS政策:请求需要进行飞行前检查,这是不允许的 遵循跨域重定向.
  • XMLHttpRequest无法加载 http://localhost:9200/LynyrdSkynyrdFreebirdAudio.mp4.重定向自 'http://localhost:9200/LynyrdSkynyrdFreebirdAudio.mp4'到 'http://cdn-audio/LynyrdSkynyrdFreebirdAudio.mp4'已被屏蔽 CORS政策:请求需要进行飞行前检查,这是不允许的 遵循跨域重定向.
  • XMLHttpRequest cannot load http: // localhost:9200/LynyrdSkynyrdFreebirdVideo.mp4. Redirect from 'http: // localhost:9200/LynyrdSkynyrdFreebirdVideo.mp4' to 'http: // cdn-video/LynyrdSkynyrdFreebirdVideo.mp4' has been blocked by CORS policy: Request requires preflight, which is disallowed to follow cross-origin redirect.
  • XMLHttpRequest cannot load http: // localhost:9200/LynyrdSkynyrdFreebirdAudio.mp4. Redirect from 'http: // localhost:9200/LynyrdSkynyrdFreebirdAudio.mp4' to 'http: // cdn-audio/LynyrdSkynyrdFreebirdAudio.mp4' has been blocked by CORS policy: Request requires preflight, which is disallowed to follow cross-origin redirect.

请注意,我做了

curl -I -X OPTIONS http://192.168.99.103:9500/LynyrdSkynyrdFreebirdAudio.mp4

这是回应:

HTTP/1.1 200 OK
Server: nginx/1.11.8
Date: Wed, 25 Jan 2017 16:31:28 GMT
Content-Length: 0
Connection: keep-alive
Allow: GET, HEAD, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Range

您能帮我解决这个问题吗?

Can you help me to solve this problem?

推荐答案

使用Google Canary解析.

Resolved using google canary.

使用canary,如果我将内容直接重定向到我的三个上游服务器之一,它将起作用. 如果我重定向到上游名称(即CDN-音频(或视频)),浏览器将无法解析名称.

With canary, it works if i redirect content directly to one of my three upstream server. If i redirect to upstream name (i.e. cdn-audio(or video)) the browser cannot resolve name.

我听说过Nginx的lua-upstream-module,有人可以帮我吗?

I've heard about lua-upstream-module for nginx, can someone help me?

这篇关于使用302时Nginx负载均衡器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆