Search code examples
phpubuntunginxfpm

How to set up nginx upstream with php on ubuntu?


On ubuntu 18.04, I have nginx running

upstream vault {
  server 127.0.0.1:8001;
}

server {
    listen 80;
    server_name vault.shopshop.space;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log debug;

    location ~ \.php$ {
        fastcgi_pass vault;
    }
}

The idea is that when I hit vault.shopshop.space, it should hit a simple php backgroud serivce listening 8001

This php background service is from here: https://www.slimframework.com/docs/v3/tutorial/first-app.html

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");

    return $response;
});
$app->run();

I run this php -S localhost:8001, I can curl in my ubuntu server.

curl http://localhost:8001/hi/bla

will output hello, bla

run netstat -npl

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13004/nginx: master 
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      539/systemd-resolve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      643/sshd            
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      12961/php-fpm: mast 
tcp6       0      0 :::22                   :::*                    LISTEN      643/sshd            
tcp6       0      0 ::1:8001                :::*                    LISTEN      12325/php           
udp    49920      0 127.0.0.53:53           0.0.0.0:*                           539/systemd-resolve 
udp        0      0 45.76.119.188:68        0.0.0.0:*                           512/systemd-network 
raw6       0      0 :::58                   :::*                    7           512/systemd-network 

the simple php service is listening 8001 in ip4 and ip6

ufw firewall

To                         Action      From
--                         ------      ----
Nginx Full                 ALLOW       Anywhere                  
OpenSSH                    ALLOW       Anywhere                  
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                  
8001                       ALLOW       Anywhere                  
Nginx Full (v6)            ALLOW       Anywhere (v6)             
OpenSSH (v6)               ALLOW       Anywhere (v6)             
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)             
8001 (v6)                  ALLOW       Anywhere (v6) 

firewall can listen 8001

www.conf, only listen 8001

/etc/php/5.6/fpm/pool.d/www.conf

;listen = /run/php/php5.6-fpm.sock
listen = 8001

The problem:

http://vault.shopshop.space/hi/bla, 404

http://vault.shopshop.space, default nginx page

I expect hello bla


Solution

  • Here is configuration example for nginx + php-fpm in documentation.

    You haven't in your configuration:

    • root
    • fastcgi params
    • etc

    Here is example with upstream:

    upstream vault {
        server 127.0.0.1:8001;
    }
    
    server {
        listen 80;
        server_name vault.shopshop.space;
    
        error_log /var/log/nginx/vault.shopshop.space_error.log;
        access_log /var/log/nginx/vault.shopshop.space_access.log;
    
        root /path/to/public;
        index index.php;
    
        location / {
            try_files $uri /index.php$is_args$args;
         }
    
        location ~ \.php {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            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 vault;
        }
    }
    

    You should fix path to public dir in root string.