nginx 位置块捕获一个值并设置 cookie,如果它不为空,则重定向到新 URL
nginx location block capturing a value and setting cookie if it's not empty, redirect to new URL
很难用标题来概括...
我试图创建一个 URL 将指定的值保存到 cookie,然后重定向用户以删除 URL 中的值。
愿望:
domain.com/importantpage => 重定向到 importantpage.php(不要修改 cookie!)
domain.com/importantpage/ => 重定向到 importantpage.php(不要修改 cookie!)这个不重要,但很好
domain.com/importantpage/aSECRETvalue33 => 将 secret_code cookie 设置为 aSECRETvalue33 并重定向到 importantpage.php
我目前写的:
location ~ ^/importantpage/?(.*?)$ {
add_header Set-Cookie "secret_code=;Domain=$server_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
return 307 $scheme://$server_name/importantpage.php;
}
这还不够好,因为域。com/importantpage(带或不带斜线)将 cookie 设置为空白值。我希望用户能够导航到 domain.com/importantpage 并让它读取现有的 cookie(如果已提供)。
我想像这样在两个地方打破它:
location ~ ^/importantpage$ {
return 307 $scheme://$server_name/importantpage.php;
}
location ~ ^/importantpage/(.*?)$ {
add_header Set-Cookie "secret_code=;Domain=$server_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
return 307 $scheme://$server_name/importantpage.php;
}
但出于某种我不确定的原因...它并不像我想象的那样有效。从理论上讲,最上面的一个应该只被域调用。com/importantpage 并避免设置 cookie..但是如果提供了 /(有或没有值),第二个应该被调用...但是相反当我导航到域时。com/importantpage 它仍然设置一个空值的 cookie?即使第二个需要一个尾部斜线来匹配它?也许 non-obvious 在幕后发生了一些事情。
有人有什么想法吗?也许有一种方法可以强制正则表达式匹配 1 个或多个 non-whitespace 个字符,或者一个 IF 语句只在值不为空时才调用 setcookie?
在单块代码段中,^/importantpage/?(.*?)$
匹配 /importantpage
后跟零个或一个斜杠后跟零个或多个字符后跟输入结尾,因此 it matches /importantpage/aSECRETvalue33
, /importantpage
, and /importantpageisnothere/some/other/path
。请注意,.*?
类似于 .*
(任何字符的零个或多个)但非贪婪匹配,在这种情况下这是不必要的,因为您允许跟随它的唯一字符是结尾无论如何输入。
在双块代码段中,由于两者都是非精确前缀,nginx looks for the longest match。您可以改为将块分成完全匹配和前缀匹配:
location = /importantpage {
return 307 $scheme://$server_name/importantpage.php;
}
location = /importantpage/ {
return 307 $scheme://$server_name/importantpage.php;
}
location ~ ^/importantpage/(.+)$ {
add_header Set-Cookie "secret_code=;Domain=$server_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
return 307 $scheme://$server_name/importantpage.php;
}
或者您只能通过将正则表达式修复为仅在 /
:
后面有字符时才匹配来处理第二种情况
location ~ ^/importantpage/(.+)$ {
add_header Set-Cookie "secret_code=;Domain=$server_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
return 307 $scheme://$server_name/importantpage.php;
}
很难用标题来概括... 我试图创建一个 URL 将指定的值保存到 cookie,然后重定向用户以删除 URL 中的值。
愿望:
domain.com/importantpage => 重定向到 importantpage.php(不要修改 cookie!)
domain.com/importantpage/ => 重定向到 importantpage.php(不要修改 cookie!)这个不重要,但很好
domain.com/importantpage/aSECRETvalue33 => 将 secret_code cookie 设置为 aSECRETvalue33 并重定向到 importantpage.php
我目前写的:
location ~ ^/importantpage/?(.*?)$ {
add_header Set-Cookie "secret_code=;Domain=$server_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
return 307 $scheme://$server_name/importantpage.php;
}
这还不够好,因为域。com/importantpage(带或不带斜线)将 cookie 设置为空白值。我希望用户能够导航到 domain.com/importantpage 并让它读取现有的 cookie(如果已提供)。
我想像这样在两个地方打破它:
location ~ ^/importantpage$ {
return 307 $scheme://$server_name/importantpage.php;
}
location ~ ^/importantpage/(.*?)$ {
add_header Set-Cookie "secret_code=;Domain=$server_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
return 307 $scheme://$server_name/importantpage.php;
}
但出于某种我不确定的原因...它并不像我想象的那样有效。从理论上讲,最上面的一个应该只被域调用。com/importantpage 并避免设置 cookie..但是如果提供了 /(有或没有值),第二个应该被调用...但是相反当我导航到域时。com/importantpage 它仍然设置一个空值的 cookie?即使第二个需要一个尾部斜线来匹配它?也许 non-obvious 在幕后发生了一些事情。
有人有什么想法吗?也许有一种方法可以强制正则表达式匹配 1 个或多个 non-whitespace 个字符,或者一个 IF 语句只在值不为空时才调用 setcookie?
在单块代码段中,^/importantpage/?(.*?)$
匹配 /importantpage
后跟零个或一个斜杠后跟零个或多个字符后跟输入结尾,因此 it matches /importantpage/aSECRETvalue33
, /importantpage
, and /importantpageisnothere/some/other/path
。请注意,.*?
类似于 .*
(任何字符的零个或多个)但非贪婪匹配,在这种情况下这是不必要的,因为您允许跟随它的唯一字符是结尾无论如何输入。
在双块代码段中,由于两者都是非精确前缀,nginx looks for the longest match。您可以改为将块分成完全匹配和前缀匹配:
location = /importantpage {
return 307 $scheme://$server_name/importantpage.php;
}
location = /importantpage/ {
return 307 $scheme://$server_name/importantpage.php;
}
location ~ ^/importantpage/(.+)$ {
add_header Set-Cookie "secret_code=;Domain=$server_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
return 307 $scheme://$server_name/importantpage.php;
}
或者您只能通过将正则表达式修复为仅在 /
:
location ~ ^/importantpage/(.+)$ {
add_header Set-Cookie "secret_code=;Domain=$server_name;Path=/;Max-Age=31536000;Secure;HTTPOnly" always;
return 307 $scheme://$server_name/importantpage.php;
}