1

I have configured my Laravel API with Angular front-end in my Nginx server as the following. But the Laravel API is not working. Can you tell me what I'm doing wrong here.

This is the folder structure

www
-- my_domain
---- front-end index.html + css/js files + assets
---- api
------ index.php
------ public
-------- index.php

This is the nginx configuration I'm using

server {
    listen 80;
    server_name my_domain.com www.my_domain.com;
    root /var/www/my_domain.com;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api {
        alias /var/www/my_domain.com/api/public;
        try_files $uri $uri/ /index.php?$args;
    }

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

    location ~ /\.ht {
        deny all;
    }
}
camille
  • 278
  • 1
  • 2
  • 20

1 Answers1

6

this is what worked for me:

server {
    listen 80;
    server_name my_domain.com www.my_domain.com;
    root /var/www/my_domain.com/api/public;

    index index.html index.htm index.php;

    location /api/ {
        root /var/www/my_domain.com/api/public;
        try_files $uri  /index.php$is_args$args;
    }
    
    location / {
        root /var/www/my_domain.com;
        try_files $uri $uri/ /index.html;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
     }

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

from here: serve react frontend and php backend on same domain with nginx

Vivek Jain
  • 2,730
  • 6
  • 12
  • 27
patorusty
  • 61
  • 1
  • 2