4

I'm using Nginx as a web server.

Is there any way to extract http_referer_hostname and put it in access log.

For example: if $http_referer is "http://example.com/?somedata", how to log only hostname part (example.com) to the access.log line?

codewario
  • 19,553
  • 20
  • 90
  • 159
Tereska
  • 751
  • 1
  • 7
  • 25

1 Answers1

1

Use map:

map $http_referer $http_referer_hostname {
    ~^.*://([^?/]+).*$ $1;
}

The desired result will be stored in $http_referer_hostname

EDIT: the regexp is changed, thanks to @phiphi

Nick Vee
  • 621
  • 2
  • 7
  • 17
  • 1
    This regex does not work properly. the capture group (.+) captures more than it should when there are multiple / afterwards. Better to exclude / and ? from the capture group: `~^.*://([^/?]+) $1;` – phiphi Sep 16 '22 at 09:47