简体   繁体   English

Nginx 自定义 error_page 与 php

[英]Nginx custom error_page with php

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.我是 nginx 的新手,为了测试它,我从 Apache 迁移,剩下的就是设置自定义错误页面。

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.这些页面是 php 文件(用于多语言目的),我尝试了 StackOverflow 中的许多方法,但我不知道如何制作。

Some of them are:他们之中有一些是:

So far I've created a file /etc/nginx/common/default-error-pages.conf to include it on the vhosts.到目前为止,我已经创建了一个文件/etc/nginx/common/default-error-pages.conf以将其包含在 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. php 文件位于/var/www/error如果我启用自动索引,我可以从浏览器中查看所有文件,但如果我单击其中任何一个,则会显示默认的 404 页面。

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.我已经创建了其中一个文件到测试站点的符号链接,即使 css 按预期加载,它也会正确执行。

I've tried nesting the locations and including this config file at the top of the site server block.我已经尝试嵌套位置并将此配置文件包含在站点server块的顶部。

If I change the extension of the php error files to html they are correctly served.如果我将 php 错误文件的扩展名更改为 html ,它们将得到正确服务。

The nginx site is similar to this (file simplified): nginx 站点与此类似(文件简化):

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. Nginx 日志文件不会显示任何意外错误/配置。

Thanks in advance提前致谢

I got wrong the root as Richard Smith told in the comment below the post.正如理查德史密斯在帖子下方的评论中所说,我弄错了root

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;我还嵌套了location块,以便正确处理 php 文件,因为其他文件正在使用fastcgi_intercept_errors on; to catch the http errors.捕捉 http 错误。

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM