具有双重条件的 nginx (ipgeo + useragent)

Posted

技术标签:

【中文标题】具有双重条件的 nginx (ipgeo + useragent)【英文标题】:nginx with double conditions (ipgeo + useragent) 【发布时间】:2018-10-04 09:33:49 【问题描述】:

我们正在部署一个使用不同 TLD 的国际网站。

我们使用来自 nginx 的 ipgeo 模块来检测用户位置,并在需要时进行重定向。

但我们发现机器人存在一些问题,因此我们希望在这种情况下防止重定向。

为此,我们需要在 nginx 配置中使用双重条件,但不支持。

我们可以使用不同的解决方案吗?谢谢

这是来自英国配置的代码 sn-p,我认为这是不言自明的。

if ($http_user_agent !~* "google|yahoo|bingbot|baiduspider|yandex|yeti|yodaobot|gigabot|ia_archiver|facebookexternalhit|twitterbot") # 转到全球站点 如果($geoip_city_continent_code!=“欧盟”) 重写 ^ https://xxxx.us$request_uri 永久; # 转到欧盟网站 如果 ($geoip_city_country_code != "GB") 重写 ^ https://xxxx.eu$request_uri 永久; # 留在英国网站

【问题讨论】:

【参考方案1】:

这可能适用于您的系统。请看一下

if ($http_user_agent !~* "google|yahoo|bingbot|baiduspider|yandex|yeti|yodaobot|gigabot|ia_archiver|facebookexternalhit|twitterbot") 

# Here you can setup a value which will skip redirect. 
set $geoip_city_continent_code="UK";




# goto global site
      if ($geoip_city_continent_code != "EU") 
        rewrite ^ https://xxxx.us$request_uri permanent;
      

    # goto EU site
      if ($geoip_city_country_code != "GB") 
        rewrite ^ https://xxxx.eu$request_uri permanent;

【讨论】:

【参考方案2】:

我们的解决方案来自@Kernelv5。

此外,它在不增加复杂性的情况下增加了重定向的灵活性。

谢谢

  set $continent "$geoip_city_continent_code";
  set $country "$geoip_city_country_code";

  if ($http_user_agent ~* "google|yahoo|bingbot|baiduspider|yandex|yeti|yodaobot|gigabot|ia_archiver|facebookexternalhit|twitterbot") 
    set $continent "US";
    set $country "US";
  

  if ($country = "GB") 
    rewrite ^ https://xxxx.co.uk$request_uri permanent;
  

  if ($continent = "EU") 
    rewrite ^ https://xxxx.eu$request_uri permanent;
  

【讨论】:

以上是关于具有双重条件的 nginx (ipgeo + useragent)的主要内容,如果未能解决你的问题,请参考以下文章

在 MYSQL 中应用双重条件

Jenkins/Nginx - 双重提示基本身份验证,为啥?为啥有内部 Jenkins 身份验证?

反应中的双重条件渲染?

laravel 查询:orWhere:双重条件

设置条件以避免双重释放分配的内存

php中mysql的双重输出