You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NGINX配置问题:Symphony应用/app请求无法转发至app.php

问题:Symphony应用NGINX路由配置异常

我正在为Symphony应用配置NGINX,目标是将所有指向/app的请求转发到app.php,但配置后无法实现需求:

  • 第一次配置用location /时,访问http://*.host.com/app无响应
  • 修改为location /app/后,访问返回404错误
    目前应用可通过site.host.com/login/访问,后台实际路由是site.host.com/app.php/login/,需要实现site.host.com/app/login/的路由形式。

第一次配置尝试

server {
    listen       81;
    server_name  *.host.com;
    port_in_redirect off;

    access_log  /var/log/nginx/_access.log;
    error_log   /var/log/nginx/_error.log;

    root   /var/www/html/web;
    index index.php index.html index.htm;

    location / {
        # try to serve file directly, fallback to rewrite
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        # rewrite all to app.php
        rewrite ^(.*)$ /app.php/$1 last;
    }

    # PHP-FPM configuration
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}

修改后的配置尝试

server {
    listen       81;
    server_name  *.host.com;
    port_in_redirect off;

    access_log  /var/log/nginx/_access.log;
    error_log   /var/log/nginx/_error.log;

    root   /var/www/html/web;
    index index.php index.html index.htm;

    location /app/ {
        # try to serve file directly, fallback to rewrite
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        # rewrite all to app.php
        rewrite ^(.*)$ /app.php/$1 last;
    }

    # PHP-FPM configuration
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}

解决方案

问题出在路由重写规则和location匹配逻辑上,调整后的完整配置如下:

server {
    listen       81;
    server_name  *.host.com;
    port_in_redirect off;

    access_log  /var/log/nginx/_access.log;
    error_log   /var/log/nginx/_error.log;

    root   /var/www/html/web;
    index index.php index.html index.htm;

    # 同时匹配/app和/app/的请求,处理末尾斜杠问题
    location /app {
        # 先尝试访问实际文件,不存在则重写
        try_files $uri $uri/ @rewriteapp;
    }

    location @rewriteapp {
        # 重写时去除/app前缀,将剩余路径传递给app.php
        rewrite ^/app(.*)$ /app.php$1 last;
    }

    # PHP-FPM配置保持不变,确保正确解析PATH_INFO
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info  ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}

配置说明

  • location /app:匹配所有以/app开头的请求(包括/app和/app/login/),用try_files $uri $uri/处理末尾斜杠自动跳转,避免无响应
  • rewrite ^/app(.*)$ /app.php$1 last:捕获/app后的路径部分,直接重写到app.php后,比如/app/login/会被重写为/app.php/login/,和后台实际路由一致
  • 保留原PHP-FPM配置,确保Symphony能正确获取PATH_INFO解析路由

内容的提问来源于stack exchange,提问作者Shivanesh Lal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 09:42:42