Nginx 不會提供遠端伺服器 PHP 文件

Nginx 不會提供遠端伺服器 PHP 文件

當我呼叫 NGINX 伺服器時,它應該提供儲存在遠端 PHP 伺服器上的 php 檔案。目前我得到了404 Not Found - nginx/1.10.3 (Ubuntu)。如果我更改root /srv/www/site為,root /var/www/html那麼它會成功地index.html從 NGINX 提供服務,因此請求實際上根本不會到達 PHP 伺服器。

  • PHP 伺服器:192.168.99.31 ( PHP 7.1.15)
  • NGINX 伺服器:192.168.99.32 ( nginx version: nginx/1.10.3 (Ubuntu))

PHP 伺服器可以透過連接埠從 NGINX 伺服器訪問9000,如下所示,所以我認為這裡不存在任何與連接相關的問題。

vagrant@nginx:~$ nc -zv 192.168.99.31 9000
Connection to 192.168.99.31 9000 port [tcp/*] succeeded!

PHP

vagrant@php:~$ cat /etc/php/7.1/fpm/pool.d/www.conf

[www]
user                   = www-data
group                  = www-data

listen                 = 9000
listen.allowed_clients = 192.168.99.32
listen.owner           = www-data
listen.group           = www-data

pm = dynamic
pm.max_children        = 5
pm.start_servers       = 2
pm.min_spare_servers   = 1
pm.max_spare_servers   = 3

-

vagrant@php:~$ dpkg --list | grep php

ii  php-common                       1:60+ubuntu16.04.1+deb.sury.org+1
ii  php7.1                           7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-cgi                       7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-cli                       7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-common                    7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-fpm                       7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-json                      7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-mbstring                  7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-mcrypt                    7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-mysql                     7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-opcache                   7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-readline                  7.1.15-1+ubuntu16.04.1+deb.sury.org+2
ii  php7.1-zip                       7.1.15-1+ubuntu16.04.1+deb.sury.org+2

-

vagrant@php:~$ cat /srv/www/site/index.php 
<?php
echo 'Hello from PHP host'.PHP_EOL;

NGINX

vagrant@nginx:~$ cat /etc/nginx/sites-available/default

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /srv/www/site;
        index index.php index.html;
        server_name _;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass 192.168.99.31:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        location ~ /\.ht {
                deny all;
        }

        error_log /var/log/nginx/site_error.log;
        access_log /var/log/nginx/site_access.log;
}

-

vagrant@nginx:~$ cat /etc/nginx/snippets/fastcgi-php.conf

fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name =404;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;

-

vagrant@nginx:~$ cat /etc/nginx/fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

fastcgi_param  REDIRECT_STATUS    200;

答案1

已排序

所以這一切都是為了try_files $uri $uri/ =404;改變try_files $uri $uri/ /index.php?$query_string;。請參閱下面的清理版本。

ubuntu@nginx:~$ cat /etc/nginx/sites-available/default

server {
    listen 80;
    listen [::]:80;

    root /srv/www/site;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_pass 192.168.99.31:9000;
    }

    # Deny access to .htaccess files
    location ~ /\.ht {
        deny all;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    error_log /var/log/nginx/site_error.log;
    access_log /var/log/nginx/site_access.log;
}

答案2

你有這樣的聲明:

try_files $fastcgi_script_name =404;

它測試檔案是否存在本地機器。由於您嘗試在其他電腦上執行 PHP 腳本,因此該try_files語句可能會強制出現 404 回應。

要么刪除該try_files語句,然後依賴遠端機器檢查腳本檔案是否存在並處理不受控制的請求正確利用。

或者,在兩台伺服器上鏡像安裝。

這個文件了解更多。

相關內容