8

我正在一个新的 EC2 实例上设置我的博客,因为当前托管它的服务器上的一个站点正在被 DDoSed。我在使用 nginx 时遇到了一些问题,因为我可以看到所有页面都很好,但索引上的 403,或者看到索引但页面上的 404(取决于我使用的配置)

这是我的 nginx 配置:

server {
    listen       80;

    server_name  www.test.com;
    server_name  test.com;
    root /www/blog;

    include conf.d/wordpress/simple.conf;
}

和 simple.conf:

location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location / {
            # This is cool because no php is touched for static content. 
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            include fastcgi.conf;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }

如果我改变 try_files $uri $uri/ /index.php?$args; 索引 index.php,首页将正常工作,其余为 404。如果我这样保留,首页为 403。

这是错误日志:

2013/08/07 19:19:41 [error] 25333#0: *1 directory index of "/www/blog/" is forbidden, client: 64.129.X.X, server: test.com, request: "GET / HTTP/1.1", host: "www.test.com"

该目录在 nginx 用户上是 755:

drwxr-xr-x 6 nginx nginx  4096 Aug  7 18:42 blog

有什么明显的我做错了吗?

谢谢 !

4

3 回答 3

23

添加index index.php;在服务器块中,如果它不起作用,那么您需要删除,$uri/因为您不想做autoindex on


编辑:刚刚注意到你已经发现了你的问题,所以我会添加它背后的原因,你需要的原因autoindex on;是因为没有它,nginx 将遵循try_files规则,

  1. 检查是否有一个名为 的文件/,当然它会失败。
  2. 检查是否有一个名为的目录/(通过添加 root 它将 = /www/blog/),此检查将成功,因此它会尝试列出该文件夹的内容。
  3. 由于您没有指定autoindex on;,默认情况下 nginx 应该禁止列出目录,因此它会返回 403 禁止错误。
  4. 该站点的其余部分工作正常,因为它未通过$uri/测试或无法访问它,因为您可能没有名为image.jpgor的文件夹stylesheet.css
于 2013-08-07T21:48:16.373 回答
1

看起来我需要在服务器 {} 定义中而不是在位置 {} 中的 inded index.php

于 2013-08-07T19:43:46.807 回答
0

您似乎不允许将参数发送到 CMS,因此这不会显示此 uri,该 uri 会从数据库中获取信息并将您重定向到 403 页面。

于 2013-08-07T19:01:03.670 回答