避免在每个pag上运行GeoIP

2024-09-30 01:22:41 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我正在使用的模块:http://wiki.nginx.org/HttpGeoipModule

据我所见,由于它是在nginx-config和uwsgi上配置的,所以看起来别无选择,只能在每个页面上运行geoip,然后只在需要时收集和使用变量。在

从性能的角度来看,我更希望它能让我只在需要时请求geoip,将其缓存在cookie或会话中,然后不再请求它来加快站点的速度。在

有人能告诉我这是否可能吗?在


Tags: 模块orgconfighttp站点cookiewikinginx
2条回答

不,不能让nginx只按需执行GeoIP查找。由于您定义了一个geoip_countrygeoip_city指令,nginx将从geoip数据库请求数据,无论答案是否在以后使用。但是你可以在没有nginx的情况下获取GeoIP数据,也就是说,直接使用你的应用程序。看看python geoip库:http://dev.maxmind.com/geoip/downloadable#Python-5

From a performance point of view I would rather have it so I request the geoip ONLY when needed, cache it in a cookie or session and then not request it again to speed up the site. Is anyone able to tell me if this is possible?`

是的,有可能。但从性能的角度来看,您不必担心,因为geoip数据库存储在内存中(在读取配置阶段),nginx的查找速度非常快。在

不管怎样,如果你愿意,你可以用类似的方法:

set $country $cookie_country;

if ($country == '') {
    set $country $geoip_country_code;
    add_header Set-Cookie country=$geoip_country_code; 
}

uwsgi_param GEOIP_COUNTRY $country;

相关问题 更多 >

    热门问题