Nginx Typecho 自定义 404 页面不适用于不存在的 PHP 文件(摆弄了两天快哭了) - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jimmy2010
V2EX    问与答

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

  •  
  •   jimmy2010 2014-06-24 16:14:10 +08:00 6721 次点击
    这是一个创建于 4174 天前的主题,其中的信息可能已经有所发展或是发生改变。
    访问某些文件类型不存在的文件,如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;
    第 1 条附言    2014-06-24 23:01:38 +08:00
    终于找到解决方案了
    我把 pathinfo.conf 中的
    try_files $fastcgi_script_name = 404;
    换成:
    try_files $uri $uri/ /index.php?/$request_uri;
    把不存在的文件跳转成 /index.php?/$request_uri,404就转交给程序处理而不是nginx标准404了
    第 2 条附言    2018-01-11 13:08:27 +08:00
    @mhcyong 额,看到你收藏了这个贴子,不知道是误点还是和我当时遇到一样的问题,我后来好像解决了,特意去翻了以前的 nginx 配置文件,希望对你有帮助:
    环境是 lnmp-org 一键包那个,应该是 1.1 版本。
    nginx.conf 关键部分:
    -----------------------------------
    if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php$1 last;
    }
    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)$
    {
    try_files $uri /index.php;
    expires 30d;
    }

    location ~ .*\.(js|css|html)?$
    {
    try_files $uri /index.php;
    expires 12h;
    }
    -----------------------------------

    pathinfo.conf 部分:
    -----------------------------------
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    set $path_info $fastcgi_path_info;
    fastcgi_param PATH_INFO $path_info;
    try_files $fastcgi_script_name /index.php;
    -----------------------------------

    fastcgi.conf 是默认配置没有改动。
    5 条回复    2014-06-25 09:52:29 +08:00
    pfitseng
        1
    pfitseng  
       2014-06-24 16:45:52 +08:00   1
    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
        2
    jimmy2010  
    OP
       2014-06-24 17:00:23 +08:00
    @pfitseng 试了一下,好像还是不行,只要是php jpg css js文件不存在的话,还是nginx默认的404.。
    oott123
        3
    oott123  
       2014-06-25 00:59:20 +08:00 via Android   1
    正常的写法不是这样的么?
    try_files $uri $uri/ /index.php
    然后去掉 rewrite 规则。
    jimmy2010
        4
    jimmy2010  
    OP
       2014-06-25 01:38:41 +08:00
    @oott123 谢谢,我明天再试试,我后来说的解决方案导致了一个新的问题:后台进不去了,点击登录后跳转回了登录前界面。
    去掉rewrite规则还可以开启伪静态吗?
    oott123
        5
    oott123  
       2014-06-25 09:52:29 +08:00 via Android
    @jimmy2010 try_files 的原理就是,判断这个文件或者目录是否存在,不存在的话就交给 index.php 处理。这取代了伪静态规则。
    不过,我也是凭印象写的,可能需要你做一些小调整。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2948 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 14:00 PVG 22:00 LAX 06:00 JFK 09:00
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86