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

标签 php ubuntu nginx fpm

在 ubuntu 18.04 上,我正在运行 nginx

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 服务监听 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

最佳答案

这里是 documentation 中 nginx + php-fpm 的配置示例.

您的配置中没有:

  • 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)目录的路径。

    关于php - 如何在 ubuntu 上使用 php 设置 nginx 上游?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52078642/

    相关文章:

    nginx 问题 : reload: Name "com.ubuntu.Upstart" does not exist

    php - Laravel mysql json 数据选择

    php - 在 laravel 5.4 中测试文件上传时出错

    ruby - rvm help 命令输出看起来很奇怪

    linux - Linux 上已安装程序的文件位置有什么意义?

    php - 为某些网站切换 nginx php 版本

    python - django:使用 NGINX 的 SECURE_SSL_REDIRECT 应用程序崩溃

    php - 如何使用 PHP MYSQL 计算特定类别的记录数

    javascript - 在 php 和 html5 中使用 javascript 添加行

    python - Ubuntu 中使用 python 的屏幕录像机