404 .php 页面响应 nginx + php fpm

404 .php 页面响应 nginx + php fpm

我刚刚安装了带有 php5-fpm 的 nginx 服务器。为什么如果您访问不存在的 php 页面,您会看到空白页,而不是 nginx 默认的“未找到”页面?这难道不应该像处理非 php 文件一样由 nginx 网络服务器处理吗?

配置

    user www-data;
worker_processes 1;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

服务器

   server {
        listen 80 default;
        server_name  216.231.135.56;
        access_log  /var/log/nginx/default.access.log;
        root   /var/www/default/public;

    ## Default location
        location / {
            index index.php;
        }

    ## Images and static content is treated different
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
          access_log        off;
          expires           30d;
        }

    ## Parse all .php file in the /var/www directory
        location ~ .php$ {
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_pass   backend;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
            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_intercept_errors        on;
            fastcgi_ignore_client_abort     off;
            fastcgi_connect_timeout 60;
            fastcgi_send_timeout 180;
            fastcgi_read_timeout 180;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
        }

    ## Disable viewing .htaccess & .htpassword
        location ~ /\.ht {
            deny  all;
        }
   }



    upstream backend {
            server 127.0.0.1:9000;
    }

答案1

根据fastcgi_intercept_errors文档,您需要error_page为此定义处理程序。因此,将以下行添加到上下文中location

error_page 404 /error/404.php;

404 Not Found当访问不存在的php文件时,会显示默认的错误页面。

您还可以查看fastcgi_params配置文件以避免重复定义fastcgi_param

相关内容