setting server name = hostname explicitly

Igor Sysoev igor at
Fri Jan 21 15:31:57 MSK 2011

On Mon, Jan 17, 2011 at 02:24:42PM -0500, spurcell wrote:

> Hi everyone,
> If Igor's reading, I just want to say that NGINX is amazing and we love
> it. Thank you!
> Apologies if this has already been answered elsewhere, but I spent a lot
> of time looking and couldn't find a solution: is there any way to
> specify the current hostname as a valid server_name explicitly without
> using a literal? I know this is what you get if you just leave
> server_name unspecified, but then I can't also have it match
> "localhost", without it being the default server.
> So I guess my question could be rephrased as: how can I set up a server
> to match requests to "localhost" and whatever the machine's hostname is
> without hard-coding the hostname in the config file or making the server
> the default. (I don't want the server to be the "default" server for
> port 80; I want another server to do that.)
> server_name $hostname; doesn't appear to work, and neither does
> server_name ~$hostname; and I'd like to avoid regex processing overhead
> anyway. I guess ideally what I'd like would be:
> server_name $hostname localhost;
> or something like that. And if neither matched, it would fall through to
> the default server.

Changes with nginx 0.9.4                                         21 Jan 2011

    *) Feature: the "server_name" directive supports the $hostname variable.

Igor Sysoev

More information about the nginx mailing list