Nginx自定义错误页面未使用指定页面


Nginx custom error_page not using the specified page?

我正在尝试在特定服务器上显示一个自定义404页面。404错误页面位于/www/example/html/404.php,网站根目录为/www/eExample/html/

目前,任何不存在的页面都会产生此结果https://i.stack.imgur.com/4ojd5.png,并且似乎没有加载我的自定义404页面(该页面具有一些自定义样式和菜单)。几天来,我一直在用不同的.conf设置来解决这个问题,下面是我当前的nginx.conf。我使用的是Ubuntu 12,Nginx 1.1.19,使用的是php-fpm。

user       www-data;
worker_processes  5;
error_log  logs/error.log;
worker_rlimit_nofile 8192;
events {
    worker_connections  4096;
}
http {
    include    mime.types;
    include    fastcgi_params;
    index    index.php index.html index.htm;
    default_type application/octet-stream;
    log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log   logs/access.log  main;
    sendfile     on;
    tcp_nopush   on;
    server_names_hash_bucket_size 128;
    server {
            listen 80;
            server_name example.com;
            client_max_body_size 20M;
            root /www/example/html;
            access_log logs/example.access.log;
            location / {
                    index index.php index.html index.htm;
                    try_files $uri $uri/ @rewrite;
            }
            location @rewrite {
                    rewrite ^/(.*)$ /index.php?q=$1;
            }
            location ~* '.(jpg|jpeg|gif|css|png|js|ico|html)$ {
                    access_log off;
                    expires max;
            }
            location /404.php {
                    internal;
            }
            location ~ '.php$ {
                    include fastcgi_params;
                    fastcgi_intercept_errors on;
                    error_page 404 /404.php;
                    fastcgi_pass   127.0.0.1:9000;
            }
            location ~ /'.ht {
                    deny  all;
            }
    }

我认为您应该执行以下操作:

error_page 404 /www/example/html/404.php;
location /404.php {
  return 404;
}

您的访问和错误日志显示了什么?

第二种选择。为您的"服务器"上下文定义一个自定义404页面:

server {
...
error_page 404 /www/example/html/404.php;
...
}

您是否尝试在location ~ '.php$块中使用:

error_page 404 = /404.php;

location ~ '.php$ {
    error_page 404 @fallback;
}
location @fallback {
    proxy_pass http://example.com/html/404.php
}