Dav*_*vid 5 nginx symfony nginx-location
我有一个 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 ~* \.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;
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。
Symfony 4 项目的 Web 根目录必须包含public子文件夹。我没有使用 NGINX,但我认为这是正确的配置:
location /api {
root /var/www/my-site.com/backend/public;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4148 次 |
| 最近记录: |