geoip_country_name在nginx中返回“-”

ih99xse1  于 2022-11-28  发布在  Nginx
关注(0)|答案(2)|浏览(160)

我尝试通过在我的dockerfile中添加以下内容来安装nginx的geoip模块:

RUN apk add --no-cache libmaxminddb nginx-mod-http-geoip 

RUN cd /var/lib; \
    mkdir -p nginx; \
    wget -q -O- https://dl.miyuru.lk/geoip/maxmind/country/maxmind.dat.gz | gunzip -c > nginx/maxmind-country.dat; \
    wget -q -O- https://dl.miyuru.lk/geoip/maxmind/city/maxmind.dat.gz | gunzip -c > nginx/maxmind-city.dat; \
    chown -R nginx. nginx

COPY nginx.conf /etc/nginx/nginx.conf

配置如下:

load_module "modules/ngx_http_geoip_module.so";

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events{worker_connections  1024;
}
# See blow link for Creating NGINX Plus and NGINX Configuration Files 
# https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/
http {

    include /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format kv 'site="$server_name" server="$host" dest_port="$server_port" dest_ip="$server_addr" '
                       'src="$remote_addr" src_ip="$realip_remote_addr" user="$remote_user" '
                       'time_local="$time_local" protocol="$server_protocol" status="$status" '
                       'bytes_out="$bytes_sent" bytes_in="$upstream_bytes_received" '
                       'http_referer="$http_referer" http_user_agent="$http_user_agent" '
                       'nginx_version="$nginx_version" http_x_forwarded_for="$http_x_forwarded_for" '
                       'http_x_header="$http_x_header" uri_query="$query_string" uri_path="$uri" '
                       'http_method="$request_method" response_time="$upstream_response_time" '
                        'cookie="$http_cookie" request_time="$request_time" category="$sent_http_content_type" https="$https"'
                        'geoip_country_name="$geoip_country_name"';

    access_log  /var/log/nginx/access.log kv;

    sendfile        on;
    keepalive_timeout  65;

    geoip_country /var/lib/nginx/maxmind-country.dat;
    geoip_city /var/lib/nginx/maxmind-city.dat;
    include /etc/nginx/conf.d/*.conf;

    # The identifier Backend is internal to nginx, and used to name this specific upstream
    upstream backend {
    # dashboard is the internal DNS name used by the backend Service inside Kubernetes
    server localhost:5005;
    }

    server {
        listen 80;
        root /usr/share/nginx/html;
        index index.html;

        location / {
        try_files $uri $uri/ /index.html;
        }

        location /api/ {
        resolver 127.0.0.11; #nginx will not crash if host is not found    
        # The following statement will proxy traffic to the upstream
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

}

但是,当我检查日志时
地理IP国家/地区名称="-"
知道这里出了什么问题吗?会不会是我在本地运行这个?

o0lyfsai

o0lyfsai1#

“-”是值为空时日志文件使用的值。GeoIP使用$remote_addr来计算请求的源。
172.17.0.1不是公用IP地址,它是您的某个代理服务器的内部地址。请检查$http_x_forwarded_for标头值以获取真实的的远程地址(假设您的反向代理服务器配置正确。
Geoip module提供geoip_proxy指令以忽略$remote_addr并改用$http_x_forwarded_for
例如(添加到其他geoip_指令中):

geoip_proxy 172.17.0.1;
px9o7tmv

px9o7tmv2#

我们遇到了类似的问题。
它基本上回到了@RichardSmith提出的观点,但是在我们的例子中,以下配置解决了问题:

geoip_proxy 0.0.0.0/0;

相关问题