NGINX:基于Cookie的用户仅针对特定URL进行重定向 [英] NGINX: Redirect a user based on cookie for a specific URL only

查看:397
本文介绍了NGINX:基于Cookie的用户仅针对特定URL进行重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个封闭的社交网络,当前当用户未登录时,他们将始终被重定向到我的域的主页.

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:

  1. 使用NGINX来检查用户是否已登录(通过检查cookie),然后在他们转到主页(mydomain.com)时重定向到mydomain.com/newsfeed.

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