NGinx:如何在不使用'的情况下测试是否设置了cookie;如果';?

NGinx:如何在不使用'的情况下测试是否设置了cookie;如果';?,nginx,Nginx,我目前正在使用NGinx的以下配置来测试我的应用程序: location / { # see if the 'id' cookie is set, if yes, pass to that server. if ($cookie_id){ proxy_pass http://${cookie_id}/$request_uri; br

我目前正在使用NGinx的以下配置来测试我的应用程序:

location / {
            # see if the 'id' cookie is set, if yes, pass to that server.           
            if ($cookie_id){
                proxy_pass http://${cookie_id}/$request_uri;                
                break;
            }

            # if the cookie isn't set, then send him to somewhere else
            proxy_pass http://localhost:99/index.php/setUserCookie;
        }
但他们说的是“”。有谁能告诉我一种不用“如果”就能完成同样工作的方法吗
还有,我对“if”的用法是否有问题?

就nginx而言,“if”是邪恶的有两个原因。一是互联网上的许多howto将直接将htaccess重写规则转换为一系列ifs,而单独的服务器或位置将是更好的选择。其次,nginx的if语句的行为与大多数人期望的不同。它的作用更像一个嵌套的位置,有些设置并不像您期望的那样继承。对其行为进行了解释

也就是说,像cookies这样的检查必须通过ifs完成。只要确保您阅读并理解了ifs是如何工作的(尤其是关于指令继承的),您就可以了

您可能需要重新考虑盲目代理cookie中设置的任何主机。也许将cookie与a结合起来以限制后端

编辑:如果在id cookie中使用名称而不是ip地址,那么还需要定义一个解析器,以便nginx可以查找后端地址。此外,默认的代理\u过程将把请求附加到setUserCookie的末尾。如果您希望代理到该url,请将该默认代理\u过程替换为:

rewrite ^ /index.php/setUserCookie break;
proxy_pass http://localhost:99;

谢谢你的评论和建议。。。你那里的编辑很有趣,我没想到。。谢谢你……:)