本文介绍了nginx作为负载平衡器:上行路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个nginx容器,在端口9200上充当负载均衡器。这是配置文件:

  proxy_ignore_headers Set-Cookie; 
add_header'Access-Control-Allow-Origin''*';
add_header'Access-Control-Allow-Headers''Range';


上游后端{
服务器192.168.99.103:9500 weight = 3;
服务器192.168.99.104:9500;
服务器192.168.99.105:9500;
}

server {

listen 80;
server_name 172.17.0.1;

location = /LynyrdSkynyrdFreebirdAudio.mp4 {

add_header'Access-Control-Allow-Origin''*';
add_header'Access-Control-Allow-Headers''Range';

if($ request_method = OPTIONS){
add_header Access-Control-Allow-MethodsGET,OPTIONS;
add_header Content-Length 0;
add_header Content-Type text / plain;
返回200;
}
proxy_pass http:// backend /;

add_header X-Upstream $ upstream_addr;

}

location = /LynyrdSkynyrdFreebirdVideo.mp4 {
add_header'Access-Control-Allow-Origin''*';
add_header'Access-Control-Allow-Headers''Range';

if($ request_method = OPTIONS){
add_header Access-Control-Allow-MethodsGET,OPTIONS;
add_header Content-Length 0;
add_header Content-Type text / plain;
返回200;
}

proxy_pass http://后端;

add_header X-Upstream $ upstream_addr;
}

}

此容器负载平衡请求在以下方式:到达时请求 localhost:9200 / LynyrdSkynyrdFreebirdVideo.mp4 localhost:9200 / LynyrdSkynyrdFreebirdAudio.mp4 ,它平衡3台服务器上的请求。
这3个服务器是3个用作缓存的nginx容器。
这是nginx缓存的配置文件:

  proxy_cache_path / tmp / nginx levels = 1:2 keys_zone = my_zone: 10m无效= 60m; 
proxy_cache_key$ scheme $ request_method $ host $ request_uri;
proxy_cache_methods GET HEAD POST;
proxy_cache_valid 200 206 100m;
proxy_ignore_headers Set-Cookie;
add_header'Access-Control-Allow-Origin''*'always;
add_header'Access-Control-Allow-Headers''Range';
server {
listen 80;
server_name 172.17.0.1;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

位置/ {
}

#error_page 404 /404.html;

#重定向服务器错误页面到静态页面/50x.html

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root / usr / share / nginx / html;
}

#代理PHP脚本到Apache在127.0.0.1:80收听

#location〜\.php $ {
# proxy_pass http://127.0.0.1;
#}

#将PHP脚本传递给FastCGI服务器侦听127.0.0.1:9000

#location〜\.php $ {
#root html;
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME / scripts $ fastcgi_script_name;
#include fastcgi_params;
#}

#拒绝访问.htaccess文件,如果Apache的文档根
#同nginx的一个

#location〜/ \ .ht {
#deny all;
#}

location / demo / {

proxy_cache my_zone;
add_header X-Proxy-Cache $ upstream_cache_status;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ remote_addr;

proxy_set_header主机$主机;

proxy_pass http://172.17.0.1:8700/shaka-player-master/demo/index1.html;

}

位置/媒体{

proxy_cache my_zone;
add_header X-Proxy-Cache $ upstream_cache_status;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ remote_addr;

proxy_set_header主机$主机;

proxy_pass http://172.17.0.1:8700/shaka-player-master/media/example-av.mpd;

}

location = /example-av1.mpd {
add_header'Access-Control-Allow-Origin''*';

proxy_cache my_zone;
add_header X-Proxy-Cache $ upstream_cache_status;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ remote_addr;

proxy_set_header主机$主机;

proxy_pass http://192.168.99.103:9600/shaka-player/media/example-av1.mpd;
}



location = /LynyrdSkynyrdFreebirdVideo.mp4 {
add_header'Access-Control-Allow-Origin''*';
add_header'Access-Control-Allow-Headers''Range';


proxy_cache my_zone;
add_header X-Proxy-Cache $ upstream_cache_status;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ remote_addr;

proxy_set_header主机$主机;

proxy_pass http://192.168.99.103:9600/shaka-player/media/LynyrdSkynyrdFreebirdVideo.mp4;

}

location = /LynyrdSkynyrdFreebirdAudio.mp4 {
add_header'Access-Control-Allow-Origin''*';
add_header'Access-Control-Allow-Headers''Range';


proxy_cache my_zone;
add_header X-Proxy-Cache $ upstream_cache_status;
proxy_set_header X-Real-IP $ remote_addr;
proxy_set_header X-Forwarded-For $ remote_addr;

proxy_set_header主机$主机;

proxy_pass http://192.168.99.103:9600/shaka-player/media/LynyrdSkynyrdFreebirdAudio.mp4;

}

}

