【问题标题】:nginx plus with proxy_next_upstream directive doesnt work带有 proxy_next_upstream 指令的 nginx plus 不起作用
【发布时间】:2017-08-16 08:07:28
【问题描述】:

问题陈述: 我想使用 nginx plus 设置主动故障转移(我订阅了 30 天试用版)。 所有服务器都应该转到主服务器,如果出现故障(404),那么请求应该转到第二台服务器。一旦主服务器启动,请求应该返回到原始服务器。可能吗? 在其他线程的帮助下,我能够创建以下配置文件。几乎所有我能找到的错误代码,我都尝试过 proxy_next_upstream,但我仍然无法达到预期的结果。

我手动关闭了主服务器以返回 404。当它关闭时它会短暂返回 503。但是重定向流量仍然没有运气。 两台服务器都作为 nodejs 应用程序托管在 IBM Bluemix 上。如果需要,我可以分享更多详细信息。

upstream up1 {
   server up_server1;
}

upstream up2 {

server up_server2;

}


server {
   listen 80;

   location / {

 proxy_pass http://up1;
         proxy_next_upstream non_idempotent invalid_header error timeout http_500 http_502 http_504 http_403 http_404;
   }
}

这由另一个看起来像的配置文件控制。只是为了提供更多信息

    user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
#    geoip_city         /etc/nginx/geoip/GeoLiteCity.dat;
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


# TCP/UDP proxy and load balancing block
#
#stream {
    # Example configuration for TCP load balancing

    #upstream stream_backend {
    #    zone tcp_servers 64k;
    #    server backend1.example.com:12345;
    #    server backend2.example.com:12345;
    #}

    #server {
    #    listen 12345;
    #    status_zone tcp_server;
    #    proxy_pass stream_backend;
    #}
#}

【问题讨论】:

    标签: nginx ibm-cloud load-balancing failover


    【解决方案1】:

    您的问题是您没有以任何方式使用upstream up2proxy_next_upstream 表示您当前上游的下一个服务器 - 在proxy_pass http://up1 中定义为“up1”,而不是神奇地选择任何其他上游。

    所以你想要的是删除upstream up2然后离开:

    upstream up1 {
       server up_server1;
       server up_server2;
    }
    

    【讨论】:

      【解决方案2】:

      我能够通过一个小调整来解决这个问题,而堆栈溢出中发布的几乎所有答案都错过了这个问题。当您定义 2 个不同的上游以与 proxy_next_upstream 一起使用时,您需要将服务器添加为原始上游指令中的第二个条目。查看下面的代码。

      upstream up1 {
         server up_server1;
         server up_server2; # this entry is important, but not sure why!
      }
      
      upstream up2 {
      
      server up_server2;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-27
        • 2023-03-05
        • 2015-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多