NGINX: different sites on the same domain

I working on a several php projects and I want to deploy those projects on the same NGINX webserver (same name) with different alias. For example: http://localhost/project1, http://localhost/project2. I thought if add multiple config in sites-avaible and enabled would be enough but its not working. Another thing I want hold the configs in separated files if its possible. These configs works fine separately.

Here is my configs. php info page:

server {
        listen 80;
        root /var/www/html/;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name localhost;

        location / {
                try_files $uri $uri/ /index.php?$args;
        }
        location ~* .(ico|jpg|webp|jpeg|gif|css|png|js|ico|bmp|zip|woff)$ {
            access_log off;
            log_not_found off;
            add_header Pragma public;
            add_header Cache-Control "public";
            expires 14d;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

site1:

server {
        listen 80;
        root /var/www/html/site1/app/webroot;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name localhost/site1;

        location /crade_web {
                try_files $uri $uri/ /index.php?$args;
        }
        location ~* .(ico|jpg|webp|jpeg|gif|css|png|js|ico|bmp|zip|woff)$ {
            access_log off;
            log_not_found off;
            add_header Pragma public;
            add_header Cache-Control "public";
            expires 14d;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

Answer

server_name is the host name so it cannot be localhost/site1.

If you put all your projects in separate subfolders you don’t have to create different server. I.e. from nginx point of view you have only one site.

If you want to create different sites you may want to give different server names like, e.g.

  • myproject1.local
  • myproject2.local
  • myotherproject.local

Mind you, you’ll have to edit your hosts file to resolve all those name to 127.0.0.1.

Attribution
Source : Link , Question Author : eszik.k , Answer Author : Francesco Abeni

Leave a Comment