0

I'm trying to run two separated sites on the same server using nginx. Depending of the configuration, nginx serves one site or another under both domain names. It won't run each site on it's own domain.

Any ideas? Thanks!

Update: I've tried Michael Hampton's suggestion, but the server doesn't start when there are two server_name directives. If I comment one of then, nginx starts, but running just one website.

Also service nginx configtest just works with only one server_name, with two server_name it fails.

The configuration files follow:

/etc/nginx/sites-available/joomla

server {
    listen 80;
    server_name n-pix.com;

    root /var/www/n-pix;
    index index.php index.html index.htm default.html default.htm;

    error_log    /var/log/nginx/joomla.error.log info;

    # Support Clean (aka Search Engine Friendly) URLs
    location / {
            try_files $uri $uri/ /index.php?$args;
    }

    client_max_body_size 1024M;

    server_tokens off;

    # deny running scripts inside writable directories
    location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
            return 403;
            error_page 403 /403_error.html;
    }

    location ~ \.php$ {
        fastcgi_pass  unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # caching of files 
    location ~* \.(ico|pdf|flv)$ {
            expires 1y;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
            expires 14d;
    }

}

/etc/nginx/sites-available/jarbas

upstream unicorn {
  server unix:/tmp/unicorn.jarbas.sock fail_timeout=0;
}

server {
  listen 80;
  server_name jarbas.n-pix.com;

  root /home/deployer/apps/jarbas/current/public;

  error_log    /var/log/nginx/jarbas.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 1G;
  keepalive_timeout 10;
}
1
  • After updating the configuration files, I ran nginx -t: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32 nginx: configuration file /etc/nginx/nginx.conf test failed – Gui vieira Jan 4 '14 at 3:55
1

You have confused your listen and server_name directives.

listen should contain the port (and optionally IP/IPv6 address) that you want the server to listen on.

server_name should contain the hostname of the server.

For instance (this config requires nginx 1.3.4 or higher):

listen 80;
listen [::]:80;
server_name n-pix.com;

and

listen 80;
listen [::]:80;
server_name jarbas.n-pix.com;
7
  • I've tried your suggestion, but the server doesn't start when there are two server_name on the config files. If I comment one of then, nginx starts, but running just one website. – Gui vieira Jan 4 '14 at 3:16
  • You have to put them in the correct server blocks! – Michael Hampton Jan 4 '14 at 3:16
  • Also service nginx configtest just works with only one server_name , with two server_name it fails. – Gui vieira Jan 4 '14 at 3:16
  • I know. I updated my config files on the question. Take a look there. – Gui vieira Jan 4 '14 at 3:18
  • You forgot to specify what the problems were with your configuration. – Michael Hampton Jan 4 '14 at 3:19
1

After updating the configuration files, I ran nginx -t:

nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
nginx: configuration file /etc/nginx/nginx.conf test failed

so I added server_names_hash_bucket_size 64; to nginx.conf:

http {
    (...)

    server_names_hash_bucket_size 64;

    (...)
}

And everything works well now. Thanks guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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