Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Based on the nginx wiki the $hostname variable is "Set to the machine's hostname as returned by gethostname ". I tried that and although gethostname doesn't work my debian box it still returns the host correctly. Then I tried to use that variable $hostname to set the server_name, but that didn't work. Why is that and is there another way I can accomplish that.

server {
    listen   80;

    autoindex off;

    server_name  static.$hostname;
    root   /var/www/static;

    access_log  /var/log/nginx/localhost.access.log;

    location / {
        index  index.html;

    error_page  404  /404.html;
    error_page  500 502 503 504  /50x.html;
share|improve this question
up vote 1 down vote accepted

Most variables in nginx only exist at runtime, not during configuration time. For this reason, most variables cannot be used with the server_name directive. Since $hostname is a constant value, there's an explicit check for exactly $hostname in the server_name handler. It only allows for the server_name to be set to $hostname, not static.$hostname. You may be able to patch the source to make it support that feature (ngx_http_core_module.c, look for $hostname), but you can't do it with the existing code.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.