我有3个域名,并试图使用Nginx在一台服务器上托管所有3个网站(一个数字海洋液滴)。

mysite1.name mysite2.name mysite3.name

只有1个有效。另外两个结果是403个错误(以同样的方式)。

在我的nginx错误日志中,我看到:[error] 13108#0: *1目录索引“/usr/share/nginx/mysite2.name/live/”被禁止。

我的站点启用配置是:

server {
        server_name www.mysite2.name;
        return 301 $scheme://mysite2.name$request_uri;
}
server {
        server_name     mysite2.name;

        root /usr/share/nginx/mysite2.name/live/;
        index index.html index.htm index.php;

        location / {
                try_files $uri $uri/ /index.html index.php;
        }

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

这三个站点都有几乎相同的配置文件。

每个站点的文件都在/usr/share/nginx/mysite1.name/someFolder这样的文件夹中,然后/usr/share/nginx/mysite1.name/live是指向该文件夹的符号链接。(mysite2和mysite3也是如此。)

我已经查看了Nginx 403禁止所有文件,但这没有帮助。

有什么问题吗?


当前回答

你可能会得到这个,因为Nginx政策(例如。"deny"),或者你可能得到这个是因为Nginx错误配置,或者你可能得到这个是因为文件系统限制。

你可以确定是否是后者(并可能通过使用strace看到错误配置的证据(除非OP无法访问它):

# pidof nginx
11853 11852

# strace -p 11853 -p 11852 -e trace=file -f
Process 11853 attached - interrupt to quit
Process 11852 attached - interrupt to quit
[pid 11853] stat("/var/www/html/kibanaindex.html", 0x7ffe04e93000) = -1 ENOENT (No such file or directory)
[pid 11853] stat("/var/www/html/kibana", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
^CProcess 11853 detached
Process 11852 detached

在这里,我正在检查nginx在运行测试时所做的文件系统活动(我有和你一样的错误)。

这是我当时配置的一部分

    location /kibana/3/ {
        alias /var/www/html/kibana;
        index index.html;
    }

在我的情况下,正如strace非常清楚地显示,在“别名”到“索引”的连接不是我所期望的,似乎我需要养成总是用/追加目录名的习惯,因此在我的情况下,以下工作:

    location /kibana/3/ {
        alias /var/www/html/kibana/;
        index index.html;
    }

其他回答

当你想保留目录选项时,你可以像这样把index.php放在$uri前面。

try_files /index.php $uri $uri/

我解决了我的问题,如果我配置如下:

location = /login {
    index  login2.html;
}

它会显示403错误。

[error] 4212#2916: *2 directory index of "D:\path/to/login/" is forbidden

我试过自动索引,但不工作。 如果我改变我的配置像这样,它工作。

location = /login/ {
    index  login2.html;
}

我认为精确匹配,如果是路径,就应该是目录。

以下是我如何在我的Kali机器上修复它:

定位到目录: cd /etc/nginx/sites-enabled / 编辑'default'配置文件: Sudo nano默认 在location块中添加以下行: 位置/你的目录{ 自动变址; autoindex_exact_size; } 注意,我在一个特定的目录中激活了自动索引 / yourdirectory只。否则,你电脑上的所有文件夹都将启用它,而你不想要它。 现在重新启动你的服务器,它现在应该工作了: Sudo服务nginx重启

您需要对静态文件目录执行权限。此外,它们需要被你的nginx用户和组咀嚼。

下面是工作的配置:

server {
    server_name www.mysite2.name;
    return 301 $scheme://mysite2.name$request_uri;
}
server {
    #This config is based on https://github.com/daylerees/laravel-website-configs/blob/6db24701073dbe34d2d58fea3a3c6b3c0cd5685b/nginx.conf
    server_name mysite2.name;

     # The location of our project's public directory.
    root /usr/share/nginx/mysite2/live/public/;

     # Point index to the Laravel front controller.
    index           index.php;

    location / {
        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$query_string;
    }

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
            rewrite     ^/(.+)/$ /$1 permanent;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #   # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

然后浏览器中唯一的输出是一个Laravel错误:“哎呀,看起来好像出了什么问题。”

不要执行chmod -R 777 app/storage(注)。让某些东西在全世界都可写是很糟糕的安全性。

chmod -R 755 app/storage工作,更安全。