在子目录中使用反向代理运行 Symfony 5

Joh*_*hni 5 php symfony

我喜欢在提供以下端点的反向代理后面运行 Symfony 5 应用程序:

https://my.domain/service1/

代理配置基本上是这样的:

ProxyPass /marketsy/ http://internal.service1/
Run Code Online (Sandbox Code Playgroud)

在反向代理连接到的服务器上,我使用以下 apache 规则为我的 Symfony 应用程序提供服务:

<VirtualHost *:80>
  ServerName internal.service1
  DocumentRoot /webroot/service1/public

 <FilesMatch \.php$>
     SetHandler proxy:unix:/run/php/php7.2-fpm-ui.sock|fcgi://localhost
     SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
     SetEnv HTTP_X_FORWARDED_PROTO "https"
 </FilesMatch>

 <Directory  /webroot/service1/public>
     AllowOverride None
     Require all granted
     FallbackResource /index.php
 </Directory>

 <Directory  /webroot/service1/public/bundles>
     FallbackResource disabled
 </Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

应用程序本身是可重新访问的,但 Symfony 无法处理“service1”路径前缀。

例如,它尝试访问https://my.domain/_wdt/8e3926而不是https://my.domain/service1/_wdt/8e3926下的探查器,并且除了根路由之外,所有路由都不起作用:

例如:当我尝试访问https://my.domain/service1/my/page 时,我将被重定向到https://my.domain/my/page

现在我的问题是,当生成 urls 时,如何配置 Symfony 以了解“service1”路径前缀。

Jim*_*mix 7

正确的做法(示例):

创建 src/Controller/BarController.php

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class BarController
{
    public function index()
    {
        return new Response('<p>Bar controler response</p>');
    }
}
Run Code Online (Sandbox Code Playgroud)

src/Controller/FooController.php

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class FooController
{
    public function index()
    {
        return new Response('<p>Foo controler response</p>');
    }
}
Run Code Online (Sandbox Code Playgroud)

创建 config/routes/prefix-routes.yaml

index:
    path: /
    controller: App\Controller\DefaultController::index

bar:
    path: /bar
    controller: App\Controller\BarController::index
 
foo:
    path: /foo
    controller: App\Controller\FooController::index
 
Run Code Online (Sandbox Code Playgroud)

并编辑路由config/routes.yaml- 删除其内容并放置:

prefixed:
   resource: "routes/prefix-routes.yaml"
   prefix: service1
Run Code Online (Sandbox Code Playgroud)

所有控制器现在都可以在 urls 上找到:

http://localhost/service1/ for DefaultController.php
http://localhost/service1/bar for BarController.php
http://localhost/service1/foo for FooController.php
Run Code Online (Sandbox Code Playgroud)

如果您希望探查器也使用service1前缀,请按config/routes/dev/web_profiler.yaml以下方式编辑:

web_profiler_wdt:
    resource: '@WebProfilerBundle/Resources/config/routing/wdt.xml'
    prefix: service1/_wdt

web_profiler_profiler:
    resource: '@WebProfilerBundle/Resources/config/routing/profiler.xml'
    prefix: service1/_profiler
Run Code Online (Sandbox Code Playgroud)

现在它们应该可以在:

http://localhost/service1/_wdt... for wdt
http://localhost/service1/_profiler for profiler
Run Code Online (Sandbox Code Playgroud)

为注解添加前缀:

创建控制器src/Controller/AnnoController.php

<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class AnnoController extends AbstractController
{
    /**
     * @Route("/anno", name="anno")
     */
    public function index()
    {
        return new Response('<p>Anno controler response</p>');
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑config/routes/annotations.yaml并添加prefix: service1

controllers:
    resource: ../../src/Controller/
    type: annotation
    prefix: service1

kernel:
    resource: ../../src/Kernel.php
    type: annotation
Run Code Online (Sandbox Code Playgroud)

现在前缀被添加到通过注释完成的路由中:

http://localhost/service1/anno for AnnoController.php
Run Code Online (Sandbox Code Playgroud)

一些参考:

Symfony 路由前缀
Symfony 路由配置键

添加前缀快速和肮脏的解决方法以service1向所有路由添加前缀(不推荐)。

而不是像上面那样改变路由只是编辑 src/Kernel.php protected function configureRoutes

$routes->import通过->prefix('service1')在末尾添加来更改每一行,使其看起来像这样:

protected function configureRoutes(RoutingConfigurator $routes): void
{
    $routes->import('../config/{routes}/'.$this->environment.'/*.yaml')->prefix('service1');
    $routes->import('../config/{routes}/*.yaml')->prefix('service1');

    if (is_file(\dirname(__DIR__).'/config/routes.yaml')) {

        $routes->import('../config/{routes}.yaml')->prefix('service1');

    } elseif (is_file($path = \dirname(__DIR__).'/config/routes.php')) {
        (require $path)($routes->withPath($path), $this);
    }
}
Run Code Online (Sandbox Code Playgroud)

所有控制器现在都可以在 urls 上找到:

http://localhost/service1/ for DefaultController.php
http://localhost/service1/bar for BarController.php
http://localhost/service1/foo for FooController.php
Run Code Online (Sandbox Code Playgroud)

以及分析器:

http://localhost/service1/_wdt... for wdt
http://localhost/service1/_profiler for profiler
Run Code Online (Sandbox Code Playgroud)