codersofthedark
codersofthedark

Reputation: 9645

Why is Custom Error Page not working in my nginx configuration?

I have following server block:

server {
    listen 80;
    server_name utsav-dev.domain.org;
    root /var/www/domain-utsav-dev/web;
    access_log /var/log/nginx/domain-utsav-dev-access.log;
    error_log  /var/log/nginx/domain-utsav-dev-error.log error;
    error_page 404 = /var/www/domain/web/50x.html;

    set $thttps $https;
    set $tscheme $scheme;
    if ($http_x_forwarded_proto = http) {
      set $thttps off;
      set $tscheme "http";
    }
    if ($http_x_forwarded_proto = HTTP) {
      set $thttps off;
      set $tscheme "http";
    }



    index app.php index.html index.htm;
    try_files $uri $uri/ @rewrite;
    location @rewrite {
        rewrite ^/(.*)$ /app.php/$1;
    }
    # CSS and Javascript
    location ~* \.(?:css|js)$ {
      expires 86400;
      access_log off;
      add_header Cache-Control "public";
    }

    location ~ \.php {
        fastcgi_index app.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION "www";
        fastcgi_param APPLICATION_ENV "dev";
    fastcgi_param PDFLIBLICENSEFILE "/etc/php5/fpm/pdflib.license";
        fastcgi_param   HTTPS           $thttps;
    }

    location ~ /\.ht {
        deny all;
    }
   chunked_transfer_encoding off;

}

Now as per script if a page is not found, nginx should internally redirect to 50x.html but when I try to open http://utsav-dev.domain.org/blah.html it gives me error "File Not Found" than custom 404 page I was expecting. Why?

Upvotes: 0

Views: 2199

Answers (1)

akawhy
akawhy

Reputation: 1608

try to set fastcgi_intercept_errors on; in your fastcgi location.

fastcgi_intercept_errors on | off; Determines whether FastCGI server responses with codes greater than or equal to 300 should be passed to a client or be redirected to nginx for processing with the error_page directive.

when you visit http://utsav-dev.domain.org/blah.html:

  • first check each location. No one hit
  • it go through the try_files. Hit the @rewrite location.
  • the uri changed to /app.php/blah.html
  • hit the fastcgi location.

you should set fastcgi_intercept_errors on; to let error_page work

Upvotes: 1

Related Questions