NGINX:基于Cookie的用户仅针对特定URL进行重定向 [英] NGINX: Redirect a user based on cookie for a specific URL only
问题描述
我正在建立一个封闭的社交网络,当前当用户未登录时,他们将始终被重定向到我的域的主页.
I'm building a closed social network and currently when a user is not logged in they will always be redirected to the homepage of my domain.
我想做的是做以下事情:
What I would like to do is do the following:
-
使用NGINX来检查用户是否已登录(通过检查cookie),然后在他们转到主页(mydomain.com)时重定向到mydomain.com/newsfeed.
Use NGINX to check if a user is logged in (through checking for a cookie) and then when they go to the homepage (mydomain.com) redirect to to mydomain.com/newsfeed.
此检查仅应在用户浏览到主页且不应该使用其他任何网址(否则它们将始终被重定向)时应用.
This check should only be applied when a user brows to the homepage and should not work at ANY other url (or else they would always be redirected).
我对NGINX还是陌生的,他研究了使用cookie进行重定向的各种教程,但未能获得答案(最著名的是将重定向限制为仅首页).
I'm very new to NGINX and looked at various tutorials for using cookies for redirect but failed to get an answer (most notably to limiting the redirect to only the homepage).
提前谢谢!
推荐答案
最终正确的解决方案:
location ~* ^/$ {
if ($http_cookie ~* "wordpress_logged_in") {
return 301 http://example.com/newsfeed/;
}
}
这篇关于NGINX:基于Cookie的用户仅针对特定URL进行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!