如果我点要负载平衡器,即 localhost:9200 / LynyrdSkynyrdFreebirdVideo.mp4 localhost:9200 / LynyrdSkynyrdFreebirdAudio.mp4 code> 404 未找到。我希望,如果我指向这个URL,负载平衡器平衡请求到 192.168.99.103(或104或105):9500 / LynyrdSkynyrdFreebirdVideo.mp 4或 192.168.99.103(或104或105):9500 / LynyrdSkynyrdFreebirdAudio.mp4
但是在上游块中,我不知道如果在服务器指令中,我可以添加一个路径。实际上,这是我觉得这个问题:其实如果我指向192.168.99.103 (或 104 105 :9500 / LynyrdSkynyrdFreebirdAudio(或视频).mp4,我正确获取内容



任何人都可以帮助我吗?

解决方案

p>

我添加了路径到 http://后端,如:

  proxy_pass http://backend/LynyrdSkynyrdFreebirdVideo.mp4; 


i've an nginx container, on port 9200, that acts as load balancer. This is config file:

proxy_ignore_headers Set-Cookie;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Range';


upstream backend {
        server 192.168.99.103:9500 weight=3;
        server 192.168.99.104:9500;
        server 192.168.99.105:9500;
    }

server {

listen 80;
server_name 172.17.0.1;

location = /LynyrdSkynyrdFreebirdAudio.mp4 {

            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Headers' 'Range';

            if ($request_method = OPTIONS ) {
               add_header Access-Control-Allow-Methods "GET, OPTIONS";
               add_header Content-Length 0;
               add_header Content-Type text/plain;
               return 200;
                }
            proxy_pass http://backend/;

            add_header X-Upstream  $upstream_addr;

        }

       location = /LynyrdSkynyrdFreebirdVideo.mp4 {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Headers' 'Range';

            if ($request_method = OPTIONS ) {
               add_header Access-Control-Allow-Methods "GET, OPTIONS";
               add_header Content-Length 0;
               add_header Content-Type text/plain;
               return 200;
                }

            proxy_pass http://backend;

            add_header X-Upstream  $upstream_addr;
        }

   }

This container load balance requests in the following way: when arrives a request localhost:9200/LynyrdSkynyrdFreebirdVideo.mp4 or localhost:9200/LynyrdSkynyrdFreebirdAudio.mp4, it balances requests across 3 servers.These 3 servers are 3 nginx containers that acts as cache.This is config file of nginx caches:

proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_methods GET HEAD POST;
proxy_cache_valid 200 206 100m;
proxy_ignore_headers Set-Cookie;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Headers' 'Range';
server {
    listen       80;
    server_name  172.17.0.1;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

    location /demo/ {

    proxy_cache my_zone;
    add_header X-Proxy-Cache             $upstream_cache_status;
    proxy_set_header X-Real-IP           $remote_addr;
    proxy_set_header X-Forwarded-For     $remote_addr;

    proxy_set_header Host                $host;

    proxy_pass http://172.17.0.1:8700/shaka-player-master/demo/index1.html ;

    }

location   /media {

    proxy_cache my_zone;
    add_header X-Proxy-Cache             $upstream_cache_status;
    proxy_set_header X-Real-IP           $remote_addr;
    proxy_set_header X-Forwarded-For     $remote_addr;

    proxy_set_header Host                $host;

    proxy_pass http://172.17.0.1:8700/shaka-player-master/media/example-av.mpd ;

  }

location =  /example-av1.mpd {
    add_header 'Access-Control-Allow-Origin' '*';

    proxy_cache my_zone;
    add_header X-Proxy-Cache             $upstream_cache_status;
    proxy_set_header X-Real-IP           $remote_addr;
    proxy_set_header X-Forwarded-For     $remote_addr;

    proxy_set_header Host                $host;

    proxy_pass http://192.168.99.103:9600/shaka-player/media/example-av1.mpd;
  }



location =  /LynyrdSkynyrdFreebirdVideo.mp4 {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Headers' 'Range';


    proxy_cache my_zone;
    add_header X-Proxy-Cache             $upstream_cache_status;
    proxy_set_header X-Real-IP           $remote_addr;
    proxy_set_header X-Forwarded-For     $remote_addr;

    proxy_set_header Host                $host;

    proxy_pass http://192.168.99.103:9600/shaka-player/media/LynyrdSkynyrdFreebirdVideo.mp4 ;

  }

location =  /LynyrdSkynyrdFreebirdAudio.mp4 {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Headers' 'Range';


    proxy_cache my_zone;
    add_header X-Proxy-Cache             $upstream_cache_status;
    proxy_set_header X-Real-IP           $remote_addr;
    proxy_set_header X-Forwarded-For     $remote_addr;

    proxy_set_header Host                $host;

    proxy_pass http://192.168.99.103:9600/shaka-player/media/LynyrdSkynyrdFreebirdAudio.mp4;

  }

}

If i point to load balancer, i.e. localhost:9200/LynyrdSkynyrdFreebirdVideo.mp4 or localhost:9200/LynyrdSkynyrdFreebirdAudio.mp4, there is 404 not found. I expect that, if i point to this URL, load balancer balances request to 192.168.99.103(or 104, or 105):9500/LynyrdSkynyrdFreebirdVideo.mp4 or 192.168.99.103(or 104, or 105):9500/LynyrdSkynyrdFreebirdAudio.mp4.But in the upstream block i don't know if in server directive i can add a path.

In fact, this is, i think, the very problem: in fact, if i point to 192.168.99.103(or104, or105):9500/LynyrdSkynyrdFreebirdAudio(or Video).mp4, i get the content correctly.

Can anyone helps me?

解决方案

Solved:

i have added path to http://backend, like:

  proxy_pass http://backend/LynyrdSkynyrdFreebirdVideo.mp4;

这篇关于nginx作为负载平衡器:上行路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:53