简体   繁体   English

基于源IP的Nginx TCP流路由

[英]Nginx TCP stream routing based on source IP

I have configured nginx as a reverse proxy for a TCP (non-http) stream.我已将 nginx 配置为 TCP(非 http)流的反向代理。 I'd like to apply different routing for a particular source IP address - can this be done, and how?我想为特定的源 IP 地址应用不同的路由 - 这可以完成吗,如何完成? I'm aware of recomendations for the http module using the if directive, but that doesn't seem to work for these streams.我知道使用if指令对 http 模块的推荐,但这似乎不适用于这些流。

Existing configuration:现有配置:

stream {
  server {
    listen 8000;
    proxy_pass staging;
  }
}

upstream staging {
    server 1.2.3.4:8000;
}

Desired configuration (not working):所需的配置(不工作):

stream {
  server {
    listen 8000;
    proxy_pass staging1;
    if ( $remote_addr ~* 4.5.6.7 ) {
        proxy_pass staging2;
    }
  }
}

upstream staging1 {
    server 1.2.3.4:8000;
}
upstream staging2 {
    server 1.2.3.44:8000;
}

This gives error '24314#24314: "if" directive is not allowed here', since it doesn't apply for the stream module - is there any other functionality how I could achieve this result?这会产生错误“24314#24314:此处不允许使用“if”指令”,因为它不适用于流模块 - 是否还有其他功能可以实现此结果?

in case some people are still wondering you can achieve this using a map:如果有些人仍然想知道您可以使用地图来实现这一点:

stream {
  upstream staging1 {
    server 1.2.3.4:8000;
  }

  upstream staging2 {
    server 1.2.3.44:8000;
  }

  map $remote_addr $backend_svr {
    4.5.6.7 "staging2";
    default "staging1";
  }
  server {
    listen 8000;
    proxy_pass $backend_svr;
  }
}

ref 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM