How to log virtual server name

Hi All,

We have a virtual hosting setup where multiple domains are delegated to
the same server IP address, and Nginx acts as a caching proxy server in
front of the web server.

I am encountering 2 issues:

  1.   Cannot set more than 1 server name - Nginx is ignoring multiple 
    

server names defined on the same IP with a warning message

  1.   Cannot log the virtual server name (Apache %v equivalent) to 
    

the access_log. Any variable in the custom log format - ie.
$server_name, $host, etc does not log the Host headers, but rather the
server name string set with the server_name directive (if matched). This
makes it impossible to have a combined log file for different sites set
in a virtual hosting environment on the same IP address.

Any ideas how to get this working?

Thank you,

Luci

On Mon, Feb 27, 2012 at 06:18:00AM +0000, Lucian D. Kafka wrote:

Hi All,

We have a virtual hosting setup where multiple domains are delegated to the same
server IP address, and Nginx acts as a caching proxy server in front of the web
server.

I am encountering 2 issues:

  1.   Cannot set more than 1 server name - Nginx is ignoring multiple server 
    

names defined on the same IP with a warning message

This may help:
http://nginx.org/en/docs/http/server_names.html

  1.   Cannot log the virtual server name (Apache %v equivalent) to the 
    

access_log. Any variable in the custom log format - ie. $server_name, $host, etc
does not log the Host headers, but rather the server name string set with the
server_name directive (if matched). This makes it impossible to have a combined
log file for different sites set in a virtual hosting environment on the same IP
address.

$http_host.


Igor S.

Hi Igor, thank you very much for your reply.

The problem is that Nginx does not behave as the documentation
describes. Entering multiple server names in one server_name directive
and using $http_host do not work.

When I enter more than 1 server name I get a warning message: Restarting
nginx: nginx: [warn] conflicting server name “www.xxx.com” on
x.x.x.x:80, ignored (there is only one mention of this server name
anywhere - I have grepped all config files looking for a runaway).

In terms of logging, $http_host (and all other variables I tried to get
a host name out of) is empty unless there is a server_name match on
‘certain’ one name in the server_name list…

What could cause $http_host to loose its value even if the server_name
does not populate correctly?

I am using nginx version: nginx/1.0.11. I will try to see if I can
pinpoint the issue further…

Cheers,

Luci

On Mon, Feb 27, 2012 at 10:49:38PM +0000, Lucian D. Kafka wrote:

Hi there,

The problem is that Nginx does not behave as the documentation describes.
Entering multiple server names in one server_name directive and using $http_host
do not work.

The following “http” section of nginx.conf allows me to see content from
two different directories, depending on the Host: header in the request;
and includes the hostname used in the Host: header in the access_log
file.

It behaves for me as the documentation describes.

What is the difference between this and the configuration file you
are using?

===
http {
log_format mine ‘$host $remote_addr - “$request” $status’;
access_log logs/mine.log mine;
server {
server_name one two;
listen 8000;
root one;
}
server {
server_name three;
listen 8000 default_server;
root three;
}
}

If I use $http_host instead of $host, I see whatever the client sent –
including the :port part.

Testing using commands like

curl -i http://localhost:8000/
curl -i -H ‘Host: one’ http://localhost:8000/
curl -i -H ‘Host: two:66’ http://localhost:8000/
curl -i -H ‘Host: three’ http://localhost:8000/

shows me the content and the log lines that I expect, as above.

So: I’m unable to reproduce the problem you report, using a
configuration
that seems to match your text. Can you provide a (minimal?) config file
that shows the problem for you?

Cheers,

f

Francis D. [email protected]

Thank you for that Francis.

Using your testing battery with different curl headers I was able to
narrow down the issue.

Basically nginx logs correctly any virtual host name under the sun
(specified or not under the server_name) - except one. This is exactly
the same one it complains as having a conflict on startup.

If I take out this one server name of the server_name directive (ie.
this name is not mentioned in any nginx config files), the complaining
about conflict stops, but logging for this one host does not work - ie
$host (and all other variants) are empty.

Cheers,

Luci

Hi,

  1.   Cannot set more than 1 server name – Nginx is ignoring multiple
    

server names defined on the same IP with a warning message

http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name

You want to specify multiple server names within one directive, do not
use
multiple directives.

  1.   Cannot log the virtual server name (Apache %v equivalent) to the
    

access_log. Any variable in the custom log format – ie. $server_name,
$host, etc does not log the Host headers, but rather the server name
string set with the server_name directive (if matched). This makes it
impossible to have a combined log file for different sites set in a
virtual hosting environment on the same IP address.

http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

“$host” works just fine, you must have tested it wrong.

Best regards,
Piotr S. < [email protected] >

On Tue, Feb 28, 2012 at 02:27:58AM +0000, Lucian D. Kafka wrote:

Hi there,

Basically nginx logs correctly any virtual host name under the sun (specified or
not under the server_name) - except one. This is exactly the same one it complains
as having a conflict on startup.

“conflicting server name” should only appear when nginx has found a
(case insensitive) duplicate server_name.

Can you provide a (minimal?) config file that shows the problem for you?

Cheers,

f

Francis D. [email protected]

seems for me that any of these work for showing the virtual host (for
followers).

$http_host $host $server_name

I just had to uncomment this line for it to ‘take’, oddly:

#access_log logs/access.log main;

@nginx dev guys: it would be nice if the default config either
mentioned that both log_format and access_log (commented out) lines need
to be uncommented to take, or for them both to start uncommented
out…to avoid this confusion for others in the future…

Hello,

On Wed, Aug 7, 2013 at 3:09 PM, Roger P. [email protected] wrote:

seems for me that any of these work for showing the virtual host (for
followers).

$http_host $host $server_name

​The Nginx docs specifies different content for those 3 variables.
The $http_host is a subset of the possible values that $host can take.
$server_name might not reflect what you wish: it will send literally the
line which is in the server configuration. It it contains a regular
expression, for instance, you’ll get it as you typed it in…


Posted via http://www.ruby-forum.com/.


nginx mailing list
[email protected]
nginx Info Page


B. R.