如何将 REST api 请求从 nginx 反向代理到 php-fpm?
How to reverse proxy a REST api request to php-fpm from nginx?
我正在为我公司将在生产中使用的 Slim Api 设置 php-fpm。目前我在将休息端点转换成可以由 php-fpm 从 Nginx 中的代理请求执行的东西时遇到问题。
所以我在 Nginx 中有一个位置块,如下所示:
# endpoint that needs to be proxied
location /api2/ {
# I know, setting root in a location block is real bad
# But I am dealing with a legacy nginx config that has so much noise in it that I kinda have to do this for now.
root /path/to/api/root/index/folder;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8000;
}
所以我有一个 php 进程池侦听端口 8000,它能够 运行 php。我的麻烦是,在有效的 api 呼叫中,我总是收到来自 Slim 的 404。呼叫 https://example.com/api2/get/data.
我已经能够确认,在 Slim 中,它接收到的请求 URI 是 /api2/get/data,这就是端点的定义方式。
我在使用 Slim 设置这个系统时是否遗漏了什么?它们是使 Slim 在 Php-Fpm 背后工作所需的特殊配置吗?
提前谢谢大家!
我找到了问题的答案。看来您必须通过 fastcgi 参数明确告诉 php-fpm 您正在使用 https!
# endpoint that needs to be proxied
location /api2/ {
# I know, setting root in a location block is real bad
# But I am dealing with a legacy nginx config that has so much noise in it that I kinda have to do this for now.
root /path/to/api/root/index/folder;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8000;
}
这解决了我遇到的所有问题。希望这对以后的其他人有帮助。
亲切地,
弓箭手
我正在为我公司将在生产中使用的 Slim Api 设置 php-fpm。目前我在将休息端点转换成可以由 php-fpm 从 Nginx 中的代理请求执行的东西时遇到问题。
所以我在 Nginx 中有一个位置块,如下所示:
# endpoint that needs to be proxied
location /api2/ {
# I know, setting root in a location block is real bad
# But I am dealing with a legacy nginx config that has so much noise in it that I kinda have to do this for now.
root /path/to/api/root/index/folder;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8000;
}
所以我有一个 php 进程池侦听端口 8000,它能够 运行 php。我的麻烦是,在有效的 api 呼叫中,我总是收到来自 Slim 的 404。呼叫 https://example.com/api2/get/data.
我已经能够确认,在 Slim 中,它接收到的请求 URI 是 /api2/get/data,这就是端点的定义方式。
我在使用 Slim 设置这个系统时是否遗漏了什么?它们是使 Slim 在 Php-Fpm 背后工作所需的特殊配置吗?
提前谢谢大家!
我找到了问题的答案。看来您必须通过 fastcgi 参数明确告诉 php-fpm 您正在使用 https!
# endpoint that needs to be proxied
location /api2/ {
# I know, setting root in a location block is real bad
# But I am dealing with a legacy nginx config that has so much noise in it that I kinda have to do this for now.
root /path/to/api/root/index/folder;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8000;
}
这解决了我遇到的所有问题。希望这对以后的其他人有帮助。
亲切地,
弓箭手