nginx不提供PHP,但提供下载

时间:2018-09-13 18:06:46

标签: php nginx php-7 fastcgi fpm

我在这里找到了类似的问题:Nginx serves .php files as downloads, instead of executing them,但是不幸的是,那里的答案对我没有帮助。所以这就是我的故事。

有效的方法

当我导航到myipaddress / index.html时,欢迎使用nginx页面会正确呈现。 当我导航到myipaddress / widgets / index.html时,欢迎来到nginx页面会正确呈现。注意:我从上面的索引中略微更改了index.html,因此欢迎消息有所不同。这样,我向自己证明了它确实找到了两个不同的index.html文件/文件夹结构。

什么不起作用

当我导航到myipaddress / widgets / info.php时,它没有显示phpinfo()命令的详细信息,而是 下载 该php文件。

已安装的软件包

我正在运行alpinelinux。安装了以下软件包:

lab-1:/var/www/localhost/htdocs# cat /etc/apk/world
curl
nginx
php7-common
php7-fpm

Nginx版本

lab-1:/etc/php7# nginx -v
nginx version: nginx/1.14.0

rc状态

lab-1:/var/www/localhost/htdocs# rc-status
Dynamic Runlevel: manual
 nginx        [  started  ]
 php-fpm7     [  started  ]

文件夹结构

lab-1:/var/www/localhost/htdocs# ls -lah
total 16
drwxr-xr-x    3 www      www         4.0K Sep 13 14:22 .
drwxr-xr-x    3 www      www         4.0K Sep 13 13:17 ..
-rw-r--r--    1 www      www          624 Sep 12 20:35 index.html
drwxr-xr-x    2 www      www         4.0K Sep 13 17:40 widgets


lab-1:/var/www/localhost/htdocs# ls -lah widgets/
total 16
drwxr-xr-x    2 www      www         4.0K Sep 13 17:40 .
drwxr-xr-x    3 www      www         4.0K Sep 13 14:22 ..
-rw-r--r--    1 root     root         632 Sep 13 14:23 index.html
-rwxr-xr-x    1 www      www           27 Sep 13 15:45 info.php

配置文件

netstat证明php fpm正在运行

lab-1:/etc/php7# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      4268/php-fpm.conf)
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4323/nginx.conf
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      346/sshd
tcp        0      0 :::80                   :::*                    LISTEN      4323/nginx.conf
tcp        0      0 :::22                   :::*                    LISTEN      346/sshd

ps -A

4268 root      0:00 {php-fpm7} php-fpm: master process (/etc/php7/php-fpm.conf)
4275 nginx     0:00 {php-fpm7} php-fpm: pool www
4276 nginx     0:00 {php-fpm7} php-fpm: pool www
4323 root      0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
4324 nginx     0:00 nginx: worker process
4325 nginx     0:00 nginx: worker process
4326 nginx     0:00 nginx: worker process
4328 nginx     0:00 nginx: worker process
4329 nginx     0:00 nginx: worker process
4330 nginx     0:00 nginx: worker process
4331 nginx     0:00 nginx: worker process
4332 nginx     0:00 nginx: worker process

评论

我试图确保侦听所有者和侦听组与nginx运行的用户相同,即“ nginx”。 我也知道使用“ 127.0.0.1:9000”不如使用unix套接字好。但是我现在尝试使用默认的安装设置来安装php7-fpm。 (说实话,我也不知道如何更改它)

问题

  1. 你能看到我哪里出问题了吗?
  2. 下载php文件的事实是否证明它在正确的conf文件中找到了“ location”指令?那么问题是否仅限于widgets.conf文件中我的location {}指令的内容?
  3. 一旦我解决了php问题,如果我的设置中确实存在巨大的漏洞(可以看到的话),可以指出来吗?谢谢。

编辑1

我在机器上安装了curl并尝试执行以下操作:

lab-1:/etc/php7# curl http://10.11.11.1111/widgets/info.php
<?php

     phpinfo();

?>

与此:

lab-1:/etc/php7# curl http://widgets/index.html
curl: (6) Could not resolve host: widgets

但是我不太知道如何解决这个问题。我希望系统始终要求先指定IP地址,然后再指定文件夹名/应用程序名。

1 个答案:

答案 0 :(得分:1)

查看文档wrt server_nameserver_name widgets;创建一个称为小部件的虚拟服务器,因此它将期望在该服务器上处理的http请求。

使用myipaddress/widgets/info.php,您将在标记为“ default_server”的服务器上发送请求(因为它是通过其ip地址而不是通过名称来寻址的),在您的情况下是默认的,它不支持php。

您可以通过将php支持放入默认块来解决此问题,但我认为这只是伪装了可疑的配置。


因此,根据您的编辑,我假设您希望服务器在IP地址上提供php页面,您将希望从小部件中添加位置块。 conf文件添加到默认服务器配置块(location /之上,否则将捕获所有对php文件的请求,因为它是首先定义的)

location ~* \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi.conf;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
}

,然后将小部件的文件放在/var/www/localhost/widgets中。这样,它们将用作http://ipaddress/widgets/filename.php

但是请注意,此应该被视为临时解决方法。提供IP地址是非常有限的,并且除其他外,这将使您无法正确设置https网站。但是,要在开发人员中启动和运行它应该会有所帮助。