我有一个 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/