Typecho安装后后台界面和文章链接均为404错误的解决方法

这是由于Nginx对pathinfo支持不够的问题,需要手动添加地址重写代码。
在站点配置文件,默认为

/etc/nginx/sites-available/default

中对应本Typecho的

server{
    ...
    location / {
        ...
        //这里添加
        ...
        try_files $uri $uri/ =404;
        ...
    }
}

添加如下代码:

if (-f $request_filename/index.html){
    rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
    rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
    rewrite (.*) /index.php;
}

然后重启Nginx:

sudo service nginx restart

就可以打开后台控制面板和前台的文章链接了。

如果问题仍然存在请检查fastcgi配置,以及PHP的cgi.fix_pathinfo问题,参见在Ubuntu14.04上搭建LEMP环境

以上代码同样适合宝塔面板,修改方式如下:

将以上代码添加到配置文件中

参考我的配置文件:

server {
    listen 80;
    server_name typecho.tips www.typecho.tips;
    root /usr/share/nginx/html/blog;
    index index.php index.html index.htm;

    location / {
         index index.html index.php;
        if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
        }
        if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
        }
        if (!-f $request_filename){
            rewrite (.*) /index.php;
        }
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

广告栏+++++++蜜糖商店|大哥云| 搬瓦工JMS|红莓网络| Mielink|萌喵加速| 飞鸟云

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注