V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
jimmy2010
V2EX  ›  问与答

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

  •  
  •   jimmy2010 · 2014-06-24 16:14:10 +08:00 · 6405 次点击
    这是一个创建于 3607 天前的主题,其中的信息可能已经有所发展或是发生改变。
    访问某些文件类型不存在的文件,如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   ·   实用小工具   ·   1218 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:22 · PVG 02:22 · LAX 11:22 · JFK 14:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.