Apache: Use internal DNS name for logging but still have CanonicalName Off
Running a couple of identical Apache servers behind a load balancer. All external requests come in with the Host
header of, say, "www.domain.com".
I'd like to be able to put the actual host name of each server in the Apache log but %v
resolves to "www.domain.com". This is because UseCanonicalName
is set to Off
by default, resulting in the request header content being used. I can use the directive UseCanonicalName On
to have Apache use the ServerName
or UseCanonicalName DNS
to use the internal DNS name. In both cases, %v
will be 'server1', for example, which would be great, since that is what I want to log.
Now, the problem is that tinkering with UseCanonicalName
will also affect redirects. A request to "www.domain.com/dir" will result in a response of "server1/dir/", which is obviously not desired.
Question: How can I use the internal DNS name of each individual server for logging but not have side effects such as redirects being ruined?
Solution 1:
So here's my test with Apache 2.4.51:
$ cat /etc/apache2/httpd.conf
ServerRoot "/usr/lib64/apache2"
User apache
Group apache
PidFile /var/run/apache2.pid
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule actions_module modules/mod_actions.so
LoadModule mime_module modules/mod_mime.so
LoadModule env_module modules/mod_env.so
ServerLimit 16
StartServers 2
MaxRequestWorkers 400
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 10000
KeepAlive On
Timeout 300
MaxKeepAliveRequests 100
KeepAliveTimeout 15
UseCanonicalName Off
AccessFileName .htaccess
ServerTokens Prod
ServerSignature Off
TraceEnable Off
EnableSendfile Off
HostnameLookups Off
LogLevel warn
ErrorLog /var/log/apache2/error_log
LogFormat "%v %V %h %l %u %t \"%r\" %>s %b" special2
CustomLog /var/log/apache2/access2_log special2
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
<FilesMatch "^\.ht">
Order deny,allow
Deny from all
</FilesMatch>
Listen 0.0.0.0:80
ServerName localhost
<VirtualHost 0.0.0.0:80>
ServerName dummy
ServerAlias foo.example.com
ServerAlias bar.example.com
DocumentRoot /var/www/install/htdocs
ErrorLog /var/www/install/logs/error_log
CustomLog /var/www/install/logs/access2_log special2
AddType application/x-ns-proxy-autoconfig .pac .proxy .dat
<Directory /var/www/install/htdocs>
Order allow,deny
allow from all
AllowOverride All
</Directory>
</VirtualHost>
Issuing these curl commands:
$ curl -s -D - --http1.1 -v --resolv foo.example.com:80:127.0.0.1 http://foo.example.com/wpad.dat |head -12
* Added foo.example.com:80:127.0.0.1 to DNS cache
* Hostname foo.example.com was found in DNS cache
* Trying 127.0.0.1:80...
* Connected to foo.example.com (127.0.0.1) port 80 (#0)
> GET /wpad.dat HTTP/1.1
> Host: foo.example.com
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Sun, 07 Nov 2021 00:21:16 GMT
< Server: Apache
<HTTP/1.1 200 OK
Last-Modified: Fri, 22 Jan 2021 12:07:17 GMT
Date: Sun, 07 Nov 2021 00:21:16 GMT
Server: Apache
<Last-Modified: Fri, 22 Jan 2021 12:07:17 GMT
ETag: "ed-5b97c078add69"
< Accept-Ranges: bytes
< Content-Length: 237
< Content-Type: application/x-ns-proxy-autoconfig
<
{ [237 bytes data]
* Connection #0 to host foo.example.com left intact
ETag: "ed-5b97c078add69"
Accept-Ranges: bytes
Content-Length: 237
Content-Type: application/x-ns-proxy-autoconfig
alert("!!!!!!!!! PAC script start parse !!!!!!!!");
function FindProxyForURL(url, host)
{
$ curl -s -D - --http1.1 -v --resolv bar.example.com:80:127.0.0.1 http://bar.example.com/wpad.dat | head -12
* Added bar.example.com:80:127.0.0.1 to DNS cache
* Hostname bar.example.com was found in DNS cache
* Trying 127.0.0.1:80...
* Connected to bar.example.com (127.0.0.1) port 80 (#0)
> GET /wpad.dat HTTP/1.1
> Host: bar.example.com
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Sun, 07 Nov 2021 00:21:18 GMT
< Server: Apache
< Last-Modified: Fri, 22 Jan 2021 12:07:17 GMT
< ETag: "ed-5b97c078add69"
HTTP/1.1 200 OK
<Date: Sun, 07 Nov 2021 00:21:18 GMT
Server: Apache
Accept-Ranges: bytes
Last-Modified: Fri, 22 Jan 2021 12:07:17 GMT
ETag: "ed-5b97c078add69"
Accept-Ranges: bytes
< Content-Length: 237
<Content-Length: 237
Content-Type: application/x-ns-proxy-autoconfig
<Content-Type: application/x-ns-proxy-autoconfig
{ [237 bytes data]
* Connection #0 to host bar.example.com left intact
alert("!!!!!!!!! PAC script start parse !!!!!!!!");
function FindProxyForURL(url, host)
{
would result in this log file:
$ cat /var/www/install/logs/access2_log
dummy foo.example.com 127.0.0.1 - - [07/Nov/2021:01:21:16 +0100] "GET /wpad.dat HTTP/1.1" 200 237
dummy bar.example.com 127.0.0.1 - - [07/Nov/2021:01:21:18 +0100] "GET /wpad.dat HTTP/1.1" 200 237