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

时间:2018-08-29 13:28:39

标签: 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 serivce监听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 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服务正在ip800和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

2 个答案:

答案 0 :(得分:1)

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

您尚未进行配置:

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

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

答案 1 :(得分:0)

@mindfl所说的一切,再加上您没有捕获所有位置块。您唯一的位置块是this,它为以function start_server($sshhost, $sshport, $sshuser, $sshpw){ $GLOBALS['sshhost'] = $sshhost; $GLOBALS['sshport'] = $sshport; $GLOBALS['sshuser'] = $sshuser; $GLOBALS['sshpw'] = $sshpw; /* do more stuff */

结尾的请求定义一个正则表达式匹配项
.php

location ~ \.php$ { fastcgi_pass vault; } 不能以http://vault.shopshop.space/hi/bla结尾,并且由于您没有.phpindex指令,因此该请求永远不会被该位置块处理。

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