Can't run two separated sites under the same NGINX server

Guilherme Albuk asked:

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:


server {
    listen 80;

    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;



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

server {
  listen 80;

  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;

My answer:

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;


listen 80;
listen [::]:80;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.