title: Nginx geoip does not show the country in the logs
body: <p>I am using a public repo for nginx configured for geolocation with geoip, but I am getting dashes in the fields <code>$geoip2_data_country_code $geoip2_data_country_name'</code>. I have the following <code>Dockerfile</code>:</p>
  <pre><code>FROM alpine:3.11
  
  COPY GeoLite2-Country.mmdb /usr/share/geoip/
  
  # Install libmaxminddb and ngx_http_geoip2_module
  
  ENV MAXMIND_VERSION=1.2.1
  
  RUN set -x \
    &amp;&amp; apk add --no-cache --virtual .build-deps \
      alpine-sdk \
      perl \
    &amp;&amp; git clone https://github.com/leev/ngx_http_geoip2_module /ngx_http_geoip2_module \
    &amp;&amp; wget https://github.com/maxmind/libmaxminddb/releases/download/${MAXMIND_VERSION}/libmaxminddb-${MAXMIND_VERSION}.tar.gz \
    &amp;&amp; tar xf libmaxminddb-${MAXMIND_VERSION}.tar.gz \
    &amp;&amp; cd libmaxminddb-${MAXMIND_VERSION} \
    &amp;&amp; ./configure \
    &amp;&amp; make \
    &amp;&amp; make check \
    &amp;&amp; make install \
    &amp;&amp; apk del .build-deps
  
  # TODO fix issue with non zero return code
  
  RUN ldconfig || :
  
  # Install nginx
  
  ENV NGINX_VERSION 1.15.11
  
  RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \
      &amp;&amp; CONFIG=&quot;\
          --prefix=/etc/nginx \
          --sbin-path=/usr/sbin/nginx \
          --modules-path=/usr/lib/nginx/modules \
          --conf-path=/etc/nginx/nginx.conf \
          --error-log-path=/var/log/nginx/error.log \
          --http-log-path=/var/log/nginx/access.log \
          --pid-path=/var/run/nginx.pid \
          --lock-path=/var/run/nginx.lock \
          --http-client-body-temp-path=/var/cache/nginx/client_temp \
          --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
          --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
          --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
          --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
          --user=nginx \
          --group=nginx \
          --with-http_ssl_module \
          --with-http_realip_module \
          --with-http_addition_module \
          --with-http_sub_module \
          --with-http_dav_module \
          --with-http_flv_module \
          --with-http_mp4_module \
          --with-http_gunzip_module \
          --with-http_gzip_static_module \
          --with-http_random_index_module \
          --with-http_secure_link_module \
          --with-http_stub_status_module \
          --with-http_auth_request_module \
          --with-http_xslt_module=dynamic \
          --with-http_image_filter_module=dynamic \
          --with-http_geoip_module=dynamic \
          --with-threads \
          --with-stream \
          --with-stream_ssl_module \
          --with-stream_ssl_preread_module \
          --with-stream_realip_module \
          --with-stream_geoip_module=dynamic \
          --with-http_slice_module \
          --with-mail \
          --with-mail_ssl_module \
          --with-compat \
          --with-file-aio \
          --with-http_v2_module \
      --add-dynamic-module=/ngx_http_geoip2_module \
      &quot; \
      &amp;&amp; addgroup -S nginx \
      &amp;&amp; adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \
      &amp;&amp; apk add --no-cache --virtual .build-deps \
          gcc \
          libc-dev \
          make \
          openssl-dev \
          pcre-dev \
          zlib-dev \
          linux-headers \
          curl \
          gnupg1 \
          libxslt-dev \
          gd-dev \
          geoip-dev \
      &amp;&amp; curl -fSL https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz \
      &amp;&amp; curl -fSL https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz.asc  -o nginx.tar.gz.asc \
      &amp;&amp; export GNUPGHOME=&quot;$(mktemp -d)&quot; \
      &amp;&amp; found=''; \
      for server in \
          ha.pool.sks-keyservers.net \
          hkp://keyserver.ubuntu.com:80 \
          hkp://p80.pool.sks-keyservers.net:80 \
          pgp.mit.edu \
      ; do \
          echo &quot;Fetching GPG key $GPG_KEYS from $server&quot;; \
          gpg --keyserver &quot;$server&quot; --keyserver-options timeout=10 --recv-keys &quot;$GPG_KEYS&quot; &amp;&amp; found=yes &amp;&amp; break; \
      done; \
      test -z &quot;$found&quot; &amp;&amp; echo &gt;&amp;2 &quot;error: failed to fetch GPG key $GPG_KEYS&quot; &amp;&amp; exit 1; \
      gpg --batch --verify nginx.tar.gz.asc nginx.tar.gz \
      &amp;&amp; rm -rf &quot;$GNUPGHOME&quot; nginx.tar.gz.asc \
      &amp;&amp; mkdir -p /usr/src \
      &amp;&amp; tar -zxC /usr/src -f nginx.tar.gz \
      &amp;&amp; rm nginx.tar.gz \
      &amp;&amp; cd /usr/src/nginx-$NGINX_VERSION \
      &amp;&amp; ./configure $CONFIG --with-debug \
      &amp;&amp; make -j$(getconf _NPROCESSORS_ONLN) \
      &amp;&amp; mv objs/nginx objs/nginx-debug \
      &amp;&amp; mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so \
      &amp;&amp; mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so \
      &amp;&amp; mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so \
      &amp;&amp; mv objs/ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so \
      &amp;&amp; ./configure $CONFIG \
      &amp;&amp; make -j$(getconf _NPROCESSORS_ONLN) \
      &amp;&amp; make install \
      &amp;&amp; rm -rf /etc/nginx/html/ \
      &amp;&amp; mkdir /etc/nginx/conf.d/ \
      &amp;&amp; mkdir -p /usr/share/nginx/html/ \
      &amp;&amp; install -m644 html/index.html /usr/share/nginx/html/ \
      &amp;&amp; install -m644 html/50x.html /usr/share/nginx/html/ \
      &amp;&amp; install -m755 objs/nginx-debug /usr/sbin/nginx-debug \
      &amp;&amp; install -m755 objs/ngx_http_xslt_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so \
      &amp;&amp; install -m755 objs/ngx_http_image_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so \
      &amp;&amp; install -m755 objs/ngx_http_geoip_module-debug.so /usr/lib/nginx/modules/ngx_http_geoip_module-debug.so \
      &amp;&amp; install -m755 objs/ngx_stream_geoip_module-debug.so /usr/lib/nginx/modules/ngx_stream_geoip_module-debug.so \
      &amp;&amp; ln -s ../../usr/lib/nginx/modules /etc/nginx/modules \
      &amp;&amp; strip /usr/sbin/nginx* \
      &amp;&amp; strip /usr/lib/nginx/modules/*.so \
      &amp;&amp; rm -rf /usr/src/nginx-$NGINX_VERSION \
      \
      # Bring in gettext so we can get `envsubst`, then throw
      # the rest away. To do this, we need to install `gettext`
      # then move `envsubst` out of the way so `gettext` can
      # be deleted completely, then move `envsubst` back.
      &amp;&amp; apk add --no-cache --virtual .gettext gettext \
      &amp;&amp; mv /usr/bin/envsubst /tmp/ \
      \
      &amp;&amp; runDeps=&quot;$( \
          scanelf --needed --nobanner --format '%n#p' /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \
              | tr ',' '\n' \
              | sort -u \
              | awk 'system(&quot;[ -e /usr/local/lib/&quot; $1 &quot; ]&quot;) == 0 { next } { print &quot;so:&quot; $1 }' \
      )&quot; \
      &amp;&amp; apk add --no-cache --virtual .nginx-rundeps $runDeps \
      &amp;&amp; apk del .build-deps \
      &amp;&amp; apk del .gettext \
      &amp;&amp; mv /tmp/envsubst /usr/local/bin/ \
      \
      # Bring in tzdata so users could set the timezones through the environment
      # variables
      &amp;&amp; apk add --no-cache tzdata \
      \
      # forward request and error logs to docker log collector
      &amp;&amp; ln -sf /dev/stdout /var/log/nginx/access.log \
      &amp;&amp; ln -sf /dev/stderr /var/log/nginx/error.log
  
  COPY nginx.conf /etc/nginx/nginx.conf
  COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf
  
  EXPOSE 80
  
  STOPSIGNAL SIGTERM
  
  CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
  </code></pre>
  <p>And the two files <code>nginx.conf</code>:</p>
  <pre><code>user  nginx;
  worker_processes  1;
  
  error_log  /var/log/nginx/error.log warn;
  pid        /var/run/nginx.pid;
  
  load_module modules/ngx_http_geoip2_module.so; # GeoIP2
  
  events {
      worker_connections  1024;
  }
  
  
  http {
      include       /etc/nginx/mime.types;
      default_type  application/octet-stream;
  
      # GeoIP2
      geoip2 /usr/share/geoip/GeoLite2-Country.mmdb {
          $geoip2_data_country_code source=$remote_addr country iso_code;
          $geoip2_data_country_name source=$remote_addr country names en;
      }
  
      log_format  main  '$remote_addr - $remote_user [$time_local] &quot;$request&quot; '
                        '$status $body_bytes_sent &quot;$http_referer&quot; '
                        '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;';
  
      # GeoIP2
      log_format  main_geo  '$remote_addr - $remote_user [$time_local] &quot;$request&quot; '
                            '$status $body_bytes_sent &quot;$http_referer&quot; '
                            '&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot; '
                            '$geoip2_data_country_code $geoip2_data_country_name';
  
      access_log  /var/log/nginx/access.log  main_geo; # GeoIP2
  
      sendfile        on;
      #tcp_nopush     on;
  
      keepalive_timeout  65;
  
      #gzip  on;
  
      include /etc/nginx/conf.d/*.conf;
  }
  </code></pre>
  <p>And <code>nginx.vh.default.conf</code>:</p>
  <pre><code>server {
      listen       80;
      server_name  localhost;
  
      location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
      }
  
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   /usr/share/nginx/html;
      }
  
  }
  </code></pre>
  <p>According to <a href="https://bubelov.com/blog/nginx-geoip/" rel="nofollow noreferrer">this page</a> if I do <code>docker run --rm -p 80:80 bubelov/nginx-alpine-geoip2</code> then I will get the correct log:</p>
  <pre><code>183.88.21.120 - - [16/Apr/2019:09:08:55 +0000] &quot;GET / HTTP/1.1&quot; 
  200 612 &quot;-&quot; 
  &quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0&quot; &quot;-&quot; 
  TH Thailand
  </code></pre>
  <p>But in my case it is the following:</p>
  <pre><code>192.168.3.1 - - [15/Jun/2022:10:46:11 +0000] &quot;GET / HTTP/1.1&quot; 304 0 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36&quot; &quot;10.0.8.83&quot; - -
  </code></pre>
  <p>The actual nginx configuration file is the following:</p>
  <pre><code>events{}
  # 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;
  
      # The identifier Backend is internal to nginx, and used to name this specific upstream
      upstream backend {
      # BACKEND_HOST is the internal DNS name used by the Backend Service inside the Kubernetes cluster 
      # or in the services list of the docker-compose. 
      server ${BACKEND_HOST}:${BACKEND_PORT};
      }
      server {
          listen ${NODE_PORT};
          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;
          }
      }
  }
  </code></pre>