Nginx – redirectcookie逻辑导致404错误

我有以下configuration:

upstream ring { server 127.0.0.1:3000 fail_timeout=0; } server { root /home/app/public; index index.dev.html; server_name localhost; location / { try_files $uri $uri/ @ring; } location @ring { proxy_pass http://ring; proxy_set_header Host $http_host; proxy_buffering off; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 

它用作文件,然后是目录,然后代理到应用程序服务器。 这按预期工作。 但是现在,我希望在用户第一次访问该网站时将用户redirect到about.html页面。 所以我改变了以下configuration:

 upstream ring { server 127.0.0.1:3000 fail_timeout=0; } server { root /home/app/public; index index.dev.html; server_name localhost; location / { set $seen false; if ($http_cookie ~* "seen") { set $seen true; } if ($seen = false) { add_header Set-Cookie seen=1; return 302 $scheme://$host/about.html; } try_files $uri $uri/ @ring; } location @ring { proxy_pass http://ring; proxy_set_header Host $http_host; proxy_buffering off; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 

现在redirect逻辑工作(没有cookieredirect到about.html与一个新的cookie集,随后的请求落在主index.html页面)。 但是,现在任何url到应用程序服务器导致404错误。 @ring的位置已经不在服务了。

我可能犯了一个愚蠢的错误,但不明白nginx足够好的debugging。

非常感谢。

好的,在nginx( https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ )中显示“if is evil”,这是一个由于“if”是必要的错误,其余的configuration是声明性的。

下面的变化起作用,做我想要的一切。 诀窍是除了返回或者重写之外,什么都不做。 然后还要使用nginx 418的内部redirect代码。

 upstream ring { server 127.0.0.1:3000 fail_timeout=0; } server { root /home/app/public; index index.dev.html; server_name localhost; location / { error_page 418 = @about; if ($cookie_seen != 1) { return 418; } try_files $uri $uri/ @ring; } location @about { add_header Set-Cookie seen=1; return 302 $scheme://$host/about.html; } location @ring { proxy_pass http://ring; proxy_set_header Host $http_host; proxy_buffering off; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }