You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NGINX下Ghost Blog根路径访问404问题求助

解决NGINX下Ghost博客根路径404及PHP文件无法加载的问题

你遇到的问题核心是当前NGINX配置缺少对根路径请求和PHP文件的处理规则,我来帮你一步步修正:

1. 修复根路径访问404的问题

你现在的配置只处理了/blog路径的代理请求,根路径/没有对应的规则,NGINX会默认去root /var/www/ghost/system/nginx-root目录找默认索引文件(比如index.html),但这个目录里并没有相关文件,所以返回404。你可以根据需求二选一:

方案A:根路径自动跳转到/blog

如果希望用户访问域名根路径时自动跳转到/blog页面,添加这个location块:

location = / {
    return 301 /blog;
}

方案B:根路径直接加载Ghost博客

如果你想让根路径直接显示博客内容(不用加/blog后缀),需要调整两个地方:

  • 把原有的location ^~ /blog改成location /
  • 修改Ghost的config.json文件,将url字段改为你的域名根路径(比如https://yourdomain.com,去掉末尾的/blog)

修改后的代理location块如下:

location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_pass http://*********:2368;
    proxy_redirect off;
}

2. 修复example.php无法加载的问题

要让NGINX解析PHP文件,首先确保服务器已经安装了PHP-FPM(如果没装,执行apt install php-fpm或对应系统的安装命令),然后添加专门处理PHP请求的location块:

假设你的example.php放在/var/www/html目录下(可根据实际路径调整),添加以下配置:

location ~ \.php$ {
    root /var/www/html; # 替换为你的PHP文件实际存放目录
    fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 这里的版本号要和你安装的PHP-FPM对应,比如php7.4-fpm.sock
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

最终完整配置示例(以方案A为例)

server {
    listen 80;
    listen [::]:80;
    server_name *********;
    root /var/www/ghost/system/nginx-root;

    # 根路径重定向到/blog
    location = / {
        return 301 /blog;
    }

    location ^~ /blog {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://*********:2368;
        proxy_redirect off;
    }

    # PHP文件解析规则
    location ~ \.php$ {
        root /var/www/html; # 替换为你的PHP文件实际目录
        fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 匹配你的PHP-FPM版本
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}

最后生效步骤

  1. 执行nginx -t检查配置语法是否正确,输出test is successful代表没问题
  2. 重启NGINX让配置生效:sudo systemctl restart nginx
  3. 如果用了方案B,还要重启Ghost:ghost restart

内容的提问来源于stack exchange,提问作者Jayp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:41:48