# How to log virtual server name

**URL:** <https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283>\
**Category:** NGINX\
**Created:** [February 27, 2012, 7:18am UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283 "2012-02-27T07:18:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 27, 2012, 7:18am UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283/1 "2012-02-27T07:18:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lucian\_DSKafka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Lucian\_DSKafka](https://www.ruby-forum.com/u/Lucian_DSKafka)\
**Post date:** [February 27, 2012, 7:23am UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283/2 "2012-02-27T07:23:05Z")

</div>

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:

> **[Server names](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.

---

<div class="post-metadata">

**Author:** ![Lucian\_DSKafka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Lucian\_DSKafka](https://www.ruby-forum.com/u/Lucian_DSKafka)\
**Post date:** [February 27, 2012, 11:50pm UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283/3 "2012-02-27T23:50:18Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![Lucian\_DSKafka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Lucian\_DSKafka](https://www.ruby-forum.com/u/Lucian_DSKafka)\
**Post date:** [February 28, 2012, 12:36am UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283/4 "2012-02-28T00:36:38Z")

</div>

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/](http://localhost:8000/)  
curl -i -H ‘Host: one’ [http://localhost:8000/](http://localhost:8000/)  
curl -i -H ‘Host: two:66’ [http://localhost:8000/](http://localhost:8000/)  
curl -i -H ‘Host: three’ [http://localhost:8000/](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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Lucian\_DSKafka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Lucian\_DSKafka](https://www.ruby-forum.com/u/Lucian_DSKafka)\
**Post date:** [February 28, 2012, 3:28am UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283/5 "2012-02-28T03:28:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lucian\_DSKafka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Lucian\_DSKafka](https://www.ruby-forum.com/u/Lucian_DSKafka)\
**Post date:** [February 27, 2012, 7:26am UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283/6 "2012-02-27T07:26:10Z")

</div>

Hi,

> 1. 
> 
> ```
> Cannot set more than 1 server name – Nginx is ignoring multiple
> 
> ```
> 
> server names defined on the same IP with a warning message

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name)**
>
> This directive appeared in version 1.11.8.

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.

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#variables)**
>
> This directive appeared in version 1.11.8.

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

Best regards,  
Piotr S. \< [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) \>

---

<div class="post-metadata">

**Author:** ![Lucian\_DSKafka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Lucian\_DSKafka](https://www.ruby-forum.com/u/Lucian_DSKafka)\
**Post date:** [February 28, 2012, 7:52pm UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283/7 "2012-02-28T19:52:29Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Lucian\_DSKafka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Lucian\_DSKafka](https://www.ruby-forum.com/u/Lucian_DSKafka)\
**Post date:** [August 7, 2013, 9:09pm UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283/8 "2013-08-07T21:09:11Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Lucian\_DSKafka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8e8cbc/32.png) [@Lucian\_DSKafka](https://www.ruby-forum.com/u/Lucian_DSKafka)\
**Post date:** [August 7, 2013, 9:53pm UTC](https://www.ruby-forum.com/t/how-to-log-virtual-server-name/217283/9 "2013-08-07T21:53:10Z")

</div>

Hello,

On Wed, Aug 7, 2013 at 3:09 PM, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://www.ruby-forum.com/).
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

* * *

_B. R._
