如果 cookie 存在,Nginx 重定向

标签 nginx

我看到了一些关于使用 Nginx 检查 cookie 的有限资源,但我无法真正找到我正在寻找的答案,希望各位 Nginx 高手能够帮助我。

本质上,我有一个虚拟主机,我想将其重定向到不同的域,除非用户有 cookie,这是我创建的:

server {
  listen 80;
  server_name example.com;

  if ($http_cookie ~* "dev_cookie" ) {
    root /home/deploy/apps/example/current/public;
    passenger_enabled on;
    rack_env production;
    break;
  }
  rewrite ^/(.*) http://beta.example.com/$1 permanent;
}

但它似乎不起作用,我收到错误:

[emerg]: "root" directive is not allowed here in /opt/nginx/conf/nginx.conf:45

我不知道如何继续这里,有什么想法吗?

最佳答案

这是有道理的。 我将使用不同的根文件夹定义另一个虚拟主机(beta.example.com) 并在遇到 cookie 时进行重写

您不能有条件地为一个域设置不同的根,但可以有条件地重定向(重写)到另一个域

这个人的例子之前对我有帮助 http://nicknotfound.com/2009/01/12/iphone-website-with-nginx/

关于如果 cookie 存在,Nginx 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2936329/

相关文章:

nginx - Certbot 没有创建 acme-challenge 文件夹

PHP 不从网页执行 [但在 Raspberry Pi 上可以从 PHP-CLI 执行]

ubuntu - 如何获取 nginx 使用的当前 worker 连接?

nginx - 为meteor推荐的nginx配置

nginx - 如何在 Laravel Homestead 盒子上设置 phpMyAdmin?

nginx 未知指令 health_check

nginx - keycloak (Nginx) 的反向代理配置

tomcat - 当代理服务器关闭时,NGINX 反向代理返回 502 错误网关

performance - 如何 : the minimal server to serve zero length answers

wordpress - Nginx 作为 Docker VHosts 的反向代理