nginx – >使用基于cookie的try_files服务不同的文件(条件,如果,然后,其他)

我需要configuration一个nginx服务器,以便:如果用户有一个特定的cookie,所以服务器必须发送一个文件,否则服务器必须发送另一个。 我读了很多相关的文章,但没有什么帮助,也知道当try_files遇见的location if有一些问题,但如何解决这个问题…

我有一个例子,应该是有效的,但事实上并非如此:

 upstream site { server localhost:4321; } server { listen *:1234; server_name site.com; root /path/www/; ssl on; ssl_certificate /path/ssl.crt; ssl_certificate_key /path/ssl.key; location = / { set $variable /path/index.html; if ($http_cookie ~* "someCookie=someValue(?:;|$)") { set $index_file /path2/index.html; } rewrite .* $index_file break; try_files $uri/index.html $uri.html $uri @site; } } 

尝试这样的事情:

 if ($cookie_myCookieNameIsBlaBlaBla ~* "cookieValueThatIWannaMatch") { # my logic in here } 

只要记住,根据nginx的文档,如果是邪恶的,所以要小心。

更多信息:

氰!

我这样做,不知道最好的解决scheme,但它确实有效。

 location @rewrite { if ($http_cookie ~* "someCookie=someValue(?:;|$)") { rewrite .* /path1/file1.html last; } rewrite .* /path2/file2.html last; } location = / { try_files $uri/index.html $uri.html $uri @rewrite; }