我正在尝试设置动态 404 页面,以便我可以保留缺失内容的 URL,将其显示在 404 页面上并保持我的网站模板一致。
这是我的网站配置:
server {
listen 80;
server_name www.example.com;
client_max_body_size 1024M;
client_body_buffer_size 512M;
if ($http_host !~* "^www\.example\.com"){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite ^/(.*)$ http://www.example.com/$1 permanent;
break;
}
rewrite ^/([a-zA-Z0-9-_]+)$ /profile.php?url=$1 last;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1y;
log_not_found off;
}
location / {
root /var/www/html/example.com;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /var/www/html/example.com;
fastcgi_pass example_fast_cgi;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/example.com$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 = @handler;
location @handler {
root /var/www/html/example.com;
try_files $uri /404.php = 404;
}
location ~ /\.ht {
deny all;
}
}
看起来好像已经差不多了,但是任何应该看到 404 页面的页面都会收到下载原始 PHP 文件的提示。添加 FastCGI 参数没有任何作用。
答案1
这应该够了吧。
server {
listen 80;
server_name example.com;
return 301 $scheme://www.$server_name$request_uri;
}
server {
listen 80;
server_name www.example.com;
root /var/www/html/example.com;
index index.php index.html index.htm;
error_page 404 /404.php;
location / {
rewrite ^/([a-zA-Z0-9-_]+)$ /profile.php?url=$1 last;
location ~ /\. {
return 404;
}
location ~* ^.+\.php$ {
include fastcgi_params;
fastcgi_pass php;
}
}
}