Php Nginx重写到根目录下的子文件夹

Php Nginx重写到根目录下的子文件夹,php,symfony,docker,nginx,docker-compose,Php,Symfony,Docker,Nginx,Docker Compose,我有几个微服务由docker管理,运行在后端专用网络中,我想建立一个由nginx管理的动态路由,以便它根据URI选择微服务。我有三个问题: 我需要nginx从URI中路径的第一段获取fastcgi_pass指令的域名,因此从语句fastcgi_pass app:9000获取app;应该来自这里:https://example.com/app/foo/bar. 如果URI是https://example.com/another-app/foo/bar 在URI中,该指令看起来像fastcgi_传递

我有几个微服务由docker管理,运行在后端专用网络中,我想建立一个由nginx管理的动态路由,以便它根据URI选择微服务。我有三个问题:

我需要nginx从URI中路径的第一段获取fastcgi_pass指令的域名,因此从语句fastcgi_pass app:9000获取app;应该来自这里:https://example.com/app/foo/bar. 如果URI是https://example.com/another-app/foo/bar 在URI中,该指令看起来像fastcgi_传递另一个应用程序:9000;这种方式是动态的,还是我必须为每个FastCGI服务器创建一个单独的位置

我还需要将它重写到根目录下的文件夹,具体取决于同一个第一个URI路径的段。我写了一个配置,但得到404个错误。我是nginx的新手,刚刚注意到nginx和php fpm容器中的路径不匹配。404错误是否与这一事实有关

这种路由可能吗?更多详细信息,请参阅下面的配置?另一个选项是为每个微服务创建一个单独的位置,但我不想每次添加或删除微服务时都更改此配置

这是我的配置:

server {
    server_name _;
    root /services;

    #rewrite to the subfolder under root depending on the first section in the path
    rewrite ^/(\w+)(/|$) /$1/app_dev.php$is_args$args last;

    location / {
        # try to serve file directly, fallback to app.php
         try_files $uri /app_dev.php$is_args$args;
    }

    location ~ ^/(\w+)/(app_dev|config)\.php(/|$) {
    #further rewrite to the /web subfolder with the front controller
        rewrite ^/(\w+)/(app_dev|config)\.php(/|$) /$1/web/$2.php$is_args$args break;

        fastcgi_pass media:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }

    location ~ \.php$ {
        return 404;
    }

    error_log  /var/log/nginx/error.log notice;
    access_log /var/log/nginx/access.log;
    rewrite_log on;
}
以下是日志:

backend_1   | 2017/09/17 20:03:54 [error] 8#8: *1 open() "/usr/share/nginx/html/media" failed (2: No such file or directory), client: 172.25.0.1, server: localhost, request: "GET /media HTTP/1.1", host: "localhost:8082"
backend_1   | 172.25.0.1 - - [17/Sep/2017:20:03:54 +0000] "GET /media HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "-"

还有一个问题是,我没有看到任何重写日志,我猜测这可能与两个位置都不匹配有关。

因此,在单个位置块本身中,这是可能的

location ~ ^/(?P<app>\w+)/(app_dev|config)\.php(/|$) {
#further rewrite to the /web subfolder with the front controller
    rewrite ^/(\w+)/(app_dev|config)\.php(/|$) /$1/web/$2.php$is_args$args break;

    fastcgi_pass $app:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;

    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;
}

我提出了以下nginx配置,允许将请求路由到不同docker容器中的应用程序。感谢@TarunLalwani的提示:

server {
    server_name _;

    location ~ ^/(?P<service>\w+)(/|$) {
        root /services/$service/web;

        rewrite ^ /app_dev.php$is_args$args break;

        resolver 127.0.0.11;
        fastcgi_pass $service:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME /app/web/app_dev.php;
        fastcgi_param DOCUMENT_ROOT /app/web;
    }

    location ~ \.php$ {
        return 404;
    }

    error_log  /var/log/nginx/error.log debug;
    access_log /var/log/nginx/access.log;
    rewrite_log on;
}

微服务的代码从应用程序的容器装载到/services目录下的nginx容器中。nginx容器中的绝对路径与应用程序容器中的绝对路径不匹配,因此使用fastcgi参数进行映射。

微服务是否也以JSON或html的形式进行响应?因为要让html正常工作,服务需要有一个基础url@TarunLalwani仅JSON
app:
    resource: '@AppBundle/Controller/'
    type: annotation
    prefix: /media