所以首先我想解释一下我目前的情况以及我想要实现的目标:
我想制作一个以
code
作为参数的 php 文件,然后处理每个传递的错误代码,遗憾的是这并没有按预期工作,因为它总是返回错误代码为 0
的自定义页面,即使实际的错误代码是不同(403 或 404)。
server {
listen 80;
server_name genefit.cc www.genefit.cc;
root /var/www/html;
index index.htm index.html index.php;
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass genefit.cc:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
# Error page configuration
error_page 400 401 402 403 404 500 502 503 504 /error.php?code=$request_uri;
location ~ /error.php$ {
root /var/www/html;
try_files $uri $uri/ =404;
internal;
fastcgi_pass genefit.cc:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/error.php;
fastcgi_param QUERY_STRING $query_string;
}
}
<?php
$status_code = isset($_GET['code']) ? intval($_GET['code']) : 500;
http_response_code($status_code);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
color: red;
}
.container {
margin-top: 50px;
}
img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>Error <?php echo $status_code; ?></h1>
<p>Something went wrong. Please try again later.</p>
<img src="https://http.cat/<?php echo $status_code; ?>" alt="Error <?php echo $status_code; ?>">
</div>
</body>
</html>
========
在尝试解决上述问题大约1小时后,我放弃了并开始在错误目录中制作单独的php文件,例如
error/403.php
和error/404.php
。这导致了本文标题中提到的太多重定向错误:
server {
listen 80;
server_name genefit.cc www.genefit.cc;
root /var/www/html;
index index.htm index.html index.php;
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile off;
client_max_body_size 100m;
location ~ /\.ht {
deny all;
}
# PHP files handling
location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass genefit.cc:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# Error pages
error_page 400 /error/400.php;
error_page 401 /error/401.php;
error_page 402 /error/402.php;
error_page 403 /error/403.php;
error_page 404 /error/404.php;
error_page 500 /error/500.php;
error_page 502 /error/502.php;
error_page 503 /error/503.php;
error_page 504 /error/504.php;
location / {
try_files $uri $uri/ /error/404.php;
}
# Serve error pages from the /error/ directory
location /error/ {
internal;
root /var/www/html/;
alias /var/www/html/error/;
try_files $uri $uri/ =404;
# PHP files handling
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass genefit.cc:9000;
fastcgi_intercept_errors off;
}
}
}
<?php
// Define the static variable for the error code
$error_code = 404;
// Set the HTTP response code
http_response_code($error_code);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error <?php echo $error_code; ?></title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
color: red;
}
.container {
margin-top: 50px;
}
img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>Error <?php echo $error_code; ?></h1>
<p>Page not found. Please check the URL or try again later.</p>
<img src="https://http.cat/<?php echo $error_code; ?>" alt="Error <?php echo $error_code; ?>">
</div>
</body>
</html>
看看你问题的第二部分......
您有
fastcgi_intercept_errors on;
,这可能是处理其他 PHP 脚本中的错误所必需的。
但是,您的“错误”处理脚本还将 HTTP 响应代码设置为“200 OK”以外的其他代码。这可能是重定向循环的原因。
我说“可能是”是因为 Nginx 通常会避免
error_page
处理,当它当前正在处理 error_page
时。
如果这是问题所在,您可以创建另一个位置块来处理“错误”脚本。
例如:
location ~ ^/error/.*\.php$ {
include fastcgi_params;
fastcgi_pass genefit.cc:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
将此块放置在上方
location ~ \.php$
,因为正则表达式位置将按顺序求值,直到找到匹配项。