Archerface
Archerface

Reputation: 55

How to reverse proxy a REST api request to php-fpm from nginx?

I am in the process of setting php-fpm for a Slim Api that my company is going to use in production. Currently I am having trouble with translating a rest endpoint into something that can be executed by php-fpm from a proxied request in Nginx.

So I have a location block in Nginx that looks like this:

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

So I have a php process pool listening on port 8000 and it is able to run php. My trouble is that I keep getting a 404 from Slim on a valid api call. The call being https://example.com/api2/get/data.

I have been able to confirm that, in Slim, the request URI that it is receiving is the /api2/get/data and that is how the endpoint is defined.

Am I missing something in setting up this system with Slim? Is their a special configuration that is needed to make Slim work behind Php-Fpm?

Thank you all in advance!

Upvotes: 1

Views: 2231

Answers (1)

Archerface
Archerface

Reputation: 55

I found the answer to my problem. It appears that you have to explicitly tell php-fpm that you are using https through fastcgi parameters!

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

This solved all of the problems that I was having. Hope this can help someone else in the future.

Cordially,

Archerface

Upvotes: 1

Related Questions