我如何配置 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?
我正在开发一个 由 3 个微服务组成的应用程序。每个微服务都是一个 Symfony 4 应用程序。
为了路由我对这个应用程序的所有请求,我正在使用 Nginx。
目前有三种 url 模式,每个微服务一种。其中之一是 ^/auth
模式,它允许通过 php-fpm 上游访问身份验证 API。
我已经尝试了很多东西,但现在这是我 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;
}
}
有了这个配置,这就是追加:
- 我提出一个请求:GET /auth/publicKey
- Nginx 处理请求:GET /auth/publicKey 并通过上游转发到我的应用程序
- Symfony 应用程序处理请求:GET /auth 而不是 GET /publicKey(我的 objective)
当 Nginx 正在处理请求时:
- $realpath_root = '/app/public' :Symfony 入口点位于 php-fpm 主机
上
- $fastcgi_path_info = '/publicKey' : Symfony 应用程序中的有效路由 url
所以我的问题是:
- 为什么我的应用程序处理 GET /auth 请求,而 Nginx 之前处理的请求是 GET /auth/publicKey?
- 因此,如何解决它?
感谢您的回答:)
尝试将 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;
}
}
我正在开发一个 由 3 个微服务组成的应用程序。每个微服务都是一个 Symfony 4 应用程序。
为了路由我对这个应用程序的所有请求,我正在使用 Nginx。
目前有三种 url 模式,每个微服务一种。其中之一是 ^/auth
模式,它允许通过 php-fpm 上游访问身份验证 API。
我已经尝试了很多东西,但现在这是我 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;
}
}
有了这个配置,这就是追加:
- 我提出一个请求:GET /auth/publicKey
- Nginx 处理请求:GET /auth/publicKey 并通过上游转发到我的应用程序
- Symfony 应用程序处理请求:GET /auth 而不是 GET /publicKey(我的 objective)
当 Nginx 正在处理请求时:
- $realpath_root = '/app/public' :Symfony 入口点位于 php-fpm 主机 上
- $fastcgi_path_info = '/publicKey' : Symfony 应用程序中的有效路由 url
所以我的问题是:
- 为什么我的应用程序处理 GET /auth 请求,而 Nginx 之前处理的请求是 GET /auth/publicKey?
- 因此,如何解决它?
感谢您的回答:)
尝试将 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;
}
}