相关疑难解决方法(0)

使用 nginx 在同一域上提供 React 前端和 php 后端

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

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

前端服务正常,但 api 服务不正确。/api当我在浏览器中点击时,我从 nginx 收到 404 错误。

我觉得我已经很接近了,但由于某种原因 nginx 没有正确的$document_root. 我添加 header( X-script) 来测试变量是什么,我得到以下结果:

X-script: /usr/share/nginx/html/index.php
Run Code Online (Sandbox Code Playgroud)

这是我的 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 ~* …
Run Code Online (Sandbox Code Playgroud)

nginx symfony nginx-location

5
推荐指数
1
解决办法
4148
查看次数

标签 统计

nginx ×1

nginx-location ×1

symfony ×1