如何配置Nginx将特定请求模式路由到托管Symfony应用程序的特定php-fpm上游?

时间:2019-08-02 15:34:23

标签: php symfony nginx routing microservices

我正在开发一个由3个微服务组成的应用程序。每个微服务都是一个 Symfony 4应用程序

我要使用 Nginx 将我的所有请求路由到此应用程序。

目前有3个网址格式,每个微服务一个。其中之一是^/auth模式,该模式允许通过上游的php-fpm访问身份验证API。

我已经尝试了很多事情,但是现在这是我最接近目标的Nginx配置。

server {

    listen 80;
    server_name app.local;
    root /app;

    # location directive for the authentication API
    location ~ ^/auth/ {

        root /app/public;

        fastcgi_pass auth_api_upstream;
        fastcgi_split_path_info ^(/auth)(/.*)$;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param SCRIPT_FILENAME $realpath_root/index.php$fastcgi_path_info;

        include fastcgi_params;
    }

    # ....

    # return 404 if other location do not match
    location / {
        return 404;
    }
}

使用此配置,这就是附加内容:

  • 我提出了一个请求: GET / auth / publicKey
  • Nginx处理请求: GET / auth / publicKey 并将其通过上游转发到我的应用程序
  • Symfony应用程序处理请求: GET / auth 而不是 GET / publicKey (我的目标)

当Nginx正在处理请求时:

  • $ realpath_root ='/ app / public':Symfony入口点位于php-fpm主机上
  • $ fastcgi_path_info ='/ publicKey':Symfony应用程序中的有效路由URL

我的问题是:

  • 当以前由Nginx处理的请求是GET / auth / publicKey时,为什么我的应用程序处理GET / auth请求?
  • 因此,如何解决?

谢谢您的回答:)

1 个答案:

答案 0 :(得分:2)

尝试将include fastcgi_params;行向上移动

类似这样的东西:

server {

    listen 80;
    server_name app.local;
    root /app;

    location ~ ^/auth/ {

        root /app/public;

        set $app_entrypoint /index.php;

        fastcgi_pass auth_api_upstream;
        fastcgi_split_path_info ^(/auth)(/.*)$;
        include fastcgi_params;

        fastcgi_param DOCUMENT_ROOT $document_root;
        fastcgi_param DOCUMENT_URI $app_entrypoint;

        fastcgi_param REQUEST_URI $fastcgi_path_info;

        fastcgi_param SCRIPT_NAME $app_entrypoint;
        fastcgi_param SCRIPT_FILENAME $document_root$app_entrypoint;

    }

    # return 404 if other location do not match
    location / {
        return 404;
    }
}