how to create session persistence or hash_ip within server context for an if statement
Reinis Rozitis
r at roze.lv
Mon Jan 24 17:15:20 UTC 2022
> I got it working! I am not sure of the performance hit, maybe someone know
how to make this more effective?
Without going deeper on what are your actual requirements and what is the
expected result, the suggestion I could give is instead of the multiple if
statements to look at the map directive
(http://nginx.org/en/docs/http/ngx_http_map_module.html ) - it is more
powerful and performant.
For example all the $geoip2_data_country_iso ifs could be:
map $geoip2_data_country_iso_code $MAGE_RUN_CODE_ORG {
NL storeview07;
US storeview08;
DE storeview09;
// optionally a default which will be set for all non-matching country
codes
}
Since you can also chain the map directives the same probably can be done
with the $redirect logic - rather than multiple ifs you could group the
checks for same conditions together.. but I'm not exactly sure I understand
the reason behind the /noredirect/ location and rewriting:
if ($request_uri ~ "^/(noredirect)/(.*)$" ) {
set $redirect false;
}
location ~ ^/(noredirect)/ {
add_header Set-Cookie
"REDIRECT=false;Domain=$host;Path=/;Max-Age=31536000";
rewrite ^/noredirect(.*)$ https://$host$1 redirect;
}
Some thoughts:
- if you have already a location match the if check (for the same uri) is
redundant you could just set the variable in the location block
- extracting regexp parts of the without using them makes little sense -
since you do rewrite ^/noredirect(.*)$ https://$host$1 redirect; the
"^/(noredirect)/(.*)$" and ~ ^/(noredirect)/ is not needed - just using
location ^~ /noredirect or location ~ ^/noredirect (depending on the
location match priority) would be enough
- in the end in one location check $redirect is being set to false and then
in in the same location still a redirect happens is confusing (at least) to
me
p.s. sometimes writing the application logic in pure webserver configuration
is cumbersome - dynamic languages like Lua (can be embedded in nginx) / Perl
or php could do a better job.
rr
More information about the nginx
mailing list