symfony - 使用 nginx 在同一域上提供 react 前端和 php 后端

标签 symfony nginx nginx-location

我有一个 React 前端和一个 Symfony 后端,我正试图在同一个域上提供服务。如果 Assets 存在,React 前端需要提供 Assets ,否则回退以提供服务 index.html .

我想在 /api 时提供 php Symfony 应用程序在请求 uri 中。与 React 应用程序类似,我需要所有请求都转到 index.php文件。

前端正在正确提供,但没有提供 api。当我点击 /api 时,我从 nginx 得到 404在浏览器中。

我觉得我很接近但由于某种原因 nginx 没有正确的 $document_root .我正在添加一个标题( X-script )来测试变量是什么,我得到以下信息:

X-script: /usr/share/nginx/html/index.php

这是我的 nginx 配置。
server {
    listen 80 default_server;
    index index.html index.htm;

    access_log /var/log/nginx/my-site.com.log;
    error_log /var/log/nginx/my-site.com-error.log error;

    charset utf-8;

    location /api {
        root /var/www/my-site.com/backend;
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location / {
        root /var/www/my-site.com/frontend;
        try_files $uri /index.html;
    }
    location ~* \.php$ {
        add_header X-script "$document_root$fastcgi_script_name" always;
        try_files $fastcgi_script_name =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }
}

任何帮助将非常感激。

最佳答案

Symfony 4 项目的 Web 根目录必须包含 public子文件夹。我没有使用 NGINX,但我认为这是正确的配置:

location /api {
    root /var/www/my-site.com/backend/public;

关于symfony - 使用 nginx 在同一域上提供 react 前端和 php 后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52471249/

相关文章:

symfony - 无法更新实体 Symfony2

ruby-on-rails - 将 rails 3.2.12 应用程序(带引擎)部署到 nginx/passenger 上的 SUB URI 时出现 404 Not Found 错误

redirect - nginx redirect :/*/to/*. html (add .html to url)

nginx 位置匹配扩展中的正则表达式不带 "resolve"

nginx 位置 404 未找到

php - 我可以覆盖消费者类中的 PHP 特征属性以使用 Doctrine2 注释吗?

symfony - 如何在 TinyMCE 中编写 Twig 标签

php - 使用串联调用 php 函数 - $newArticle->setInternationalText.$i();

node.js - Nginx 502 错误网关 Node JS

php - Yii 框架不能在 CMap.php 中使用标量值作为数组