nginx - geoip_country_name 在 nginx 中返回 "-"

标签 nginx dockerfile nginx-location geoip

我正在尝试通过 dockerfile 为 nginx 安装 geoip 模块,方法是将以下内容添加到我的 dockerfile 中:

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

nginx.config 如下:

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;
        }
    }

}

但是,当我检查我得到的日志时

geoip_country_name = "-"

知道这里出了什么问题吗?难道我是在本地运行这个吗?

最佳答案

“-”是日志文件在值为空时使用的内容。 GeoIP 使用 $remote_addr 来计算请求的来源。

172.17.0.1 不是公共(public) IP 地址,它是您的代理服务器之一的内部地址。检查真实远程地址的 $http_x_forwarded_for header 值(假设您的反向代理服务器配置正确。

Geoip module提供 geoip_proxy 指令来忽略 $remote_addr 并使用 $http_x_forwarded_for 代替。

例如(添加到您的其他 geoip_ 指令中):

geoip_proxy 172.17.0.1;

关于nginx - geoip_country_name 在 nginx 中返回 "-",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71077437/

相关文章:

php - 具有多个位置 block 的 nginx 配置

nginx - POST 响应缓存在 nginx 中不起作用

nginx - 配置 Nginx 以忽略 url 中的路径

ssl - NGINX - SSL 握手时关闭连接,同时 SSL 握手到上游

nginx - 禁用 404 错误日志记录

docker - 无法从 root 构建 docker

linux - Docker设置ipaddress并启动服务

gcc - NginX 0.9.6 令人沮丧的编译问题 Ubuntu/GCC 4.6.1

php - Nginx - 无法打开流 : Permission denied in/var/www/example. com

docker - 如何在 docker build 中提供和使用命令行参数?