【发布时间】:2018-05-21 07:36:00
【问题描述】:
我正在尝试从访问我网站的人那里获取 country_code,我决定使用nginx geoIP 模块来执行此操作。我已经设置了nginx 配置等。但不知何故,它总是不断给出空值。当我使用nginx -V 时,nginx 具有--with-http_geoip_module,所以它的GeoIP 函数应该可以工作。我使用uwsgi 作为我的django。
我已经安装了 geoIP 数据:
cd /etc/nginx/geoip
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gunzip GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
这是我的 nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 210;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
geoip_country /etc/nginx/geoip/GeoIP.dat; # the country IP database
geoip_city /etc/nginx/geoip/GeoLiteCity.dat; # the city IP database
proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
proxy_set_header GEOIP_COUNTRY_CODE3 $geoip_country_code3;
proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;
proxy_set_header GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
proxy_set_header GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
proxy_set_header GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
proxy_set_header GEOIP_REGION $geoip_region;
proxy_set_header GEOIP_CITY $geoip_city;
proxy_set_header GEOIP_POSTAL_CODE $geoip_postal_code;
proxy_set_header GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
proxy_set_header GEOIP_LATITUDE $geoip_latitude;
proxy_set_header GEOIP_LONGITUDE $geoip_longitude;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
这是我的 /etc/nginx/sites-available:
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name www.testname.com testname.com;
access_log off;
error_log /var/www/log_nginx/error.log;
gzip on;
gzip_disable "msie6";
client_header_timeout 180s;
client_body_timeout 180s;
client_max_body_size 100m;
proxy_connect_timeout 120s;
proxy_send_timeout 180s;
proxy_read_timeout 180s;
send_timeout 600s;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
location /static {
alias /var/www/test_project/static;
}
location /media {
alias /var/www/test_project/media_root;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HTTP_X_GEOIP_COUNTRY $geoip_country_name;
proxy_set_header HTTP_X_GEOIP_CITY $geoip_city;
include uwsgi_params;
uwsgi_read_timeout 500;
uwsgi_send_timeout 500;
uwsgi_pass unix:/var/www/testproject_global.sock;
}
}
这是我的 uwsgi_params:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
uwsgi_param HTTP_GEOIP_COUNTRY_NAME $geoip_country_name;
uwsgi_param HTTP_X_GEOIP_COUNTRY $geoip_country_code;
uwsgi_param HTTP_GEOIP_COUNTRY_CODE3 $geoip_country_code3;
uwsgi_param HTTP_GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
uwsgi_param HTTP_GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
uwsgi_param HTTP_GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
uwsgi_param HTTP_GEOIP_REGION $geoip_region;
uwsgi_param HTTP_GEOIP_CITY $geoip_city;
uwsgi_param HTTP_GEOIP_POSTAL_CODE $geoip_postal_code;
uwsgi_param HTTP_GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
uwsgi_param HTTP_GEOIP_LATITUDE $geoip_latitude;
uwsgi_param HTTP_GEOIP_LONGITUDE $geoip_longitude;
我已经在 /etc/nginx/geoip/ 中设置了 GeoIP.dat 和 GeoLiteCity.dat
但是当我试图在我的 Django 中获取数据时:
keys_with_values = ''.join("%s=%r <br />" % (key, val) for (key, val) in request.META.iteritems())
它总是返回空值,如下所示:
HTTP_X_GEOIP_COUNTRY=''
HTTP_GEOIP_COUNTRY_NAME=''
# etc etc
【问题讨论】:
-
任何帮助都会很棒:(