使用302时,Nginx负载平衡器错误

时间:2017-01-25 16:33:58

标签: nginx cors load-balancing

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

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,因为跨站点来源。 然后响应头显示在配置代码中。

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

  • XMLHttpRequest无法加载 http:// localhost:9200 / LynyrdSkynyrdFreebirdVideo.mp4。重定向 'http:// localhost: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政策:请求需要预检,这是不允许的 遵循跨源重定向。

请注意我做了

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

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

3 个答案:

答案 0 :(得分:0)

您的nginx配置正在向浏览器返回302。

换句话说,您尝试将请求从“localhost”重定向到“cdn-video”(除非您已配置本地DNS或主机文件映射)将无法在浏览器中解析。

您还应该将CORS方法标头添加到您的nginx配置

add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

我建议将你的nginx配置简化为(未经测试)的内容:

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

upstream cdn-video {
  least_conn;
  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 /audio/ {
    if ($request_method = OPTIONS) {
      add_header Access-Control-Allow-Origin '*';
      add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
      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-audio; # expects the audio files to be in a "audio" folder on the upstream box
    add_header X-Upstream  $upstream_addr;
    add_header Host $host;
  }

  location /video/ {
    if ($request_method = OPTIONS) {
      add_header Access-Control-Allow-Origin '*' ;
      add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
      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; # expects the video files to be in a "video" folder on the upstream box
    add_header X-Upstream  $upstream_addr;
    add_header Host $host;
  }
}

答案 1 :(得分:0)

@sideshowbarker这是请求和响应标头,如果是proxy_pass和重定向302。

header options + get chunk with proxy pass:

OPTIONS

General

Request URL:http://localhost:9200/LynyrdSkynyrdFreebirdVideo.mp4
Request Method:OPTIONS
Status Code:200 OK
Remote Address:127.0.0.1:9200

Response Headers

view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Range
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:0
Content-Length:0
Content-Type:text/plain
Content-Type:video/mp4
Date:Thu, 26 Jan 2017 08:41:52 GMT
Host:localhost
Server:nginx/1.11.6

Request Headers

view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:range
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:9200
Origin:http://localhost:8400
Referer:http://localhost:8400/shaka-player-master/demo/homepage.html
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36


GET

General

Request URL:http://localhost:9200/LynyrdSkynyrdFreebirdAudio.mp4
Request Method:GET
Status Code:206 Partial Content
Remote Address:127.0.0.1:9200

Response Headers

view source
Access-Control-Allow-Headers:Range
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:692
Content-Range:bytes 1040-1731/6797033
Content-Type:video/mp4
Date:Thu, 26 Jan 2017 08:41:52 GMT
ETag:W/"6797033-1484728792000"
Host:localhost
Last-Modified:Wed, 18 Jan 2017 08:39:52 GMT
Server:nginx/1.11.6
X-Proxy-Cache:MISS
X-Upstream:192.168.99.103:9500

Request Headers

view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:localhost:9200
Origin:http://localhost:8400
Range:bytes=1040-1731
Referer:http://localhost:8400/shaka-player-master/demo/homepage.html
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36




header options and get chunk with redirect:

OPTIONS:

General

Request URL:http://localhost:9200/LynyrdSkynyrdFreebirdVideo.mp4
Request Method:OPTIONS
Status Code:200 OK
Remote Address:127.0.0.1:9200

Response Headers

view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Range
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:0
Content-Length:0
Content-Type:text/plain
Content-Type:video/mp4
Date:Thu, 26 Jan 2017 08:52:22 GMT
Host:localhost
Server:nginx/1.11.6

Request Headers

view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:range
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:9200
Origin:http://localhost:8400
Referer:http://localhost:8400/shaka-player-master/demo/homepage.html
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

GET

General

Request URL:http://localhost:9200/LynyrdSkynyrdFreebirdVideo.mp4
Request Method:GET
Status Code:302 Moved Temporarily
Remote Address:127.0.0.1:9200

Response Headers

view source
Connection:keep-alive
Content-Length:161
Content-Type:text/html
Date:Thu, 26 Jan 2017 08:52:22 GMT
Host:localhost
Location:http://cdn-video/LynyrdSkynyrdFreebirdVideo.mp4
Server:nginx/1.11.6

Request Headers

view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:localhost:9200
Origin:http://localhost:8400
Range:bytes=1136-1767
Referer:http://localhost:8400/shaka-player-master/demo/homepage.html
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

Initiator = http://localhost:9200/LynyrdSkynyrdFreebirdVideo.mp4  ; type= xhr

SECOND GET:

General

Request URL:http://localhost:9200/LynyrdSkynyrdFreebirdVideo.mp4
Request Method:GET
Status Code:302 Moved Temporarily
Remote Address:127.0.0.1:9200

Response Headers

view source
Connection:keep-alive
Content-Length:161
Content-Type:text/html
Date:Thu, 26 Jan 2017 08:52:22 GMT
Host:localhost
Location:http://cdn-video/LynyrdSkynyrdFreebirdVideo.mp4
Server:nginx/1.11.6

Request Headers

view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:localhost:9200
Origin:http://localhost:8400
Range:bytes=1136-1767
Referer:http://localhost:8400/shaka-player-master/demo/homepage.html
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36


Initiator = "other"; type= text/html.

答案 2 :(得分:0)

使用谷歌金丝雀解决。

使用金丝雀,如果我将内容直接重定向到我的三个上游服务器之一,它就可以工作。 如果我重定向到上游名称(即cdn-audio(或视频)),则浏览器无法解析名称。

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