AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 902024
Accepted
BentCoder
BentCoder
Asked: 2018-03-17 05:59:59 +0800 CST2018-03-17 05:59:59 +0800 CST 2018-03-17 05:59:59 +0800 CST

Nginx 不会从远程服务器提供 PHP 文件

  • 772

当我调用 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))

如下所示,可以从 NGINX 服务器通过端口访问 PHP 服务器,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;
nginx
  • 2 2 个回答
  • 2982 Views

2 个回答

  • Voted
  1. Best Answer
    BentCoder
    2018-03-17T16:05:01+08:002018-03-17T16:05:01+08:00

    已排序

    所以这一切都是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
  2. Richard Smith
    2018-03-17T06:36:36+08:002018-03-17T06:36:36+08:00

    你有这样的声明:

    try_files $fastcgi_script_name =404;
    

    哪个测试本地机器上是否存在该文件。由于您试图在其他机器上执行 PHP 脚本,因此该try_files语句可能会强制 404 响应。

    要么删除该try_files语句并依靠远程机器检查脚本文件的存在并正确处理不受控制的请求漏洞利用。

    或者,在两台服务器上镜像安装。

    有关更多信息,请参阅此文档。

    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve