【发布时间】:2019-10-22 20:41:31
【问题描述】:
我是 nginx 新手,为了测试它,我从 Apache 迁移,剩下的就是设置自定义错误页面。
这些页面是 php 文件(用于多语言目的),我尝试了 StackOverflow 中的许多方法,但我不知道如何制作。
其中一些是:
- nginx-php-fpm-custom-error-pages
- nginx-fastcgi-for-php-error-page
- nginx-error-pages-one-location-rule-to-fit-them-all
到目前为止,我已经创建了一个文件 /etc/nginx/common/default-error-pages.conf 以将其包含在虚拟主机中。该文件包含:
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;
}
php 文件位于/var/www/error,如果我启用自动索引,我可以从浏览器中查看所有文件,但如果我点击其中任何一个,则会显示默认的 404 页面。
我已经创建了其中一个文件到测试站点的符号链接,即使 css 按预期加载,它也会正确执行。
我已尝试嵌套位置并将此配置文件包含在站点顶部 server 块中。
如果我将 php 错误文件的扩展名更改为 html,它们会正确提供。
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;
}
}
应该如何配置?我已经改了很多次了,我不知道还能尝试什么。
Nginx 日志文件不会显示任何意外错误/配置。
提前致谢
【问题讨论】:
-
要使用 URI
/error/400.php访问/var/www/error/400.php需要root /var/www;语句,不是root /var/www/error/; -
改了,效果很好,非常感谢,我还嵌套了
location块来捕捉php文件的配置,不要弄乱其他文件。
标签: php nginx ubuntu-server