如何在 ubuntu 上使用 php 设置 nginx upstream?

How to set up nginx upstream with php on ubuntu?

在 ubuntu 18.04,我有 nginx 运行ning

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

想法是,当我点击 vault.shopshop.space 时,它应该点击一个简单的 php backgroud serivce listening 8001

此 php 后台服务来自此处: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();

我运行这个php -S localhost:8001,我可以在我的ubuntu服务器上curl。

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

会输出hello, bla

运行 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 

简单的 php 服务正在 ip4 和 ip6 中监听 8001

ufw 防火墙

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) 

防火墙可以监听8001

www.conf,只听8001

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

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

问题:

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

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

我期待你好 bla

这里是 nginx + php-fpm 在 documentation.

中的配置示例

你还没有在你的配置中:

  • fastcgi 参数
  • 等等

这是上游的例子:

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

您应该修复根字符串中 public 目录的路径。

@mindfl 所说的一切,加上您没有捕获所有位置块。您唯一的位置块是这个,它为以 .php

结尾的请求定义正则表达式匹配
location ~ \.php$ {
    fastcgi_pass vault;
}

http://vault.shopshop.space/hi/bla 不会以 .php 结尾,并且由于您没有 indextry_files 指令,因此该位置永远不会处理该请求块。

@mindfl 发布的配置对您来说应该是一个很好的起点。