【问题标题】:NGINX Index 404 Not FoundNGINX 索引 404 未找到
【发布时间】:2018-01-20 13:57:46
【问题描述】:

我刚刚在运行 NGINX 的新服务器上安装了一个 Ghost 博客。 Ghost config.json 文件指向正确的目录 /blog 并且当我访问它时博客加载正常。

不起作用的是,当我从 URL 中删除 /blog 时,我会进入 404 页面。我检查了启用站点的文件,如下所示:

server {
listen 80;
listen [::]:80;

server_name *********;
root /var/www/ghost/system/nginx-root;

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;
}

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

client_max_body_size 50m;

但我不完全确定需要更改哪些内容才能避免出现 404 错误。我有一个示例 .php 文件,应该加载但没有。

我一直使用 Digital Ocean One-Click Ghost 应用程序,但这次我想使用 Ghost CLI。我有一种感觉,我错过了一些东西。

【问题讨论】:

  • /var/www/ghost/system/nginx-root上创建index.php
  • 试过了,但它现在给了我一个 403 Forbidden 错误。
  • 没关系 - 我错过了关于 PHP 文件的部分......

标签: php nginx ghost


【解决方案1】:

以下可能会消除您的一些限制,但它会起作用

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name _;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/thedomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/thedomain.com/privkey.pem;

    access_log /var/log/nginx/thedomain.access.log;
    error_log  /var/log/nginx/thedomain.error.log;

    root            /var/www/thedomain;

    index           index.html;

    gzip on;
    gzip_proxied any;
    gzip_types text/css text/javascript text/xml text/plain application/javascript application/x-javascript application/json;

    location / {
        try_files   $uri $uri/ =404;
    }
}

您需要确保所有的 ssl 文件都在那里并且允许 www-data 访问。

如果你第一次需要运行 certbot,只要不带 ssl 语句的 80 块中的 443 代码

【讨论】:

  • 感谢您的回答。我尝试了您的代码,但在启动 NGINX 时出现以下错误 - “nginx.service 的作业失败,因为控制进程以错误代码退出。有关详细信息,请参阅“systemctl status nginx.service”和“journalctl -xe”。”
  • @Jayp 好的,让我设置一下,一秒钟
  • 当然,不用担心
  • @Jayp 好的,我让它运行了,你需要确保 ssl 文件在那里并且允许 www-data 访问(我认为)。祝你好运
  • 谢谢,我会调查的!
【解决方案2】:

您发布的 nginx 配置仅处理 Ghost。

您已经设置了一个响应端口 80 的服务器,将根设置为 Ghost 的 nginx-root,并创建了 2 个位置块。一个用于/blog/ 并为Ghost 服务,第二个.well-known 块用于处理使用letsencrypt 生成SSL 证书。

我不是为 PHP 配置 nginx 的专家,但 this guide from Digital Oceanthis stackoverflow question 涵盖了很多细节。

我认为你有几个选择:

  1. 设置索引为index.php
  2. / 添加一个新的位置块,用于提供 php 文件
  3. 添加一个块来处理所有的php文件

我相信像这样添加一个新的位置块,意味着如果 URL 中的路径匹配,您拥有的任何 .php 文件都会被调用。

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

但 fastcgi_pass 的值将取决于您在系统上设置 PHP 的方式。

有了这个,去 /index.php 应该可以了。

设置index: index.php 将意味着/ 映射到/index.php 我不确定这是否会干扰Ghost,如果确实如此,您需要一个特定的location / {} 块而不是设置的索引.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-23
    • 2016-12-27
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    相关资源
    最近更新 更多