Nginx 自定义错误页面不起作用

Nginx 自定义错误页面不起作用

我的 Nginx 没有提供自定义 4x 和 5x 错误页面。我的配置如下

error_page 400 404 /404;
error_page 500 502 504  /service-unavailable;

/404/service-unavailable两个 URL 在我的网站上实际存在。

完整服务器块配置

server {
    listen       80;# listen port
    server_name  ww.itimes.com ww1.itimes.com;
    root   /iTimesnfs/Nweb/web/itimesV3/public_html;# Document root

    error_page 400 404 /404;
    error_page 500 502 504  /service-unavailable;

    index index.php  index.html index.htm;

    location / {
        try_files   $uri $uri/ /index.php?$args;
    }

    location ~ /\. {
        access_log off;
        log_not_found off;
        deny all;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        #fastcgi_pass 127.0.0.1:9000;
        #fastcgi_index index.php;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_pass fastcgiservers;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        expires 90d;
        add_header Pragma public;
        add_header Cache-Control "public";
        try_files   $uri $uri/ /index.php?$args;
    }
location /nginx_status {
        stub_status on;
        access_log   off;
        allow 127.0.0.1;
        deny all;
    }
}

答案1

将其添加到您的服务器块

location ~* ^/(400|404|500|502|503|service-unavailable)$ {
  log_not_found off;
  root /path/to/your/docroot;
  internal;
}

相关内容