Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bar2's avatar
Level 8

NGINX setup for multiple projects

server 
{
    listen   443 ssl;

    root /var/www/domain.net/laravel-api/public;
    index index.php index.html index.htm;

    client_max_body_size 80m;

    ssl_certificate /root/apis.domain.net.crt;
    ssl_certificate_key /root/apis.domain.net.key;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    server_name apis.domain.net;
}

Above server configuration is working fine as it is. But I want to map more than 1 project to apis.domain.net such as

apis.domain.net/laravel-api-1
apis.domain.net/laravel-api-2

When I try to change the server_name to apis.domain.net/laravel-api-1 for this given project (/var/www/domain.net/laravel-api/public) laravel starts to return 404 for every page. How can I achieve this?

0 likes
14 replies
byjml's avatar

It's returning 404 because it's looking for the route laravel-api-1 which does not exit.

bar2's avatar
Level 8

@byjml since I am setting it up as the root directory for the project (/var/www/domain.net/laravel-api/public in this case) shouldn't it be OK with it?

bar2's avatar
Level 8

@bashy do you have a suggestion for a working subdirectory nginx setup? I have tried most of the things you posted.

colourmill's avatar

I'm a bit confused about what you're trying to achieve here. You say you want to "map more than one project to a single domain". But then your setup seems to suggest you're using a single laravel install for multiple projects. Is that your intention? Also, you cannot use apis.domain.net/laravel-api-2 as a server_name, it would be pointless too, since you seem to be pointing it to the same root anyway.

There's a few options here but it's not exactly clear where you want to take it.

For a typical multi-project setup, you'd have multiple vhosts:

/etc/nginx-enabled/laracasts.app (server_name laracasts.app)
/etc/nginx-enabled/envoyer.app (server_name envoyer.app)

And set their roots accordingly:

root /var/www/laracasts.app/public;
root /var/www/envoyer.app/public;

But if you'd rather use a single domain and approach your different api's through different URL's, then you'd only need a single vhost and the appropriate laravel routes and there is no need for a special nginx approach.

1 like
bashy's avatar
bashy
Best Answer
Level 65

Tried this?

In your main site's vhost, add these

location ^~ /laravel-api-1 {
    alias /var/www/domain.net/laravel-api-1/public;
    try_files $uri $uri/ @laravel1;

    location ~ \.php {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
    }
}

location @laravel1 {
    rewrite /laravel-api-1/(.*)$ /laravel-api-1/index.php?/$1 last;
}


location ^~ /laravel-api-2 {
    alias /var/www/domain.net/laravel-api-2/public;
    try_files $uri $uri/ @laravel2;

    location ~ \.php {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include /etc/nginx/fastcgi_params;
    }
}

location @laravel2 {
    rewrite /laravel-api-2/(.*)$ /laravel-api-2/index.php?/$1 last;
}
4 likes
bar2's avatar
Level 8

@bashy thanks a lot for your help. After fixing the fastcgi_pass to correct php7.0-fpm.sock path on your suggestion, it all started to work :)

bashy's avatar

@bar2 Yup, php7 doesn't use php5-fpm.sock, is that what you were using?

Dedy's avatar

here's my config file:

server { listen 8080; server_name localhost;

error_page   500 502 503 504  /50x.html;
    location = /50x.html {
            root   html;
    }

location / {
    root   /path/to/www;
    index  index.php index.html index.htm;

    location ~ \.php$ {
                    root           /path/to/www;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
                    include        fastcgi_params;
            }
}

    location /site1 {       
    location = /site1/ {
                    rewrite ^(.*)$ /site1/index.php last;
            }

            alias  /path/to/www/site1/public;
            index  index.php;

    if (-f $request_filename) {
        break;
    }

    rewrite ^(.*)$ /site1/index.php last;

    location ~ /site1/.+\.php$ {
        root           /path/to/www/site1/public;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   /path/to/www/site1/public/index.php;
        include        fastcgi_params;
    }
    }

    location /site2 {               
            location = /site2/ {
                    rewrite ^(.*)$ /site2/index.php last;
            }

            alias  /path/to/www/site2/public;
            index  index.php;

            if (-f $request_filename) {
                    break;
            }

            rewrite ^(.*)$ /site2/index.php last;

            location ~ /site2/.+\.php$ {
                    root           /path/to/www/site2/public;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME   /path/to/www/site2/public/index.php;
                    include        fastcgi_params;
            }
    }

}

1 like
hkadgar's avatar

@bashy config as yours, the error display as following:

NotFoundHttpException in RouteCollection.php line 160:

hkadgar's avatar

@bashy the debug log of the nginx show that, i think "/conftools/publish" can not found the routing:


2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "QUERY_STRING: "
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "REQUEST_METHOD"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script var: "GET"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "REQUEST_METHOD: GET"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "CONTENT_TYPE"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "CONTENT_TYPE: "
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "CONTENT_LENGTH"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "CONTENT_LENGTH: "
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "SCRIPT_FILENAME"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script var: "/home/vagrant/Code/public/index.php"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "SCRIPT_FILENAME: /home/vagrant/Code/public/index.php"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "SCRIPT_NAME"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script var: "/index.php"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "SCRIPT_NAME: /index.php"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "REQUEST_URI"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script var: "/conftools/publish"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "REQUEST_URI: /conftools/publish"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "DOCUMENT_URI"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script var: "/index.php"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "DOCUMENT_URI: /index.php"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "DOCUMENT_ROOT"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script var: "/home/vagrant/Code/public"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "DOCUMENT_ROOT: /home/vagrant/Code/public"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "SERVER_PROTOCOL"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script var: "HTTP/1.1"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "SERVER_PROTOCOL: HTTP/1.1"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "GATEWAY_INTERFACE"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "CGI/1.1"
2016/12/16 05:51:11 [debug] 4978#4978: *21 fastcgi param: "GATEWAY_INTERFACE: CGI/1.1"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "SERVER_SOFTWARE"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script copy: "nginx/"
2016/12/16 05:51:11 [debug] 4978#4978: *21 http script var: "1.9.14"

bashy's avatar

@hkadgar I don't use such a setup. I tested at the time and it was fine.

bar2's avatar
Level 8

@hkadgar @bashy 's solution worked out well for me. Try the accepted answer as your boilerplate?

Please or to participate in this conversation.