【问题标题】:How to reverse proxy a REST api request to php-fpm from nginx?如何将 REST api 请求从 nginx 反向代理到 php-fpm?
【发布时间】:2018-03-20 00:40:16
【问题描述】:

我正在为我的公司将在生产中使用的 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;
}

所以我有一个监听 8000 端口的 php 进程池,它能够运行 php。我的麻烦是我在有效的 api 调用中不断从 Slim 收到 404。电话是https://example.com/api2/get/data

我已经能够确认,在 Slim 中,它接收的请求 URI 是 /api2/get/data,这就是定义端点的方式。

在使用 Slim 设置这个系统时,我是否遗漏了什么?它们是使 Slim 在 Php-Fpm 后面工作所需的特殊配置吗?

提前谢谢大家!

【问题讨论】:

    标签: php rest nginx slim fastcgi


    【解决方案1】:

    我找到了问题的答案。看来你必须通过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;
    }
    

    这解决了我遇到的所有问题。希望这可以在将来对其他人有所帮助。

    谨此

    射手脸

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 2019-06-20
      相关资源
      最近更新 更多