首页 > 解决方案 > Nginx php文件正在下载而不是执行

问题描述

我已经在 CentOs 7 上配置了 Ngnix 服务器,它正在使用 html 查找,但是当我尝试打开 .php 文件时,它会被下载。我正在使用 PHP7.2。这是我的配置(我使用 123.123.123.123 而不是我的真实 IP):

server {
        listen [::]:80 default_server ipv6only=on;
        listen 123.123.123.123:80 default_server;

        root /var/www/example.com/public_html;
        index index.php index.html;

        server_name 123.123.123.123;

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

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

因此,当我尝试访问 123.123.123.123/test/index.php 时,我正在下载文件。

我怎么能强制 ngnix 执行 php 文件?

标签: phpnginxcentos7centos6

解决方案


你还需要 fastcgi_pass:

location ~ \.php$ {        
  fastcgi_pass 127.0.0.1:9000;         
  fastcgi_index index.php;         
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        
  include fastcgi_params; 
  }       

推荐阅读