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

问题描述 投票:0回答:2

在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 listen 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服务正在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

我希望你好

php ubuntu nginx fpm
2个回答
1
投票

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

您还没有配置:

  • fastcgi params
  • 等等

这是上游的例子:

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

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


0
投票

@mindfl说的所有内容,加上你没有抓到所有位置块。您唯一的位置块是this,它定义了以.php结尾的请求的正则表达式匹配

location ~ \.php$ {
    fastcgi_pass vault;
}

http://vault.shopshop.space/hi/bla不以.php结尾,因为你没有indextry_files指令,那么该位置块永远不会处理该请求。

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

© www.soinside.com 2019 - 2024. All rights reserved.