nginx:代理tcpstream量取决于远程ip

我想用nginx代理tcpstream量到不同的后端,这取决于请求者的IP地址。 到目前为止,我在nginx.conf中有以下设置:

stream { server { listen 1025; if ($remote_addr != 10.0.99.18) { proxy_pass mailcatcher_production:1025; } if ($remote_addr = 10.0.99.18) { proxy_pass mailcatcher_others:1025; } } } 

但是在启动nginx时出现以下错误:

[emerg] 1#1: "if" directive is not allowed here in /etc/nginx/nginx.conf:36

我认为这并不重要,但我在docker中运行nginx,并在启动时将nginx.conf复制到容器中。

尝试使用地图模块:

 stream { map $remote_addr $mailcatcher { 10.0.99.18 mailcatcher_others; default mailcatcher_production; } server { listen 1025; proxy_pass $mailcatcher:1025; } }