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;
    }
}

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.