nginx无法解析PHP程序,一开启网页就直接下载PHP程序

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

代码已贴上,环境是阿里云上的集成环境,如何解决(403页面并自动下载)

阅读 8.2k
5 个回答

/etc/nginx/default.d 目录下的配置文件什么内容??

不过不管是什么内容,都没有看到:

location ~ \.php(.*) {
    fastcgi_pass 127.0.0.1:9000;
    .....
}

这样的代码,说明你没有设置如果碰到类似 index.phptest.php 这样的以 .php(.*) 结尾文件时的处理方式!

当然他就会当成是不认识的文件进行下载了。

Nginx和PHP是通过FastCGI的

location ~ \.php {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
}

Nginx是服务器,主要用来处理HTTP请求,其本身并不支持解析运行PHP代码的能力,需要配合php-fpm来运行php代码。

从题主上面贴出的配置,还不能准确判断出问题所在,需要知道
include /etc/nginx/default.d/*.conf; 这个文件下的配置文件内容。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题