gpt4 book ai didi

php - 将 nginx 错误重定向到 php

转载 作者:可可西里 更新时间:2023-10-31 22:55:25 24 4
gpt4 key购买 nike

我的目标是将 nginx 中的任何错误(即 404、405 等...)重定向到我位于 index.php 的 php 脚本.`

server {

root /usr/share/nginx/TradeLog/www/;
index index.php index.html index.htm default.html default.htm;

# Make site accessible from http://localhost/
server_name localhost;
server_name_in_redirect off;

fastcgi_intercept_errors on;

error_page 403 404 /index.php/;


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

location ~* \.php$
{
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_read_timeout 600; # Set fairly high for debugging
fastcgi_index index.php;
}

location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
expires max;
}
}

`

但是,当我尝试这样做时,遇到了 nginx 404 错误。我在这里做错了什么?

编辑

我发现如果我转fastcgi_intercept_errors onoff ,然后 nginx 处理错误。我的下一步是为将显示错误的 php 脚本设置一个新请求。这是在位置 /errors/code/<code_number> .

假设我有以下位置 block :

location = /composer.phar
{
return 403;
}

然后我想重定向到错误页面 /error/code/403

因此,我在想

error_page 403 = /error/code/403;

会为我解决这个问题,但我的 PHP 脚本仍在获取原始请求 - 我总是显示 404 页面,因为我的 PHP 脚本正在获取一个它不知道的位置请求。我希望 nginx 告诉它要在外部显示哪个页面,然后将错误编号传递给 scrip。

最佳答案

我认为你应该替换:

error_page 403 404 /index.php/;

与:

error_page 403 404 /index.php;

额外的/使重定向到目录,而不是文件。您的其余配置似乎没问题。

关于php - 将 nginx 错误重定向到 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com