0

I'm new into nginx and in order to test it I'm migrating from Apache and all I got left is setting up the custom error pages.

These pages are php files (for multi language purposes), I have tried many of the methods from StackOverflow but I cannot figure out how to make it.

Some of them are:

So far I've created a file /etc/nginx/common/default-error-pages.conf to include it on the vhosts. This file contains:

error_page 400 /error/400.php;
error_page 401 /error/401.php;
error_page 403 /error/403.php;
error_page 404 /error/404.php;

error_page 500 /error/500.php;
error_page 503 /error/503.php;

location /error/ {
    alias /var/www/error/;
    autoindex on;
}

location ~ \.php$ {
    root /var/www/error/;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;

    # fastcgi_intercept_errors on;
}

The php files are located in /var/www/error if I enable the autoindex I'm able to view them from the browser all the files but if I click in any of them the default 404 page is shown.

I have created a symlink of one of those files to a testing site and it gets executed correcly even the css loads as expected.

I've tried nesting the locations and including this config file at the top of the site server block.

If I change the extension of the php error files to html they are correctly served.

The nginx site is similar to this (file simplified):

server {
        listen 4430 ssl http2 default_server;
        listen [::]:4430 ssl http2 default_server;
        server_name test.local;

        root /var/www/html;
        index index.php index.html;
        # execute php files "helper"
        include common/php-files.conf;
        fastcgi_hide_header X-Powered-By;
        include common/default-error-pages.conf;

        location / {
                # autoindex on;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
}

How is supposed to be configured? I've changed it so many times that I don't know what else to try.

Nginx logs files don't reveal any unexpected error/configuration.

Thanks in advance

Alexander BC
  • 130
  • 2
  • 10
  • To access `/var/www/error/400.php` using the URI `/error/400.php` requires a `root /var/www;` statement, **not** `root /var/www/error/;` – Richard Smith Oct 23 '19 at 10:37
  • Changed and it worked, thank you a lot, I also nested the `location` blocks to catch the configuration of the php files and do not mess up with the others. – Alexander BC Oct 23 '19 at 13:12

1 Answers1

2

I got wrong the root as Richard Smith told in the comment below the post.

After changing it to the suggested one all worked perflectly.

I also nested the location blocks so the php files are correctly handled, as others are being processed with fastcgi_intercept_errors on; to catch the http errors.

So the config stays as:

error_page 403 /error/403.php;
error_page 404 /error/404.php;

error_page 500 /error/500.php;
error_page 503 /error/503.php;

location /error/ {
    root /var/www/;
    try_files $uri $uri/ =404;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_intercept_errors off;
    }
}
Alexander BC
  • 130
  • 2
  • 10