Nginx Typecho 自定义 404 页面不适用于不存在的 PHP 文件(摆弄了两天快哭了)

2014-06-24 16:14:10 +08:00
 jimmy2010
访问某些文件类型不存在的文件,如xxx.html、xxx.log、xxx.txt等,能够显示主题自定义404内容,但是访问一个不存在的PHP、css、js等文件,还是返回nginx默认的404页面。这是怎么回事?

下面是我的配置:
nginx.conf
========
server
{
listen xx.xx.xx.xx:80;
#listen [::]:80;
server_name xx.com www.xx.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/vhosts/xx;

include typecho.conf;

location ~ [^/]\.php(/|$)

{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

}
-------------------------------------------------------------------------
typecho.conf
========
location / {
index index.html index.php;
if (-e $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-e $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-e $request_filename){
rewrite (.*) /index.php;
}
}
-------------------------------------------------------------------------
pathinfo.conf
========
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
try_files $fastcgi_script_name = 404;
6424 次点击
所在节点    问与答
5 条回复
pfitseng
2014-06-24 16:45:52 +08:00
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}

fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
jimmy2010
2014-06-24 17:00:23 +08:00
@pfitseng 试了一下,好像还是不行,只要是php jpg css js文件不存在的话,还是nginx默认的404.。
oott123
2014-06-25 00:59:20 +08:00
正常的写法不是这样的么?
try_files $uri $uri/ /index.php
然后去掉 rewrite 规则。
jimmy2010
2014-06-25 01:38:41 +08:00
@oott123 谢谢,我明天再试试,我后来说的解决方案导致了一个新的问题:后台进不去了,点击登录后跳转回了登录前界面。
去掉rewrite规则还可以开启伪静态吗?
oott123
2014-06-25 09:52:29 +08:00
@jimmy2010 try_files 的原理就是,判断这个文件或者目录是否存在,不存在的话就交给 index.php 处理。这取代了伪静态规则。
不过,我也是凭印象写的,可能需要你做一些小调整。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/119275

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX