Django无法从nginx

时间:2018-05-21 07:36:00

标签: django nginx uwsgi geoip

我正在尝试从访问我网站的人那里获取country_code,我决定使用nginx geoIP模块执行此操作。我已经设置了nginx配置等等。但不知怎的,它始终保持空值。我使用nginx--with-http_geoip_modulenginx -V,因此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

1 个答案:

答案 0 :(得分:0)

您的某些proxy_set_header标题名称格式错误,但这不是您的问题,因为您实际上并未进行任何代理,因此没有proxy_pass指令。

uwsgi param指令设置由uwsgi_pass传递的标头。您的Nginx指令看起来是正确的(尽管最佳做法会看到您的自定义标头前缀为X_),问题是cgi headershttp headers具有不同的协议。

因此,如果您的Nginx指令说明了这一点:

uwsgi_param HTTP_X_GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;

然后您的HTTP标头将如下所示:

X-Geoip-City-Country-Code: xyz