How to get user public ip and log country code with geoip2 data

How to get user public ip and log country code with geoip2 data with django and caddy

1. Caddy version (caddy version):

caddy:2.4.6

2. How I run Caddy:

a. System environment:

Linux, x86_64, Docker, docker-compose

b. Command:

docker-compose up

c. Service/unit/compose file:

version: "3.9"
services:
  caddy:
    restart: always
    build: 
      context: ./  
      dockerfile: Dockerfile.caddy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./volumes/settings/caddy/Caddyfile:/etc/caddy/Caddyfile
      - ./volumes/static:/var/www/staticfiles
      - ./volumes/media/public:/var/www/media/public
      - ./volumes/logs/caddy:/var/log/caddy
      - ./volumes/settings/caddy/data:/root/
      - ./volumes/geoip2/:/usr/share/GeoIP/
    depends_on:
      - webapp

  webapp:
    restart: always
    image: webapp
    container_name: webapp-container
    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000 --reload
    build:
      context: ./
      dockerfile: Dockerfile.django
    volumes:
      - ./volumes/ComponentHub-django:/root/
      - ./volumes/logs/django:/var/log/django
      - ./volumes/static:/var/www/staticfiles
      - ./volumes/media:/var/www/media
      - ./volumes/geoip2/:/usr/share/GeoIP/
    expose:
      - 8000
    depends_on:
      - db

  db:
    restart: always
    image: mysql:8.0.28
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    container_name: mysql
    expose:
      - 3306
    volumes:
      - ./volumes/mysql/db:/var/lib/mysql
      - ./volumes/mysql/sql:/docker-entrypoint-initdb.d
    environment:
      ...
    healthcheck:
      test: "exit 0"


d. My complete Caddyfile or JSON config:

http://localhost:80 {

	handle_path /static/* {
		root * /var/www/staticfiles
		file_server
	}
	# handle_path /media/public/* {
	# 	root * /var/www/media/public
	# 	file_server
	# }

	handle {
		reverse_proxy webapp:8000 {
			header_up Country-Name {geoip_country_name}
		}
	}


	log {
		output file /var/log/caddy/error.log
		format json {
			time_format common_log
		}
		level ERROR
	}
	log {
		output file /var/log/caddy/access.log
		# format transform "{common_log}"
		format transform `{geoip_country_code} {level} {request>remote_addr} - {request>user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size} "{request>headers>Referer>[0]}" "{request>headers>User-Agent>[0]}"` {
        time_format "02/Jan/2006:15:04:05 -0700"
		}
		level INFO
	}
}

domain {
	# # HTTPS options:
	# header Strict-Transport-Security max-age=31536000;

	# # Removing some headers for improved security:
	# header -Server

	handle_path /static/* {
		root * /var/www/staticfiles
		file_server
	}
	# handle_path /media/public/* {
	# 	root * /var/www/media/public
	# 	file_server
	# }

	handle {
		reverse_proxy webapp:8000
	}


	log {
		output file /var/log/caddy/access.log
		# format transform "{common_log}"
		format transform `{geoip_country_code} {level} {request>remote_addr} - {request>user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size} "{request>headers>Referer>[0]}" "{request>headers>User-Agent>[0]}"` {
        time_format "02/Jan/2006:15:04:05 -0700"
		}
		level INFO
	}
}

www.admin.domain {
	redir https://domain/admin{uri}
}

I don’t understand. What’s the question exactly?

I want to log each request country code and city from their public ip address
like this plugin: GitHub - aablinov/caddy-geoip: Caddy plugin to resolve user GeoIP data)

Well, first of all, aablinov/caddy-geoip is a plugin for Caddy v1, not Caddy v2. So you can’t use that at all. There are 3 other geo plugins for Caddy v2 you can find on Download Caddy which you could use though.

Unfortunately, you can’t use request placeholders in the transform encoder, you can only use fields that already exist in the log.

For now, there’s currently no way for a Caddy plugin to add new fields to access logs; it might be possible in the future to make that possible, but we’d need to see demand from plugin authors to do it.

1 Like

This topic was automatically closed after 29 days. New replies are no longer allowed.