Nginx geoip 重定向,但排除 URL [英] Nginx geoip redirect , but exclude URL

查看:93
本文介绍了Nginx geoip 重定向,但排除 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在 Nginx 中对 GeoIP 重定向进行了以下设置:

Currently, I have the following setup for GeoIP redirect in Nginx:

set $fromAU 0;
set $isAUuri 0;
set $isUSuri 0;
if ( $geoip_country_code = "AU" ) { set $fromAU 1; }
if ( $request_uri ~* "^/australia" ) { set $isAUuri 1; }
if ( $request_uri ~* "^/usa-can" ) { set $isUSuri 1; }
if ( $request_uri ~* ^/(wp-admin|wp-login\.php) ) { set $isUSuri 1; }
set $redirectableAU "${fromAU}${isAUuri}";
if ( $redirectableAU = "10"  ) { rewrite "^(/australia)?(.*)" $scheme://$host/australia permanent; }
if ( $redirectableAU = "01"  ) { rewrite "^(/australia)?(.*)" $scheme://$host/usa-can permanent; }
set $redirectableUS "${fromAU}${isUSuri}";
if ( $redirectableUS = "00"  ) { rewrite "^(/australia)?(.*)" $scheme://$host/usa-can permanent; }

按预期工作,但有一个小问题:

which works as intended with one slight issue:

domain.com/wp-admin -> 按预期工作

domain.com/wp-admin -> works as intended

domain.com/usa-can/wp-admin -> 按预期工作

domain.com/usa-can/wp-admin -> works as intended

domain.com/australia/wp-admin -> 重定向到/usa-can

domain.com/australia/wp-admin -> redirects to /usa-can

我正在尝试为美国和 AU 地区制定 GeoIP 重定向,它在前端工作得很好,但问题在于最后的 wp-admin 重定向,任何见解将不胜感激:)

I am trying to work out the GeoIP redirect for the USA and AU regions, which on the frontend works just fine, but the issue is with last wp-admin redirect, any insights will be greatly appreciated :)

推荐答案

我会使用 nginx 的 map 模块,因为它针对快速查找进行了优化.https://nginx.org/en/docs/http/ngx_http_map_module.html

I would use the map module from nginx as it is optimized for fast lookups. https://nginx.org/en/docs/http/ngx_http_map_module.html

# partly adopted from 
# https://www.digitalocean.com/community/tutorials/how-to-use-nginx-s-map-module-on-ubuntu-16-04

# My countries
#
map $geoip_country_code $fromAU {
    default 0;
    AU 1;
}

map $request_uri $redirect_uri {
    default /wp-admin;
     ~*^/australia /australia/wp-admin;
     ~*^/usa-can /usa-can/wp-admin;
}

if ( $fromAU = "1"  ) { redirect $scheme://$host/$redirect_uri permanent; }
if ( $fromAU = "0"  ) { redirect $scheme://$host/$redirect_uri permanent; }

这篇关于Nginx geoip 重定向,但排除 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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