简体   繁体   English

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

[英]How can I configure Nginx to route a specific request pattern to a specific php-fpm upstream hosting a Symfony application?

I'm developing an application composed of 3 microservices . 我正在开发一个由3个微服务组成应用程序 Each microservice is a Symfony 4 application . 每个微服务都是Symfony 4应用程序

To route all my request made to this application I'm using Nginx . 要将我对此应用程序的所有请求路由,我正在使用Nginx

There are, for now, three url patterns, one for each microservice. 目前,有三个url模式,每个微服务一个。 One of them is the ^/auth pattern which allow the access to the authentication API through the php-fpm upstream. 其中一个是^/auth模式,它允许通过php-fpm upstream访问身份验证API。

I've tried many things but for now this is the closest Nginx configuration I have from my objective. 我已经尝试了很多东西但是现在这是我从目标中得到的最接近的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;
    }
}

With this configuration, this is what append : 使用此配置,这是附加内容:

  • I make a request : GET /auth/publicKey 我提出了一个请求: GET / auth / publicKey
  • Nginx handle the request : GET /auth/publicKey and forward it to my application through the upstream Nginx处理请求: GET / auth / publicKey并通过上游将其转发到我的应用程序
  • The Symfony application handle the request : GET /auth instead of GET /publicKey (my objective) Symfony应用程序处理请求: GET / auth而不是GET / publicKey (我的目标)

While Nginx is handling the request : 虽然Nginx正在处理请求:

  • $realpath_root = '/app/public' : Where the Symfony entrypoint is on the php-fpm host $ realpath_root ='/ app / public':Symfony入口点位于php-fpm主机上
  • $fastcgi_path_info = '/publicKey' : A valid route url in the Symfony application $ fastcgi_path_info ='/ publicKey':Symfony应用程序中的有效路由URL

So my questions are : 所以我的问题是:

  • Why is my application handling a GET /auth request when the request previously handled by Nginx is GET /auth/publicKey ? 当Nginx先前处理的请求是GET / auth / publicKey时,为什么我的应用程序处理GET / auth请求?
  • And therefor, how to fix it ? 为此,如何解决它?

Thank you for your answers :) 谢谢您的回答 :)

Try to move the include fastcgi_params; 尝试移动include fastcgi_params; line upward 向上排

something like this: 这样的事情:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM