如何使用 api 前缀在 NGINX 中配置 Laravel API 和 Angular 前端

cam*_*lle 1 nginx laravel angularjs angular

我在 Nginx 服务器中使用 Angular 前端配置了 Laravel API,如下所示。但 Laravel API 无法正常工作。你能告诉我我在这里做错了什么吗?

这是文件夹结构

www
-- my_domain
---- front-end index.html + css/js files + assets
---- api
------ index.php
------ public
-------- index.php
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的 nginx 配置

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

这对我有用:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

从这里开始: 使用 nginx 在同一域上提供 React 前端和 php 后端