如何在nginx中设置geoIp的路径

时间:2017-04-27 03:35:17

标签: php nginx geolocation geoip maxmind

我正在尝试在centos6上安装 Nginx GeoIP模块 我正在使用本指南进行安装: How to install Nginx GeoIP module
但是当我尝试在nginx.cof中设置GeoIp的路径时,我遇到了这个问题:

  

nginx:[emerg]未知指令“geoip_country”in   /etc/nginx/nginx.conf:7 nginx:
配置文件   /etc/nginx/nginx.conf测试失败

这些是我添加到nginx.conf的行:

geoip_country /usr/local/share/GeoIP/GeoIP.dat;
geoip_city /usr/local/share/GeoIP/GeoLiteCity.dat;

我在谷歌搜索了这个问题,答案是它应该在main{} 部分但是当我在nginx.conf中插入main{}并在那里插入行时,我看到另一个错误说main{}错了!另外我在其他地方读到main{}是同一个nginx.conf文件!
我很困惑,任何人都可以帮我吗?


[UPDATE]
这是我的nginx.conf中的http部分:

http {
    include       /etc/nginx/mime.types;

    # For user configurations not maintained by DirectAdmin. Empty by default.
    include /etc/nginx/nginx-includes.conf;

    # Supplemental configuration
    include /etc/nginx/nginx-modsecurity-enable.conf;
    include /etc/nginx/nginx-defaults.conf;
    include /etc/nginx/nginx-gzip.conf;
    include /etc/nginx/directadmin-ips.conf;
    include /etc/nginx/directadmin-settings.conf;
    include /etc/nginx/nginx-vhosts.conf;
    include /etc/nginx/directadmin-vhosts.conf;

    geoip_country /usr/local/share/GeoIP/GeoIP.dat;
    geoip_city /usr/local/share/GeoIP/GeoLiteCity.dat;

# Set php-fpm geoip variables
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;
}

它有什么问题吗?

1 个答案:

答案 0 :(得分:0)

geoip_指令需要放在http { ... }块中,该块位于nginx.conf文件中。例如:

http {
    geoip_country /usr/local/share/GeoIP/GeoIP.dat;
    geoip_city /usr/local/share/GeoIP/GeoLiteCity.dat;

    server {
        ...
    }
}

有关详情,请参阅this document