Nginx Multiple Locations Laravel Project

- 1 answer

I am having issues trying to setup a second set of locations for my Nginx Setup. I currently have 1 working route, that uses reverse proxy to a NodeJS Express server.

I am tryin to setup a second location to serve a laravel project, here is my Nginx config file, I know there are some errors, but after googling I coulnd't find an answer on my own.


server {
        listen 443 http2 ssl;
        listen [::]:443 http2 ssl;

        ssl on;
        ssl_certificate /etc/letsencrypt/live/;
        ssl_certificate_key /etc/letsencrypt/live/;

        # This doesnt work, its a laravel project
        # Theres something wrong with my try_files
        location /project2 {
                root /home/ubuntu/project2/public/;
                try_files $uri $uri/ /index.php?$query_string;

        # This works, I am reverse proxying to NodeJS Application
        location / {
                proxy_pass http://localhost:3001;
                proxy_set_header Host $http_host;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";


You need to add something to the /project2 that tells nginx how to handle php files. a block that will know what to do with

I grabbed the following from here. I updated your location regex, though I haven't tested, so you may need to fix it (everything about nginx is trial and error until it works):

location ~* /project2/(.*\.php)$ {
    root /home/ubuntu/project2/public/;
    try_files $1 $1/ $1/index.php?$query_string;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

You are serving from /home/ubuntu/project2/public/, but your php urls end with project2, so you need to do a bit of regex magic to extract the proper url.

Of course, if you can make things simpler with your directory structure, you can use a simpler regex.